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.lar.BaseStagedModelDataHandler;
024 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
025 import com.liferay.portal.kernel.lar.PortletDataContext;
026 import com.liferay.portal.kernel.lar.PortletDataException;
027 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
028 import com.liferay.portal.kernel.util.GetterUtil;
029 import com.liferay.portal.kernel.util.StreamUtil;
030 import com.liferay.portal.kernel.util.StringPool;
031 import com.liferay.portal.kernel.util.UnicodeProperties;
032 import com.liferay.portal.kernel.xml.Element;
033 import com.liferay.portal.model.Group;
034 import com.liferay.portal.model.Layout;
035 import com.liferay.portal.model.LayoutPrototype;
036 import com.liferay.portal.model.LayoutSetPrototype;
037 import com.liferay.portal.service.GroupLocalServiceUtil;
038 import com.liferay.portal.service.LayoutLocalServiceUtil;
039 import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
040 import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
041 import com.liferay.portal.service.ServiceContext;
042 import com.liferay.portlet.sites.util.SitesUtil;
043
044 import java.io.File;
045 import java.io.FileInputStream;
046 import java.io.InputStream;
047
048 import java.util.List;
049
050
053 public class LayoutSetPrototypeStagedModelDataHandler
054 extends BaseStagedModelDataHandler<LayoutSetPrototype> {
055
056 public static final String[] CLASS_NAMES =
057 {LayoutSetPrototype.class.getName()};
058
059 @Override
060 public void deleteStagedModel(
061 String uuid, long groupId, String className, String extraData)
062 throws PortalException {
063
064 Group group = GroupLocalServiceUtil.getGroup(groupId);
065
066 LayoutSetPrototype layoutSetPrototype =
067 fetchStagedModelByUuidAndGroupId(uuid, group.getCompanyId());
068
069 if (layoutSetPrototype != null) {
070 LayoutSetPrototypeLocalServiceUtil.deleteLayoutSetPrototype(
071 layoutSetPrototype);
072 }
073 }
074
075 @Override
076 public LayoutSetPrototype fetchStagedModelByUuidAndCompanyId(
077 String uuid, long companyId) {
078
079 return LayoutSetPrototypeLocalServiceUtil.
080 fetchLayoutSetPrototypeByUuidAndCompanyId(uuid, companyId);
081 }
082
083 @Override
084 public String[] getClassNames() {
085 return CLASS_NAMES;
086 }
087
088 @Override
089 protected void doExportStagedModel(
090 PortletDataContext portletDataContext,
091 LayoutSetPrototype layoutSetPrototype)
092 throws Exception {
093
094 Element layoutSetPrototypeElement =
095 portletDataContext.getExportDataElement(layoutSetPrototype);
096
097 portletDataContext.addClassedModel(
098 layoutSetPrototypeElement,
099 ExportImportPathUtil.getModelPath(layoutSetPrototype),
100 layoutSetPrototype);
101
102 exportLayouts(layoutSetPrototype, portletDataContext);
103
104 exportLayoutPrototypes(
105 portletDataContext, layoutSetPrototype, layoutSetPrototypeElement);
106 }
107
108 @Override
109 protected void doImportStagedModel(
110 PortletDataContext portletDataContext,
111 LayoutSetPrototype layoutSetPrototype)
112 throws Exception {
113
114 long userId = portletDataContext.getUserId(
115 layoutSetPrototype.getUserUuid());
116
117 UnicodeProperties settingsProperties =
118 layoutSetPrototype.getSettingsProperties();
119
120 boolean layoutsUpdateable = GetterUtil.getBoolean(
121 settingsProperties.getProperty("layoutsUpdateable"), true);
122
123 ServiceContext serviceContext = portletDataContext.createServiceContext(
124 layoutSetPrototype);
125
126 serviceContext.setAttribute("addDefaultLayout", false);
127
128 LayoutSetPrototype importedLayoutSetPrototype = null;
129
130 if (portletDataContext.isDataStrategyMirror()) {
131 LayoutSetPrototype existingLayoutSetPrototype =
132 fetchStagedModelByUuidAndCompanyId(
133 layoutSetPrototype.getUuid(),
134 portletDataContext.getCompanyId());
135
136 if (existingLayoutSetPrototype == null) {
137 serviceContext.setUuid(layoutSetPrototype.getUuid());
138
139 importedLayoutSetPrototype =
140 LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
141 userId, portletDataContext.getCompanyId(),
142 layoutSetPrototype.getNameMap(),
143 layoutSetPrototype.getDescriptionMap(),
144 layoutSetPrototype.isActive(), layoutsUpdateable,
145 serviceContext);
146 }
147 else {
148 importedLayoutSetPrototype =
149 LayoutSetPrototypeLocalServiceUtil.updateLayoutSetPrototype(
150 existingLayoutSetPrototype.getLayoutSetPrototypeId(),
151 layoutSetPrototype.getNameMap(),
152 layoutSetPrototype.getDescriptionMap(),
153 layoutSetPrototype.isActive(), layoutsUpdateable,
154 serviceContext);
155 }
156 }
157 else {
158 importedLayoutSetPrototype =
159 LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
160 userId, portletDataContext.getCompanyId(),
161 layoutSetPrototype.getNameMap(),
162 layoutSetPrototype.getDescriptionMap(),
163 layoutSetPrototype.isActive(), layoutsUpdateable,
164 serviceContext);
165 }
166
167 importLayoutPrototypes(portletDataContext, layoutSetPrototype);
168 importLayouts(
169 portletDataContext, layoutSetPrototype, importedLayoutSetPrototype,
170 serviceContext);
171
172 portletDataContext.importClassedModel(
173 layoutSetPrototype, importedLayoutSetPrototype);
174 }
175
176 protected void exportLayoutPrototypes(
177 PortletDataContext portletDataContext,
178 LayoutSetPrototype layoutSetPrototype,
179 Element layoutSetPrototypeElement)
180 throws Exception {
181
182 DynamicQuery dynamicQuery = LayoutLocalServiceUtil.dynamicQuery();
183
184 Property groupIdProperty = PropertyFactoryUtil.forName("groupId");
185
186 dynamicQuery.add(groupIdProperty.eq(layoutSetPrototype.getGroupId()));
187
188 Conjunction conjunction = RestrictionsFactoryUtil.conjunction();
189
190 Property layoutPrototypeUuidProperty = PropertyFactoryUtil.forName(
191 "layoutPrototypeUuid");
192
193 conjunction.add(layoutPrototypeUuidProperty.isNotNull());
194 conjunction.add(layoutPrototypeUuidProperty.ne(StringPool.BLANK));
195
196 dynamicQuery.add(conjunction);
197
198 List<Layout> layouts = LayoutLocalServiceUtil.dynamicQuery(
199 dynamicQuery);
200
201 boolean exportLayoutPrototypes = portletDataContext.getBooleanParameter(
202 LayoutSetPrototypePortletDataHandler.NAMESPACE, "page-templates");
203
204 for (Layout layout : layouts) {
205 String layoutPrototypeUuid = layout.getLayoutPrototypeUuid();
206
207 LayoutPrototype layoutPrototype =
208 LayoutPrototypeLocalServiceUtil.
209 getLayoutPrototypeByUuidAndCompanyId(
210 layoutPrototypeUuid, portletDataContext.getCompanyId());
211
212 portletDataContext.addReferenceElement(
213 layout, layoutSetPrototypeElement, layoutPrototype,
214 PortletDataContext.REFERENCE_TYPE_DEPENDENCY,
215 !exportLayoutPrototypes);
216
217 if (exportLayoutPrototypes) {
218 StagedModelDataHandlerUtil.exportStagedModel(
219 portletDataContext, layoutPrototype);
220 }
221 }
222 }
223
224 protected void exportLayouts(
225 LayoutSetPrototype layoutSetPrototype,
226 PortletDataContext portletDataContext)
227 throws Exception {
228
229 File file = null;
230 InputStream inputStream = null;
231
232 try {
233 file = SitesUtil.exportLayoutSetPrototype(
234 layoutSetPrototype, new ServiceContext());
235
236 inputStream = new FileInputStream(file);
237
238 String layoutSetPrototypeLARPath =
239 ExportImportPathUtil.getModelPath(
240 layoutSetPrototype,
241 getLayoutSetPrototypeLARFileName(layoutSetPrototype));
242
243 portletDataContext.addZipEntry(
244 layoutSetPrototypeLARPath, inputStream);
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 {
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 void importReferenceStagedModels(
318 PortletDataContext portletDataContext,
319 LayoutSetPrototype layoutSetPrototype) {
320 }
321
322 }