1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.blogs.lar;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.xml.Document;
27  import com.liferay.portal.kernel.xml.Element;
28  import com.liferay.portal.kernel.xml.SAXReaderUtil;
29  import com.liferay.portal.lar.PortletDataContext;
30  import com.liferay.portal.lar.PortletDataException;
31  import com.liferay.portal.lar.PortletDataHandler;
32  import com.liferay.portal.lar.PortletDataHandlerBoolean;
33  import com.liferay.portal.lar.PortletDataHandlerControl;
34  import com.liferay.portal.lar.PortletDataHandlerKeys;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortletKeys;
37  import com.liferay.portlet.blogs.model.BlogsEntry;
38  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
39  import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
40  
41  import java.util.Calendar;
42  import java.util.List;
43  
44  import javax.portlet.PortletPreferences;
45  
46  /**
47   * <a href="BlogsPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Bruno Farache
50   * @author Raymond Augé
51   *
52   */
53  public class BlogsPortletDataHandlerImpl implements PortletDataHandler {
54  
55      public PortletPreferences deleteData(
56              PortletDataContext context, String portletId,
57              PortletPreferences prefs)
58          throws PortletDataException {
59  
60          try {
61              if (!context.addPrimaryKey(
62                      BlogsPortletDataHandlerImpl.class, "deleteData")) {
63  
64                  BlogsEntryLocalServiceUtil.deleteEntries(context.getGroupId());
65              }
66  
67              return null;
68          }
69          catch (Exception e) {
70              throw new PortletDataException(e);
71          }
72      }
73  
74      public String exportData(
75              PortletDataContext context, String portletId,
76              PortletPreferences prefs)
77          throws PortletDataException {
78  
79          try {
80              Document doc = SAXReaderUtil.createDocument();
81  
82              Element root = doc.addElement("blogs-data");
83  
84              root.addAttribute("group-id", String.valueOf(context.getGroupId()));
85  
86              List<BlogsEntry> entries = BlogsEntryUtil.findByGroupId(
87                  context.getGroupId());
88  
89              for (BlogsEntry entry : entries) {
90                  exportEntry(context, root, entry);
91              }
92  
93              return doc.formattedString();
94          }
95          catch (Exception e) {
96              throw new PortletDataException(e);
97          }
98      }
99  
100     public PortletDataHandlerControl[] getExportControls() {
101         return new PortletDataHandlerControl[] {
102             _entries, _comments, _ratings, _tags
103         };
104     }
105 
106     public PortletDataHandlerControl[] getImportControls() {
107         return new PortletDataHandlerControl[] {
108             _entries, _comments, _ratings, _tags
109         };
110     }
111 
112     public PortletPreferences importData(
113             PortletDataContext context, String portletId,
114             PortletPreferences prefs, String data)
115         throws PortletDataException {
116 
117         try {
118             Document doc = SAXReaderUtil.read(data);
119 
120             Element root = doc.getRootElement();
121 
122             List<Element> entryEls = root.elements("entry");
123 
124             for (Element entryEl : entryEls) {
125                 String path = entryEl.attributeValue("path");
126 
127                 if (!context.isPathNotProcessed(path)) {
128                     continue;
129                 }
130 
131                 BlogsEntry entry = (BlogsEntry)context.getZipEntryAsObject(
132                     path);
133 
134                 importEntry(context, entry);
135             }
136 
137             return null;
138         }
139         catch (Exception e) {
140             throw new PortletDataException(e);
141         }
142     }
143 
144     public boolean isPublishToLiveByDefault() {
145         return false;
146     }
147 
148     protected void exportEntry(
149             PortletDataContext context, Element root, BlogsEntry entry)
150         throws PortalException, SystemException {
151 
152         if (!context.isWithinDateRange(entry.getModifiedDate())) {
153             return;
154         }
155 
156         String path = getEntryPath(context, entry);
157 
158         if (!context.isPathNotProcessed(path)) {
159             return;
160         }
161 
162         Element entryEl = root.addElement("entry");
163 
164         entryEl.addAttribute("path", path);
165 
166         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
167             context.addComments(BlogsEntry.class, entry.getEntryId());
168         }
169 
170         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
171             context.addRatingsEntries(BlogsEntry.class, entry.getEntryId());
172         }
173 
174         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
175             context.addTagsEntries(BlogsEntry.class, entry.getEntryId());
176         }
177 
178         entry.setUserUuid(entry.getUserUuid());
179 
180         context.addZipEntry(path, entry);
181     }
182 
183     protected String getEntryPath(
184         PortletDataContext context, BlogsEntry entry) {
185 
186         StringBuilder sb = new StringBuilder();
187 
188         sb.append(context.getPortletPath(PortletKeys.BLOGS));
189         sb.append("/entries/");
190         sb.append(entry.getEntryId());
191         sb.append(".xml");
192 
193         return sb.toString();
194     }
195 
196     protected void importEntry(PortletDataContext context, BlogsEntry entry)
197         throws Exception {
198 
199         long userId = context.getUserId(entry.getUserUuid());
200         long plid = context.getPlid();
201 
202         Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
203 
204         displayDateCal.setTime(entry.getDisplayDate());
205 
206         int displayDateMonth = displayDateCal.get(Calendar.MONTH);
207         int displayDateDay = displayDateCal.get(Calendar.DATE);
208         int displayDateYear = displayDateCal.get(Calendar.YEAR);
209         int displayDateHour = displayDateCal.get(Calendar.HOUR);
210         int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
211 
212         if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
213             displayDateHour += 12;
214         }
215 
216         boolean draft = entry.isDraft();
217         boolean allowTrackbacks = entry.isAllowTrackbacks();
218         String[] trackbacks = StringUtil.split(entry.getTrackbacks());
219 
220         String[] tagsEntries = null;
221 
222         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
223             tagsEntries = context.getTagsEntries(
224                 BlogsEntry.class, entry.getEntryId());
225         }
226 
227         boolean addCommunityPermissions = true;
228         boolean addGuestPermissions = true;
229 
230         ThemeDisplay themeDisplay = null;
231 
232         BlogsEntry existingEntry = null;
233 
234         if (context.getDataStrategy().equals(
235                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
236 
237             existingEntry = BlogsEntryUtil.fetchByUUID_G(
238                 entry.getUuid(), context.getGroupId());
239 
240             if (existingEntry == null) {
241                 existingEntry = BlogsEntryLocalServiceUtil.addEntry(
242                     entry.getUuid(), userId, plid, entry.getTitle(),
243                     entry.getContent(), displayDateMonth, displayDateDay,
244                     displayDateYear, displayDateHour, displayDateMinute,
245                     draft, allowTrackbacks, trackbacks, tagsEntries,
246                     addCommunityPermissions, addGuestPermissions, themeDisplay);
247             }
248             else {
249                 existingEntry = BlogsEntryLocalServiceUtil.updateEntry(
250                     userId, existingEntry.getEntryId(), entry.getTitle(),
251                     entry.getContent(), displayDateMonth, displayDateDay,
252                     displayDateYear, displayDateHour, displayDateMinute,
253                     draft, allowTrackbacks, trackbacks, tagsEntries,
254                     themeDisplay);
255             }
256         }
257         else {
258             existingEntry = BlogsEntryLocalServiceUtil.addEntry(
259                 userId, plid, entry.getTitle(), entry.getContent(),
260                 displayDateMonth, displayDateDay, displayDateYear,
261                 displayDateHour, displayDateMinute, draft, allowTrackbacks,
262                 trackbacks, tagsEntries, addCommunityPermissions,
263                 addGuestPermissions, themeDisplay);
264         }
265 
266         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
267             context.importComments(
268                 BlogsEntry.class, entry.getEntryId(),
269                 existingEntry.getEntryId(), context.getGroupId());
270         }
271 
272         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
273             context.importRatingsEntries(
274                 BlogsEntry.class, entry.getEntryId(),
275                 existingEntry.getEntryId());
276         }
277     }
278 
279     private static final String _NAMESPACE = "blogs";
280 
281     private static final PortletDataHandlerBoolean _entries =
282         new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
283 
284     private static final PortletDataHandlerBoolean _comments =
285         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
286 
287     private static final PortletDataHandlerBoolean _ratings =
288         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
289 
290     private static final PortletDataHandlerBoolean _tags =
291         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
292 
293 }