001
014
015 package com.liferay.portlet.dynamicdatamapping.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.lar.StagedModelDataHandlerUtil;
024 import com.liferay.portal.kernel.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.util.FileUtil;
027 import com.liferay.portal.kernel.util.MapUtil;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.kernel.xml.Element;
032 import com.liferay.portal.model.Image;
033 import com.liferay.portal.service.ImageLocalServiceUtil;
034 import com.liferay.portal.service.ServiceContext;
035 import com.liferay.portlet.dynamicdatamapping.TemplateDuplicateTemplateKeyException;
036 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
037 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
038 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
039 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
040
041 import java.io.File;
042
043 import java.util.Map;
044
045
049 public class DDMTemplateStagedModelDataHandler
050 extends BaseStagedModelDataHandler<DDMTemplate> {
051
052 public static final String[] CLASS_NAMES = {DDMTemplate.class.getName()};
053
054 @Override
055 public void deleteStagedModel(
056 String uuid, long groupId, String className, String extraData)
057 throws PortalException, SystemException {
058
059 DDMTemplate ddmTemplate =
060 DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
061 uuid, groupId);
062
063 if (ddmTemplate != null) {
064 DDMTemplateLocalServiceUtil.deleteTemplate(ddmTemplate);
065 }
066 }
067
068 @Override
069 public String[] getClassNames() {
070 return CLASS_NAMES;
071 }
072
073 @Override
074 public String getDisplayName(DDMTemplate template) {
075 return template.getNameCurrentValue();
076 }
077
078 protected DDMTemplate addTemplate(
079 long userId, long groupId, DDMTemplate template, long classPK,
080 File smallFile, ServiceContext serviceContext)
081 throws Exception {
082
083 DDMTemplate newTemplate = null;
084
085 try {
086 return DDMTemplateLocalServiceUtil.addTemplate(
087 userId, groupId, template.getClassNameId(), classPK,
088 template.getTemplateKey(), template.getNameMap(),
089 template.getDescriptionMap(), template.getType(),
090 template.getMode(), template.getLanguage(),
091 template.getScript(), template.isCacheable(),
092 template.isSmallImage(), template.getSmallImageURL(), smallFile,
093 serviceContext);
094 }
095 catch (TemplateDuplicateTemplateKeyException tdtke) {
096 newTemplate = DDMTemplateLocalServiceUtil.addTemplate(
097 userId, groupId, template.getClassNameId(), classPK, null,
098 template.getNameMap(), template.getDescriptionMap(),
099 template.getType(), template.getMode(), template.getLanguage(),
100 template.getScript(), template.isCacheable(),
101 template.isSmallImage(), template.getSmallImageURL(), smallFile,
102 serviceContext);
103
104 if (_log.isWarnEnabled()) {
105 StringBundler sb = new StringBundler(4);
106
107 sb.append("A template with the key ");
108 sb.append(template.getTemplateKey());
109 sb.append(" already exists. The new generated key is ");
110 sb.append(newTemplate.getTemplateKey());
111
112 _log.warn(sb.toString());
113 }
114 }
115
116 return newTemplate;
117 }
118
119 @Override
120 protected void doExportStagedModel(
121 PortletDataContext portletDataContext, DDMTemplate template)
122 throws Exception {
123
124 Element templateElement = portletDataContext.getExportDataElement(
125 template);
126
127 DDMStructure structure = DDMStructureLocalServiceUtil.fetchStructure(
128 template.getClassPK());
129
130 if (structure != null) {
131 StagedModelDataHandlerUtil.exportReferenceStagedModel(
132 portletDataContext, template, structure,
133 PortletDataContext.REFERENCE_TYPE_STRONG);
134 }
135
136 if (template.isSmallImage()) {
137 Image smallImage = ImageLocalServiceUtil.fetchImage(
138 template.getSmallImageId());
139
140 if (Validator.isNotNull(template.getSmallImageURL())) {
141 String smallImageURL =
142 ExportImportHelperUtil.replaceExportContentReferences(
143 portletDataContext, template, templateElement,
144 template.getSmallImageURL().concat(StringPool.SPACE),
145 true);
146
147 template.setSmallImageURL(smallImageURL);
148 }
149 else if (smallImage != null) {
150 String smallImagePath = ExportImportPathUtil.getModelPath(
151 template,
152 smallImage.getImageId() + StringPool.PERIOD +
153 template.getSmallImageType());
154
155 templateElement.addAttribute(
156 "small-image-path", smallImagePath);
157
158 template.setSmallImageType(smallImage.getType());
159
160 portletDataContext.addZipEntry(
161 smallImagePath, smallImage.getTextObj());
162 }
163 }
164
165 if (portletDataContext.getBooleanParameter(
166 DDMPortletDataHandler.NAMESPACE, "referenced-content")) {
167
168 String content =
169 ExportImportHelperUtil.replaceExportContentReferences(
170 portletDataContext, template, templateElement,
171 template.getScript(), true);
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 long classPK = template.getClassPK();
189
190 Element structureElement = portletDataContext.getReferenceDataElement(
191 template, DDMStructure.class, classPK);
192
193 if (structureElement != null) {
194 StagedModelDataHandlerUtil.importStagedModel(
195 portletDataContext, structureElement);
196
197 Map<Long, Long> structureIds =
198 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
199 DDMStructure.class);
200
201 classPK = MapUtil.getLong(structureIds, classPK, classPK);
202 }
203
204 File smallFile = null;
205
206 try {
207 if (template.isSmallImage()) {
208 Element element =
209 portletDataContext.getImportDataStagedModelElement(
210 template);
211
212 String smallImagePath = element.attributeValue(
213 "small-image-path");
214
215 if (Validator.isNotNull(template.getSmallImageURL())) {
216 String smallImageURL =
217 ExportImportHelperUtil.replaceImportContentReferences(
218 portletDataContext, element,
219 template.getSmallImageURL(), true);
220
221 template.setSmallImageURL(smallImageURL);
222 }
223 else if (Validator.isNotNull(smallImagePath)) {
224 byte[] bytes = portletDataContext.getZipEntryAsByteArray(
225 smallImagePath);
226
227 if (bytes != null) {
228 smallFile = FileUtil.createTempFile(
229 template.getSmallImageType());
230
231 FileUtil.write(smallFile, bytes);
232 }
233 }
234 }
235
236 ServiceContext serviceContext =
237 portletDataContext.createServiceContext(
238 template, DDMPortletDataHandler.NAMESPACE);
239
240 DDMTemplate importedTemplate = null;
241
242 if (portletDataContext.isDataStrategyMirror()) {
243 DDMTemplate existingTemplate =
244 DDMTemplateLocalServiceUtil.
245 fetchDDMTemplateByUuidAndGroupId(
246 template.getUuid(),
247 portletDataContext.getScopeGroupId());
248
249 if (existingTemplate == null) {
250 existingTemplate =
251 DDMTemplateLocalServiceUtil.
252 fetchDDMTemplateByUuidAndGroupId(
253 template.getUuid(),
254 portletDataContext.getCompanyGroupId());
255 }
256
257 if (existingTemplate == null) {
258 serviceContext.setUuid(template.getUuid());
259
260 importedTemplate = addTemplate(
261 userId, portletDataContext.getScopeGroupId(), template,
262 classPK, smallFile, serviceContext);
263 }
264 else if (portletDataContext.isCompanyStagedGroupedModel(
265 existingTemplate)) {
266
267 return;
268 }
269 else {
270 importedTemplate =
271 DDMTemplateLocalServiceUtil.updateTemplate(
272 existingTemplate.getTemplateId(),
273 template.getClassPK(), template.getNameMap(),
274 template.getDescriptionMap(), template.getType(),
275 template.getMode(), template.getLanguage(),
276 template.getScript(), template.isCacheable(),
277 template.isSmallImage(),
278 template.getSmallImageURL(), smallFile,
279 serviceContext);
280 }
281 }
282 else {
283 importedTemplate = addTemplate(
284 userId, portletDataContext.getScopeGroupId(), template,
285 classPK, smallFile, serviceContext);
286 }
287
288 portletDataContext.importClassedModel(
289 template, importedTemplate, DDMPortletDataHandler.NAMESPACE);
290
291 Map<String, String> ddmTemplateKeys =
292 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
293 DDMTemplate.class + ".ddmTemplateKey");
294
295 ddmTemplateKeys.put(
296 template.getTemplateKey(), importedTemplate.getTemplateKey());
297 }
298 finally {
299 if (smallFile != null) {
300 smallFile.delete();
301 }
302 }
303 }
304
305 private static Log _log = LogFactoryUtil.getLog(
306 DDMTemplateStagedModelDataHandler.class);
307
308 }