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.util.CalendarFactoryUtil;
024 import com.liferay.portal.kernel.util.StreamUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.kernel.xml.Element;
029 import com.liferay.portal.model.Image;
030 import com.liferay.portal.service.ImageLocalServiceUtil;
031 import com.liferay.portal.service.ServiceContext;
032 import com.liferay.portlet.blogs.model.BlogsEntry;
033 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
034
035 import java.io.InputStream;
036
037 import java.util.Calendar;
038
039
042 public class BlogsEntryStagedModelDataHandler
043 extends BaseStagedModelDataHandler<BlogsEntry> {
044
045 public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
046
047 @Override
048 public void deleteStagedModel(
049 String uuid, long groupId, String className, String extraData)
050 throws PortalException, SystemException {
051
052 BlogsEntry entry =
053 BlogsEntryLocalServiceUtil.fetchBlogsEntryByUuidAndGroupId(
054 uuid, groupId);
055
056 if (entry != null) {
057 BlogsEntryLocalServiceUtil.deleteEntry(entry);
058 }
059 }
060
061 @Override
062 public String[] getClassNames() {
063 return CLASS_NAMES;
064 }
065
066 @Override
067 public String getDisplayName(BlogsEntry entry) {
068 return entry.getTitle();
069 }
070
071 @Override
072 protected void doExportStagedModel(
073 PortletDataContext portletDataContext, BlogsEntry entry)
074 throws Exception {
075
076 Element entryElement = portletDataContext.getExportDataElement(entry);
077
078 if (entry.isSmallImage()) {
079 Image smallImage = ImageLocalServiceUtil.fetchImage(
080 entry.getSmallImageId());
081
082 if (Validator.isNotNull(entry.getSmallImageURL())) {
083 String smallImageURL =
084 ExportImportHelperUtil.replaceExportContentReferences(
085 portletDataContext, entry, entryElement,
086 entry.getSmallImageURL().concat(StringPool.SPACE),
087 true);
088
089 entry.setSmallImageURL(smallImageURL);
090 }
091 else if (smallImage != null) {
092 String smallImagePath = ExportImportPathUtil.getModelPath(
093 entry,
094 smallImage.getImageId() + StringPool.PERIOD +
095 smallImage.getType());
096
097 entryElement.addAttribute("small-image-path", smallImagePath);
098
099 entry.setSmallImageType(smallImage.getType());
100
101 portletDataContext.addZipEntry(
102 smallImagePath, smallImage.getTextObj());
103 }
104 }
105
106 String content = ExportImportHelperUtil.replaceExportContentReferences(
107 portletDataContext, entry, entryElement, entry.getContent(),
108 portletDataContext.getBooleanParameter(
109 BlogsPortletDataHandler.NAMESPACE, "referenced-content"));
110
111 entry.setContent(content);
112
113 portletDataContext.addClassedModel(
114 entryElement, ExportImportPathUtil.getModelPath(entry), entry,
115 BlogsPortletDataHandler.NAMESPACE);
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(
183 entry, BlogsPortletDataHandler.NAMESPACE);
184
185 BlogsEntry importedEntry = null;
186
187 if (portletDataContext.isDataStrategyMirror()) {
188 serviceContext.setAttribute("urlTitle", entry.getUrlTitle());
189
190 BlogsEntry existingEntry =
191 BlogsEntryLocalServiceUtil.fetchBlogsEntryByUuidAndGroupId(
192 entry.getUuid(), portletDataContext.getScopeGroupId());
193
194 if (existingEntry == null) {
195 serviceContext.setUuid(entry.getUuid());
196
197 importedEntry = BlogsEntryLocalServiceUtil.addEntry(
198 userId, entry.getTitle(), entry.getDescription(),
199 entry.getContent(), displayDateMonth, displayDateDay,
200 displayDateYear, displayDateHour, displayDateMinute,
201 allowPingbacks, allowTrackbacks, trackbacks,
202 entry.isSmallImage(), entry.getSmallImageURL(),
203 smallImageFileName, smallImageInputStream,
204 serviceContext);
205 }
206 else {
207 importedEntry = BlogsEntryLocalServiceUtil.updateEntry(
208 userId, existingEntry.getEntryId(), entry.getTitle(),
209 entry.getDescription(), entry.getContent(),
210 displayDateMonth, displayDateDay, displayDateYear,
211 displayDateHour, displayDateMinute, allowPingbacks,
212 allowTrackbacks, trackbacks, entry.getSmallImage(),
213 entry.getSmallImageURL(), smallImageFileName,
214 smallImageInputStream, serviceContext);
215 }
216 }
217 else {
218 importedEntry = BlogsEntryLocalServiceUtil.addEntry(
219 userId, entry.getTitle(), entry.getDescription(),
220 entry.getContent(), displayDateMonth, displayDateDay,
221 displayDateYear, displayDateHour, displayDateMinute,
222 allowPingbacks, allowTrackbacks, trackbacks,
223 entry.getSmallImage(), entry.getSmallImageURL(),
224 smallImageFileName, smallImageInputStream, serviceContext);
225 }
226
227 portletDataContext.importClassedModel(
228 entry, importedEntry, BlogsPortletDataHandler.NAMESPACE);
229 }
230 finally {
231 StreamUtil.cleanUp(smallImageInputStream);
232 }
233 }
234
235 }