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