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);
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);
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 }
171
172 protected void exportLayoutPrototypes(
173 PortletDataContext portletDataContext,
174 LayoutSetPrototype layoutSetPrototype,
175 Element layoutSetPrototypeElement)
176 throws Exception {
177
178 DynamicQuery dynamicQuery = LayoutLocalServiceUtil.dynamicQuery();
179
180 Property groupIdProperty = PropertyFactoryUtil.forName("groupId");
181
182 dynamicQuery.add(groupIdProperty.eq(layoutSetPrototype.getGroupId()));
183
184 Conjunction conjunction = RestrictionsFactoryUtil.conjunction();
185
186 Property layoutPrototypeUuidProperty = PropertyFactoryUtil.forName(
187 "layoutPrototypeUuid");
188
189 conjunction.add(layoutPrototypeUuidProperty.isNotNull());
190 conjunction.add(layoutPrototypeUuidProperty.ne(StringPool.BLANK));
191
192 dynamicQuery.add(conjunction);
193
194 List<Layout> layouts = LayoutLocalServiceUtil.dynamicQuery(
195 dynamicQuery);
196
197 boolean exportLayoutPrototypes = portletDataContext.getBooleanParameter(
198 LayoutSetPrototypePortletDataHandler.NAMESPACE, "page-templates");
199
200 for (Layout layout : layouts) {
201 String layoutPrototypeUuid = layout.getLayoutPrototypeUuid();
202
203 LayoutPrototype layoutPrototype =
204 LayoutPrototypeLocalServiceUtil.
205 getLayoutPrototypeByUuidAndCompanyId(
206 layoutPrototypeUuid, portletDataContext.getCompanyId());
207
208 portletDataContext.addReferenceElement(
209 layout, layoutSetPrototypeElement, layoutPrototype,
210 PortletDataContext.REFERENCE_TYPE_DEPENDENCY,
211 !exportLayoutPrototypes);
212
213 if (exportLayoutPrototypes) {
214 StagedModelDataHandlerUtil.exportStagedModel(
215 portletDataContext, layoutPrototype);
216 }
217 }
218 }
219
220 protected void exportLayouts(
221 LayoutSetPrototype layoutSetPrototype,
222 PortletDataContext portletDataContext)
223 throws Exception {
224
225 File file = null;
226 InputStream inputStream = null;
227
228 try {
229 file = SitesUtil.exportLayoutSetPrototype(
230 layoutSetPrototype, new ServiceContext());
231
232 inputStream = new FileInputStream(file);
233
234 String layoutSetPrototypeLARPath =
235 ExportImportPathUtil.getModelPath(
236 layoutSetPrototype,
237 getLayoutSetPrototypeLARFileName(layoutSetPrototype));
238
239 if (portletDataContext.isPathNotProcessed(
240 layoutSetPrototypeLARPath)) {
241
242 portletDataContext.addZipEntry(
243 layoutSetPrototypeLARPath, inputStream);
244 }
245
246 List<Layout> layoutSetPrototypeLayouts =
247 LayoutLocalServiceUtil.getLayouts(
248 layoutSetPrototype.getGroupId(), true);
249
250 Element layoutSetPrototypeElement =
251 portletDataContext.getExportDataElement(layoutSetPrototype);
252
253 for (Layout layoutSetPrototypeLayout : layoutSetPrototypeLayouts) {
254 portletDataContext.addReferenceElement(
255 layoutSetPrototype, layoutSetPrototypeElement,
256 layoutSetPrototypeLayout,
257 PortletDataContext.REFERENCE_TYPE_EMBEDDED, false);
258 }
259 }
260 finally {
261 StreamUtil.cleanUp(inputStream);
262
263 if (file != null) {
264 file.delete();
265 }
266 }
267 }
268
269 protected String getLayoutSetPrototypeLARFileName(
270 LayoutSetPrototype layoutSetPrototype) {
271
272 return layoutSetPrototype.getLayoutSetPrototypeId() + ".lar";
273 }
274
275 protected void importLayoutPrototypes(
276 PortletDataContext portletDataContext,
277 LayoutSetPrototype layoutSetPrototype)
278 throws PortletDataException {
279
280 List<Element> layoutPrototypeElements =
281 portletDataContext.getReferenceDataElements(
282 layoutSetPrototype, LayoutPrototype.class);
283
284 for (Element layoutPrototypeElement : layoutPrototypeElements) {
285 StagedModelDataHandlerUtil.importStagedModel(
286 portletDataContext, layoutPrototypeElement);
287 }
288 }
289
290 protected void importLayouts(
291 PortletDataContext portletDataContext,
292 LayoutSetPrototype layoutSetPrototype,
293 LayoutSetPrototype importedLayoutSetPrototype,
294 ServiceContext serviceContext)
295 throws PortalException, SystemException {
296
297 InputStream inputStream = null;
298
299 try {
300 String layoutSetPrototypeLARPath =
301 ExportImportPathUtil.getModelPath(
302 layoutSetPrototype,
303 getLayoutSetPrototypeLARFileName(layoutSetPrototype));
304
305 inputStream = portletDataContext.getZipEntryAsInputStream(
306 layoutSetPrototypeLARPath);
307
308 SitesUtil.importLayoutSetPrototype(
309 importedLayoutSetPrototype, inputStream, serviceContext);
310 }
311 finally {
312 StreamUtil.cleanUp(inputStream);
313 }
314 }
315
316 @Override
317 protected boolean validateMissingReference(
318 String uuid, long companyId, long groupId)
319 throws Exception {
320
321 LayoutSetPrototype layoutSetPrototype =
322 LayoutSetPrototypeLocalServiceUtil.
323 fetchLayoutSetPrototypeByUuidAndCompanyId(uuid, companyId);
324
325 if (layoutSetPrototype == null) {
326 return false;
327 }
328
329 return true;
330 }
331
332 }