001
014
015 package com.liferay.portlet.dynamicdatamapping.lar;
016
017 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
018 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019 import com.liferay.portal.kernel.lar.PortletDataContext;
020 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.util.FileUtil;
024 import com.liferay.portal.kernel.util.MapUtil;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
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.model.Repository;
031 import com.liferay.portal.model.RepositoryEntry;
032 import com.liferay.portal.service.ServiceContext;
033 import com.liferay.portal.service.persistence.ImageUtil;
034 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
035 import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
036 import com.liferay.portlet.documentlibrary.model.DLFileRank;
037 import com.liferay.portlet.documentlibrary.model.DLFolder;
038 import com.liferay.portlet.dynamicdatamapping.TemplateDuplicateTemplateKeyException;
039 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
040 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
041 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
042 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
043 import com.liferay.portlet.dynamicdatamapping.service.persistence.DDMTemplateUtil;
044 import com.liferay.portlet.journal.lar.JournalPortletDataHandler;
045
046 import java.io.File;
047
048 import java.util.Map;
049
050
054 public class DDMTemplateStagedModelDataHandler
055 extends BaseStagedModelDataHandler<DDMTemplate> {
056
057 public static final String[] CLASS_NAMES = {DDMTemplate.class.getName()};
058
059 @Override
060 public String[] getClassNames() {
061 return CLASS_NAMES;
062 }
063
064 protected DDMTemplate addTemplate(
065 long userId, long groupId, DDMTemplate template, long classPK,
066 File smallFile, ServiceContext serviceContext)
067 throws Exception {
068
069 DDMTemplate newTemplate = null;
070
071 try {
072 return DDMTemplateLocalServiceUtil.addTemplate(
073 userId, groupId, template.getClassNameId(), classPK,
074 template.getTemplateKey(), template.getNameMap(),
075 template.getDescriptionMap(), template.getType(),
076 template.getMode(), template.getLanguage(),
077 template.getScript(), template.isCacheable(),
078 template.isSmallImage(), template.getSmallImageURL(), smallFile,
079 serviceContext);
080 }
081 catch (TemplateDuplicateTemplateKeyException tdtke) {
082 newTemplate = DDMTemplateLocalServiceUtil.addTemplate(
083 userId, groupId, template.getClassNameId(), classPK, null,
084 template.getNameMap(), template.getDescriptionMap(),
085 template.getType(), template.getMode(), template.getLanguage(),
086 template.getScript(), template.isCacheable(),
087 template.isSmallImage(), template.getSmallImageURL(), smallFile,
088 serviceContext);
089
090 if (_log.isWarnEnabled()) {
091 StringBundler sb = new StringBundler(4);
092
093 sb.append("A template with the key ");
094 sb.append(template.getTemplateKey());
095 sb.append(" already exists. The new generated key is ");
096 sb.append(newTemplate.getTemplateKey());
097
098 _log.warn(sb.toString());
099 }
100 }
101
102 return newTemplate;
103 }
104
105 @Override
106 protected void doExportStagedModel(
107 PortletDataContext portletDataContext, DDMTemplate template)
108 throws Exception {
109
110 DDMStructure structure = DDMStructureLocalServiceUtil.fetchStructure(
111 template.getClassPK());
112
113 if (structure != null) {
114 StagedModelDataHandlerUtil.exportStagedModel(
115 portletDataContext, structure);
116 }
117
118 Element dlFileEntryTypesElement =
119 portletDataContext.getExportDataGroupElement(DLFileEntryType.class);
120 Element dlFoldersElement = portletDataContext.getExportDataGroupElement(
121 DLFolder.class);
122 Element dlFileEntriesElement =
123 portletDataContext.getExportDataGroupElement(DLFileEntry.class);
124 Element dlFileRanksElement =
125 portletDataContext.getExportDataGroupElement(DLFileRank.class);
126 Element dlRepositoriesElement =
127 portletDataContext.getExportDataGroupElement(Repository.class);
128 Element dlRepositoryEntriesElement =
129 portletDataContext.getExportDataGroupElement(RepositoryEntry.class);
130
131 Element templateElement =
132 portletDataContext.getExportDataStagedModelElement(template);
133
134 if (template.isSmallImage()) {
135 Image smallImage = ImageUtil.fetchByPrimaryKey(
136 template.getSmallImageId());
137
138 if (Validator.isNotNull(template.getSmallImageURL())) {
139 String smallImageURL =
140 DDMPortletDataHandler.exportReferencedContent(
141 portletDataContext, dlFileEntryTypesElement,
142 dlFoldersElement, dlFileEntriesElement,
143 dlFileRanksElement, dlRepositoriesElement,
144 dlRepositoryEntriesElement, templateElement,
145 template.getSmallImageURL().concat(StringPool.SPACE));
146
147 template.setSmallImageURL(smallImageURL);
148 }
149 else if (smallImage != null) {
150 String smallImagePath = ExportImportPathUtil.getModelPath(
151 template, smallImage.getImageId() + StringPool.PERIOD +
152 template.getSmallImageType());
153
154 templateElement.addAttribute(
155 "small-image-path", smallImagePath);
156
157 template.setSmallImageType(smallImage.getType());
158
159 portletDataContext.addZipEntry(
160 smallImagePath, smallImage.getTextObj());
161 }
162 }
163
164 if (portletDataContext.getBooleanParameter(
165 DDMPortletDataHandler.NAMESPACE, "embedded-assets")) {
166
167 String content = DDMPortletDataHandler.exportReferencedContent(
168 portletDataContext, dlFileEntryTypesElement, dlFoldersElement,
169 dlFileEntriesElement, dlFileRanksElement, dlRepositoriesElement,
170 dlRepositoryEntriesElement, templateElement,
171 template.getScript());
172
173 template.setScript(content);
174 }
175
176 portletDataContext.addClassedModel(
177 templateElement, ExportImportPathUtil.getModelPath(template),
178 template, DDMPortletDataHandler.NAMESPACE);
179 }
180
181 @Override
182 protected void doImportStagedModel(
183 PortletDataContext portletDataContext, DDMTemplate template)
184 throws Exception {
185
186 long userId = portletDataContext.getUserId(template.getUserUuid());
187
188 Map<Long, Long> structureIds =
189 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
190 DDMStructure.class);
191
192 String structurePath = ExportImportPathUtil.getModelPath(
193 portletDataContext, DDMStructure.class.getName(),
194 template.getClassPK());
195
196 DDMStructure structure =
197 (DDMStructure)portletDataContext.getZipEntryAsObject(structurePath);
198
199 StagedModelDataHandlerUtil.importStagedModel(
200 portletDataContext, structure);
201
202 long classPK = MapUtil.getLong(
203 structureIds, template.getClassPK(), template.getClassPK());
204
205 File smallFile = null;
206
207 if (template.isSmallImage()) {
208 Element element =
209 portletDataContext.getImportDataStagedModelElement(template);
210
211 String smallImagePath = element.attributeValue("small-image-path");
212
213 if (Validator.isNotNull(template.getSmallImageURL())) {
214 String smallImageURL =
215 JournalPortletDataHandler.importReferencedContent(
216 portletDataContext, element,
217 template.getSmallImageURL());
218
219 template.setSmallImageURL(smallImageURL);
220 }
221 else if (Validator.isNotNull(smallImagePath)) {
222 byte[] bytes = portletDataContext.getZipEntryAsByteArray(
223 smallImagePath);
224
225 if (bytes != null) {
226 smallFile = FileUtil.createTempFile(
227 template.getSmallImageType());
228
229 FileUtil.write(smallFile, bytes);
230 }
231 }
232 }
233
234 ServiceContext serviceContext = portletDataContext.createServiceContext(
235 template, DDMPortletDataHandler.NAMESPACE);
236
237 DDMTemplate importedTemplate = null;
238
239 if (portletDataContext.isDataStrategyMirror()) {
240 DDMTemplate existingTemplate = DDMTemplateUtil.fetchByUUID_G(
241 template.getUuid(), portletDataContext.getScopeGroupId());
242
243 if (existingTemplate == null) {
244 serviceContext.setUuid(template.getUuid());
245
246 importedTemplate = addTemplate(
247 userId, portletDataContext.getScopeGroupId(), template,
248 classPK, smallFile, serviceContext);
249 }
250 else {
251 importedTemplate = DDMTemplateLocalServiceUtil.updateTemplate(
252 existingTemplate.getTemplateId(), template.getNameMap(),
253 template.getDescriptionMap(), template.getType(),
254 template.getMode(), template.getLanguage(),
255 template.getScript(), template.isCacheable(),
256 template.isSmallImage(), template.getSmallImageURL(),
257 smallFile, serviceContext);
258 }
259 }
260 else {
261 importedTemplate = addTemplate(
262 userId, portletDataContext.getScopeGroupId(), template, classPK,
263 smallFile, serviceContext);
264 }
265
266 portletDataContext.importClassedModel(
267 template, importedTemplate, DDMPortletDataHandler.NAMESPACE);
268 }
269
270 private static Log _log = LogFactoryUtil.getLog(
271 DDMTemplateStagedModelDataHandler.class);
272
273 }