001
014
015 package com.liferay.portlet.layoutsetprototypes.lar;
016
017 import com.liferay.portal.kernel.dao.orm.Conjunction;
018 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019 import com.liferay.portal.kernel.dao.orm.Property;
020 import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021 import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
022 import com.liferay.portal.kernel.exception.PortalException;
023 import com.liferay.portal.kernel.exception.SystemException;
024 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
025 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
026 import com.liferay.portal.kernel.lar.PortletDataContext;
027 import com.liferay.portal.kernel.lar.PortletDataException;
028 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
029 import com.liferay.portal.kernel.util.GetterUtil;
030 import com.liferay.portal.kernel.util.StreamUtil;
031 import com.liferay.portal.kernel.util.StringPool;
032 import com.liferay.portal.kernel.util.UnicodeProperties;
033 import com.liferay.portal.kernel.xml.Element;
034 import com.liferay.portal.model.Group;
035 import com.liferay.portal.model.Layout;
036 import com.liferay.portal.model.LayoutPrototype;
037 import com.liferay.portal.model.LayoutSetPrototype;
038 import com.liferay.portal.service.GroupLocalServiceUtil;
039 import com.liferay.portal.service.LayoutLocalServiceUtil;
040 import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
041 import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
042 import com.liferay.portal.service.ServiceContext;
043 import com.liferay.portlet.sites.util.SitesUtil;
044
045 import java.io.File;
046 import java.io.FileInputStream;
047 import java.io.InputStream;
048
049 import java.util.List;
050
051
054 public class LayoutSetPrototypeStagedModelDataHandler
055 extends BaseStagedModelDataHandler<LayoutSetPrototype> {
056
057 public static final String[] CLASS_NAMES =
058 {LayoutSetPrototype.class.getName()};
059
060 @Override
061 public void deleteStagedModel(
062 String uuid, long groupId, String className, String extraData)
063 throws PortalException, SystemException {
064
065 Group group = GroupLocalServiceUtil.getGroup(groupId);
066
067 LayoutSetPrototype layoutSetPrototype =
068 LayoutSetPrototypeLocalServiceUtil.
069 fetchLayoutSetPrototypeByUuidAndCompanyId(
070 uuid, group.getCompanyId());
071
072 if (layoutSetPrototype != null) {
073 LayoutSetPrototypeLocalServiceUtil.deleteLayoutSetPrototype(
074 layoutSetPrototype);
075 }
076 }
077
078 @Override
079 public String[] getClassNames() {
080 return CLASS_NAMES;
081 }
082
083 @Override
084 protected void doExportStagedModel(
085 PortletDataContext portletDataContext,
086 LayoutSetPrototype layoutSetPrototype)
087 throws Exception {
088
089 Element layoutSetPrototypeElement =
090 portletDataContext.getExportDataElement(layoutSetPrototype);
091
092 portletDataContext.addClassedModel(
093 layoutSetPrototypeElement,
094 ExportImportPathUtil.getModelPath(layoutSetPrototype),
095 layoutSetPrototype, LayoutSetPrototypePortletDataHandler.NAMESPACE);
096
097 exportLayouts(layoutSetPrototype, portletDataContext);
098
099 exportLayoutPrototypes(
100 portletDataContext, layoutSetPrototype, layoutSetPrototypeElement);
101 }
102
103 @Override
104 protected void doImportStagedModel(
105 PortletDataContext portletDataContext,
106 LayoutSetPrototype layoutSetPrototype)
107 throws Exception {
108
109 long userId = portletDataContext.getUserId(
110 layoutSetPrototype.getUserUuid());
111
112 UnicodeProperties settingsProperties =
113 layoutSetPrototype.getSettingsProperties();
114
115 boolean layoutsUpdateable = GetterUtil.getBoolean(
116 settingsProperties.getProperty("layoutsUpdateable"), true);
117
118 ServiceContext serviceContext = portletDataContext.createServiceContext(
119 layoutSetPrototype, LayoutSetPrototypePortletDataHandler.NAMESPACE);
120
121 serviceContext.setAttribute("addDefaultLayout", false);
122
123 LayoutSetPrototype importedLayoutSetPrototype = null;
124
125 if (portletDataContext.isDataStrategyMirror()) {
126 LayoutSetPrototype existingLayoutSetPrototype =
127 LayoutSetPrototypeLocalServiceUtil.
128 fetchLayoutSetPrototypeByUuidAndCompanyId(
129 layoutSetPrototype.getUuid(),
130 portletDataContext.getCompanyId());
131
132 if (existingLayoutSetPrototype == null) {
133 serviceContext.setUuid(layoutSetPrototype.getUuid());
134
135 importedLayoutSetPrototype =
136 LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
137 userId, portletDataContext.getCompanyId(),
138 layoutSetPrototype.getNameMap(),
139 layoutSetPrototype.getDescription(),
140 layoutSetPrototype.isActive(), layoutsUpdateable,
141 serviceContext);
142 }
143 else {
144 importedLayoutSetPrototype =
145 LayoutSetPrototypeLocalServiceUtil.updateLayoutSetPrototype(
146 existingLayoutSetPrototype.getLayoutSetPrototypeId(),
147 layoutSetPrototype.getNameMap(),
148 layoutSetPrototype.getDescription(),
149 layoutSetPrototype.isActive(), layoutsUpdateable,
150 serviceContext);
151 }
152 }
153 else {
154 importedLayoutSetPrototype =
155 LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
156 userId, portletDataContext.getCompanyId(),
157 layoutSetPrototype.getNameMap(),
158 layoutSetPrototype.getDescription(),
159 layoutSetPrototype.isActive(), layoutsUpdateable,
160 serviceContext);
161 }
162
163 importLayoutPrototypes(portletDataContext, layoutSetPrototype);
164 importLayouts(
165 portletDataContext, layoutSetPrototype, importedLayoutSetPrototype,
166 serviceContext);
167
168 portletDataContext.importClassedModel(
169 layoutSetPrototype, importedLayoutSetPrototype,
170 LayoutSetPrototypePortletDataHandler.NAMESPACE);
171 }
172
173 protected void exportLayoutPrototypes(
174 PortletDataContext portletDataContext,
175 LayoutSetPrototype layoutSetPrototype,
176 Element layoutSetPrototypeElement)
177 throws Exception {
178
179 DynamicQuery dynamicQuery = LayoutLocalServiceUtil.dynamicQuery();
180
181 Property groupIdProperty = PropertyFactoryUtil.forName("groupId");
182
183 dynamicQuery.add(groupIdProperty.eq(layoutSetPrototype.getGroupId()));
184
185 Conjunction conjunction = RestrictionsFactoryUtil.conjunction();
186
187 Property layoutPrototypeUuidProperty = PropertyFactoryUtil.forName(
188 "layoutPrototypeUuid");
189
190 conjunction.add(layoutPrototypeUuidProperty.isNotNull());
191 conjunction.add(layoutPrototypeUuidProperty.ne(StringPool.BLANK));
192
193 dynamicQuery.add(conjunction);
194
195 List<Layout> layouts = LayoutLocalServiceUtil.dynamicQuery(
196 dynamicQuery);
197
198 boolean exportLayoutPrototypes = portletDataContext.getBooleanParameter(
199 LayoutSetPrototypePortletDataHandler.NAMESPACE, "page-templates");
200
201 for (Layout layout : layouts) {
202 String layoutPrototypeUuid = layout.getLayoutPrototypeUuid();
203
204 LayoutPrototype layoutPrototype =
205 LayoutPrototypeLocalServiceUtil.
206 getLayoutPrototypeByUuidAndCompanyId(
207 layoutPrototypeUuid, portletDataContext.getCompanyId());
208
209 portletDataContext.addReferenceElement(
210 layout, layoutSetPrototypeElement, layoutPrototype,
211 PortletDataContext.REFERENCE_TYPE_DEPENDENCY,
212 !exportLayoutPrototypes);
213
214 if (exportLayoutPrototypes) {
215 StagedModelDataHandlerUtil.exportStagedModel(
216 portletDataContext, layoutPrototype);
217 }
218 }
219 }
220
221 protected void exportLayouts(
222 LayoutSetPrototype layoutSetPrototype,
223 PortletDataContext portletDataContext)
224 throws Exception {
225
226 File file = null;
227 InputStream inputStream = null;
228
229 try {
230 file = SitesUtil.exportLayoutSetPrototype(
231 layoutSetPrototype, new ServiceContext());
232
233 inputStream = new FileInputStream(file);
234
235 String layoutSetPrototypeLARPath =
236 ExportImportPathUtil.getModelPath(
237 layoutSetPrototype,
238 getLayoutSetPrototypeLARFileName(layoutSetPrototype));
239
240 portletDataContext.addZipEntry(
241 layoutSetPrototypeLARPath, inputStream);
242
243 List<Layout> layoutSetPrototypeLayouts =
244 LayoutLocalServiceUtil.getLayouts(
245 layoutSetPrototype.getGroupId(), true);
246
247 Element layoutSetPrototypeElement =
248 portletDataContext.getExportDataElement(layoutSetPrototype);
249
250 for (Layout layoutSetPrototypeLayout : layoutSetPrototypeLayouts) {
251 portletDataContext.addReferenceElement(
252 layoutSetPrototype, layoutSetPrototypeElement,
253 layoutSetPrototypeLayout,
254 PortletDataContext.REFERENCE_TYPE_EMBEDDED, false);
255 }
256 }
257 finally {
258 StreamUtil.cleanUp(inputStream);
259
260 if (file != null) {
261 file.delete();
262 }
263 }
264 }
265
266 protected String getLayoutSetPrototypeLARFileName(
267 LayoutSetPrototype layoutSetPrototype) {
268
269 return layoutSetPrototype.getLayoutSetPrototypeId() + ".lar";
270 }
271
272 protected void importLayoutPrototypes(
273 PortletDataContext portletDataContext,
274 LayoutSetPrototype layoutSetPrototype)
275 throws PortletDataException {
276
277 List<Element> layoutPrototypeElements =
278 portletDataContext.getReferenceDataElements(
279 layoutSetPrototype, LayoutPrototype.class);
280
281 for (Element layoutPrototypeElement : layoutPrototypeElements) {
282 StagedModelDataHandlerUtil.importStagedModel(
283 portletDataContext, layoutPrototypeElement);
284 }
285 }
286
287 protected void importLayouts(
288 PortletDataContext portletDataContext,
289 LayoutSetPrototype layoutSetPrototype,
290 LayoutSetPrototype importedLayoutSetPrototype,
291 ServiceContext serviceContext)
292 throws PortalException, SystemException {
293
294 InputStream inputStream = null;
295
296 try {
297 String layoutSetPrototypeLARPath =
298 ExportImportPathUtil.getModelPath(
299 layoutSetPrototype,
300 getLayoutSetPrototypeLARFileName(layoutSetPrototype));
301
302 inputStream = portletDataContext.getZipEntryAsInputStream(
303 layoutSetPrototypeLARPath);
304
305 SitesUtil.importLayoutSetPrototype(
306 importedLayoutSetPrototype, inputStream, serviceContext);
307 }
308 finally {
309 StreamUtil.cleanUp(inputStream);
310 }
311 }
312
313 @Override
314 protected boolean validateMissingReference(
315 String uuid, long companyId, long groupId)
316 throws Exception {
317
318 LayoutSetPrototype layoutSetPrototype =
319 LayoutSetPrototypeLocalServiceUtil.
320 fetchLayoutSetPrototypeByUuidAndCompanyId(uuid, companyId);
321
322 if (layoutSetPrototype == null) {
323 return false;
324 }
325
326 return true;
327 }
328
329 }