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 portletDataContext.addZipEntry(
240 layoutSetPrototypeLARPath, inputStream);
241
242 List<Layout> layoutSetPrototypeLayouts =
243 LayoutLocalServiceUtil.getLayouts(
244 layoutSetPrototype.getGroupId(), true);
245
246 Element layoutSetPrototypeElement =
247 portletDataContext.getExportDataElement(layoutSetPrototype);
248
249 for (Layout layoutSetPrototypeLayout : layoutSetPrototypeLayouts) {
250 portletDataContext.addReferenceElement(
251 layoutSetPrototype, layoutSetPrototypeElement,
252 layoutSetPrototypeLayout,
253 PortletDataContext.REFERENCE_TYPE_EMBEDDED, false);
254 }
255 }
256 finally {
257 StreamUtil.cleanUp(inputStream);
258
259 if (file != null) {
260 file.delete();
261 }
262 }
263 }
264
265 protected String getLayoutSetPrototypeLARFileName(
266 LayoutSetPrototype layoutSetPrototype) {
267
268 return layoutSetPrototype.getLayoutSetPrototypeId() + ".lar";
269 }
270
271 protected void importLayoutPrototypes(
272 PortletDataContext portletDataContext,
273 LayoutSetPrototype layoutSetPrototype)
274 throws PortletDataException {
275
276 List<Element> layoutPrototypeElements =
277 portletDataContext.getReferenceDataElements(
278 layoutSetPrototype, LayoutPrototype.class);
279
280 for (Element layoutPrototypeElement : layoutPrototypeElements) {
281 StagedModelDataHandlerUtil.importStagedModel(
282 portletDataContext, layoutPrototypeElement);
283 }
284 }
285
286 protected void importLayouts(
287 PortletDataContext portletDataContext,
288 LayoutSetPrototype layoutSetPrototype,
289 LayoutSetPrototype importedLayoutSetPrototype,
290 ServiceContext serviceContext)
291 throws PortalException, SystemException {
292
293 InputStream inputStream = null;
294
295 try {
296 String layoutSetPrototypeLARPath =
297 ExportImportPathUtil.getModelPath(
298 layoutSetPrototype,
299 getLayoutSetPrototypeLARFileName(layoutSetPrototype));
300
301 inputStream = portletDataContext.getZipEntryAsInputStream(
302 layoutSetPrototypeLARPath);
303
304 SitesUtil.importLayoutSetPrototype(
305 importedLayoutSetPrototype, inputStream, serviceContext);
306 }
307 finally {
308 StreamUtil.cleanUp(inputStream);
309 }
310 }
311
312 @Override
313 protected boolean validateMissingReference(
314 String uuid, long companyId, long groupId)
315 throws Exception {
316
317 LayoutSetPrototype layoutSetPrototype =
318 LayoutSetPrototypeLocalServiceUtil.
319 fetchLayoutSetPrototypeByUuidAndCompanyId(uuid, companyId);
320
321 if (layoutSetPrototype == null) {
322 return false;
323 }
324
325 return true;
326 }
327
328 }