001
014
015 package com.liferay.portlet.blogs.lar;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
020 import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
021 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
022 import com.liferay.portal.kernel.lar.PortletDataContext;
023 import com.liferay.portal.kernel.trash.TrashHandler;
024 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
025 import com.liferay.portal.kernel.util.StreamUtil;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.kernel.xml.Element;
030 import com.liferay.portal.model.Image;
031 import com.liferay.portal.service.ImageLocalServiceUtil;
032 import com.liferay.portal.service.ServiceContext;
033 import com.liferay.portlet.blogs.model.BlogsEntry;
034 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
035
036 import java.io.InputStream;
037
038 import java.util.Calendar;
039
040
043 public class BlogsEntryStagedModelDataHandler
044 extends BaseStagedModelDataHandler<BlogsEntry> {
045
046 public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
047
048 @Override
049 public void deleteStagedModel(
050 String uuid, long groupId, String className, String extraData)
051 throws PortalException, SystemException {
052
053 BlogsEntry entry =
054 BlogsEntryLocalServiceUtil.fetchBlogsEntryByUuidAndGroupId(
055 uuid, groupId);
056
057 if (entry != null) {
058 BlogsEntryLocalServiceUtil.deleteEntry(entry);
059 }
060 }
061
062 @Override
063 public String[] getClassNames() {
064 return CLASS_NAMES;
065 }
066
067 @Override
068 public String getDisplayName(BlogsEntry entry) {
069 return entry.getTitle();
070 }
071
072 @Override
073 protected void doExportStagedModel(
074 PortletDataContext portletDataContext, BlogsEntry entry)
075 throws Exception {
076
077 Element entryElement = portletDataContext.getExportDataElement(entry);
078
079 if (entry.isSmallImage()) {
080 Image smallImage = ImageLocalServiceUtil.fetchImage(
081 entry.getSmallImageId());
082
083 if (Validator.isNotNull(entry.getSmallImageURL())) {
084 String smallImageURL =
085 ExportImportHelperUtil.replaceExportContentReferences(
086 portletDataContext, entry, entryElement,
087 entry.getSmallImageURL().concat(StringPool.SPACE),
088 true);
089
090 entry.setSmallImageURL(smallImageURL);
091 }
092 else if (smallImage != null) {
093 String smallImagePath = ExportImportPathUtil.getModelPath(
094 entry,
095 smallImage.getImageId() + StringPool.PERIOD +
096 smallImage.getType());
097
098 entryElement.addAttribute("small-image-path", smallImagePath);
099
100 entry.setSmallImageType(smallImage.getType());
101
102 portletDataContext.addZipEntry(
103 smallImagePath, smallImage.getTextObj());
104 }
105 }
106
107 String content = ExportImportHelperUtil.replaceExportContentReferences(
108 portletDataContext, entry, entryElement, entry.getContent(),
109 portletDataContext.getBooleanParameter(
110 BlogsPortletDataHandler.NAMESPACE, "referenced-content"));
111
112 entry.setContent(content);
113
114 portletDataContext.addClassedModel(
115 entryElement, ExportImportPathUtil.getModelPath(entry), entry);
116 }
117
118 @Override
119 protected void doImportStagedModel(
120 PortletDataContext portletDataContext, BlogsEntry entry)
121 throws Exception {
122
123 long userId = portletDataContext.getUserId(entry.getUserUuid());
124
125 Element entryElement =
126 portletDataContext.getImportDataStagedModelElement(entry);
127
128 String content = ExportImportHelperUtil.replaceImportContentReferences(
129 portletDataContext, entryElement, entry.getContent(),
130 portletDataContext.getBooleanParameter(
131 BlogsPortletDataHandler.NAMESPACE, "referenced-content"));
132
133 entry.setContent(content);
134
135 Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
136
137 displayDateCal.setTime(entry.getDisplayDate());
138
139 int displayDateMonth = displayDateCal.get(Calendar.MONTH);
140 int displayDateDay = displayDateCal.get(Calendar.DATE);
141 int displayDateYear = displayDateCal.get(Calendar.YEAR);
142 int displayDateHour = displayDateCal.get(Calendar.HOUR);
143 int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
144
145 if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
146 displayDateHour += 12;
147 }
148
149 boolean allowPingbacks = entry.isAllowPingbacks();
150 boolean allowTrackbacks = entry.isAllowTrackbacks();
151 String[] trackbacks = StringUtil.split(entry.getTrackbacks());
152
153 String smallImageFileName = null;
154 InputStream smallImageInputStream = null;
155
156 try {
157 if (entry.isSmallImage()) {
158 String smallImagePath = entryElement.attributeValue(
159 "small-image-path");
160
161 if (Validator.isNotNull(entry.getSmallImageURL())) {
162 String smallImageURL =
163 ExportImportHelperUtil.replaceImportContentReferences(
164 portletDataContext, entryElement,
165 entry.getSmallImageURL(), true);
166
167 entry.setSmallImageURL(smallImageURL);
168 }
169 else if (Validator.isNotNull(smallImagePath)) {
170 smallImageFileName = String.valueOf(
171 entry.getSmallImageId()).concat(
172 StringPool.PERIOD).concat(
173 entry.getSmallImageType());
174
175 smallImageInputStream =
176 portletDataContext.getZipEntryAsInputStream(
177 smallImagePath);
178 }
179 }
180
181 ServiceContext serviceContext =
182 portletDataContext.createServiceContext(entry);
183
184 BlogsEntry importedEntry = null;
185
186 if (portletDataContext.isDataStrategyMirror()) {
187 serviceContext.setAttribute("urlTitle", entry.getUrlTitle());
188
189 BlogsEntry existingEntry =
190 BlogsEntryLocalServiceUtil.fetchBlogsEntryByUuidAndGroupId(
191 entry.getUuid(), portletDataContext.getScopeGroupId());
192
193 if (existingEntry == null) {
194 serviceContext.setUuid(entry.getUuid());
195
196 importedEntry = BlogsEntryLocalServiceUtil.addEntry(
197 userId, entry.getTitle(), entry.getDescription(),
198 entry.getContent(), displayDateMonth, displayDateDay,
199 displayDateYear, displayDateHour, displayDateMinute,
200 allowPingbacks, allowTrackbacks, trackbacks,
201 entry.isSmallImage(), entry.getSmallImageURL(),
202 smallImageFileName, smallImageInputStream,
203 serviceContext);
204 }
205 else {
206 importedEntry = BlogsEntryLocalServiceUtil.updateEntry(
207 userId, existingEntry.getEntryId(), entry.getTitle(),
208 entry.getDescription(), entry.getContent(),
209 displayDateMonth, displayDateDay, displayDateYear,
210 displayDateHour, displayDateMinute, allowPingbacks,
211 allowTrackbacks, trackbacks, entry.getSmallImage(),
212 entry.getSmallImageURL(), smallImageFileName,
213 smallImageInputStream, serviceContext);
214 }
215 }
216 else {
217 importedEntry = BlogsEntryLocalServiceUtil.addEntry(
218 userId, entry.getTitle(), entry.getDescription(),
219 entry.getContent(), displayDateMonth, displayDateDay,
220 displayDateYear, displayDateHour, displayDateMinute,
221 allowPingbacks, allowTrackbacks, trackbacks,
222 entry.getSmallImage(), entry.getSmallImageURL(),
223 smallImageFileName, smallImageInputStream, serviceContext);
224 }
225
226 portletDataContext.importClassedModel(entry, importedEntry);
227 }
228 finally {
229 StreamUtil.cleanUp(smallImageInputStream);
230 }
231 }
232
233 @Override
234 protected void doRestoreStagedModel(
235 PortletDataContext portletDataContext, BlogsEntry entry)
236 throws Exception {
237
238 long userId = portletDataContext.getUserId(entry.getUserUuid());
239
240 BlogsEntry existingEntry =
241 BlogsEntryLocalServiceUtil.fetchBlogsEntryByUuidAndGroupId(
242 entry.getUuid(), portletDataContext.getScopeGroupId());
243
244 if ((existingEntry == null) || !existingEntry.isInTrash()) {
245 return;
246 }
247
248 TrashHandler trashHandler = existingEntry.getTrashHandler();
249
250 if (trashHandler.isRestorable(existingEntry.getEntryId())) {
251 trashHandler.restoreTrashEntry(userId, existingEntry.getEntryId());
252 }
253 }
254
255 }