001
014
015 package com.liferay.portlet.layoutprototypes.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.ExportImportPathUtil;
021 import com.liferay.portal.kernel.lar.PortletDataContext;
022 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023 import com.liferay.portal.kernel.xml.Element;
024 import com.liferay.portal.model.Group;
025 import com.liferay.portal.model.Layout;
026 import com.liferay.portal.model.LayoutConstants;
027 import com.liferay.portal.model.LayoutPrototype;
028 import com.liferay.portal.service.GroupLocalServiceUtil;
029 import com.liferay.portal.service.LayoutLocalServiceUtil;
030 import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
031 import com.liferay.portal.service.ServiceContext;
032
033 import java.util.List;
034
035
038 public class LayoutPrototypeStagedModelDataHandler
039 extends BaseStagedModelDataHandler <LayoutPrototype> {
040
041 public static final String[] CLASS_NAMES =
042 {LayoutPrototype.class.getName()};
043
044 @Override
045 public void deleteStagedModel(
046 String uuid, long groupId, String className, String extraData)
047 throws PortalException, SystemException {
048
049 Group group = GroupLocalServiceUtil.getGroup(groupId);
050
051 LayoutPrototype layoutPrototype =
052 LayoutPrototypeLocalServiceUtil.
053 fetchLayoutPrototypeByUuidAndCompanyId(
054 uuid, group.getCompanyId());
055
056 if (layoutPrototype != null) {
057 LayoutPrototypeLocalServiceUtil.deleteLayoutPrototype(
058 layoutPrototype);
059 }
060 }
061
062 @Override
063 public String[] getClassNames() {
064 return CLASS_NAMES;
065 }
066
067 @Override
068 public String getDisplayName(LayoutPrototype layoutPrototype) {
069 return layoutPrototype.getNameCurrentValue();
070 }
071
072 @Override
073 protected void doExportStagedModel(
074 PortletDataContext portletDataContext,
075 LayoutPrototype layoutPrototype)
076 throws Exception {
077
078 Element layoutPrototypeElement =
079 portletDataContext.getExportDataElement(layoutPrototype);
080
081 exportLayouts(
082 portletDataContext, layoutPrototype, layoutPrototypeElement);
083
084 portletDataContext.addClassedModel(
085 layoutPrototypeElement,
086 ExportImportPathUtil.getModelPath(layoutPrototype), layoutPrototype,
087 LayoutPrototypePortletDataHandler.NAMESPACE);
088 }
089
090 @Override
091 protected void doImportStagedModel(
092 PortletDataContext portletDataContext,
093 LayoutPrototype layoutPrototype)
094 throws Exception {
095
096 long userId = portletDataContext.getUserId(
097 layoutPrototype.getUserUuid());
098
099 ServiceContext serviceContext = portletDataContext.createServiceContext(
100 layoutPrototype, LayoutPrototypePortletDataHandler.NAMESPACE);
101
102 serviceContext.setAttribute("addDefaultLayout", false);
103
104 LayoutPrototype importedLayoutPrototype = null;
105
106 if (portletDataContext.isDataStrategyMirror()) {
107 LayoutPrototype existingLayoutPrototype =
108 LayoutPrototypeLocalServiceUtil.
109 fetchLayoutPrototypeByUuidAndCompanyId(
110 layoutPrototype.getUuid(),
111 portletDataContext.getCompanyId());
112
113 if (existingLayoutPrototype == null) {
114 serviceContext.setUuid(layoutPrototype.getUuid());
115
116 importedLayoutPrototype =
117 LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
118 userId, portletDataContext.getCompanyId(),
119 layoutPrototype.getNameMap(),
120 layoutPrototype.getDescription(),
121 layoutPrototype.isActive(), serviceContext);
122 }
123 else {
124 importedLayoutPrototype =
125 LayoutPrototypeLocalServiceUtil.updateLayoutPrototype(
126 existingLayoutPrototype.getLayoutPrototypeId(),
127 layoutPrototype.getNameMap(),
128 layoutPrototype.getDescription(),
129 layoutPrototype.isActive(), serviceContext);
130 }
131 }
132 else {
133 importedLayoutPrototype =
134 LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
135 userId, portletDataContext.getCompanyId(),
136 layoutPrototype.getNameMap(),
137 layoutPrototype.getDescription(),
138 layoutPrototype.isActive(), serviceContext);
139 }
140
141 importLayouts(
142 portletDataContext, layoutPrototype,
143 importedLayoutPrototype.getGroupId());
144
145 portletDataContext.importClassedModel(
146 layoutPrototype, importedLayoutPrototype,
147 LayoutPrototypePortletDataHandler.NAMESPACE);
148 }
149
150 protected void exportLayouts(
151 PortletDataContext portletDataContext,
152 LayoutPrototype layoutPrototype, Element layoutPrototypeElement)
153 throws Exception {
154
155 List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
156 layoutPrototype.getGroupId(), true,
157 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
158
159 for (Layout layout : layouts) {
160 StagedModelDataHandlerUtil.exportReferenceStagedModel(
161 portletDataContext, layoutPrototype, layout,
162 PortletDataContext.REFERENCE_TYPE_EMBEDDED);
163 }
164 }
165
166 protected void importLayouts(
167 PortletDataContext portletDataContext,
168 LayoutPrototype layoutPrototype, long importedGroupId)
169 throws PortalException {
170
171 long groupId = portletDataContext.getGroupId();
172 boolean privateLayout = portletDataContext.isPrivateLayout();
173 long scopeGroupId = portletDataContext.getScopeGroupId();
174
175 try {
176 portletDataContext.setGroupId(importedGroupId);
177 portletDataContext.setPrivateLayout(true);
178 portletDataContext.setScopeGroupId(importedGroupId);
179
180 List<Element> layoutElements =
181 portletDataContext.getReferenceDataElements(
182 layoutPrototype, Layout.class);
183
184 for (Element layoutElement : layoutElements) {
185 StagedModelDataHandlerUtil.importStagedModel(
186 portletDataContext, layoutElement);
187 }
188 }
189 finally {
190 portletDataContext.setGroupId(groupId);
191 portletDataContext.setPrivateLayout(privateLayout);
192 portletDataContext.setScopeGroupId(scopeGroupId);
193 }
194 }
195
196 @Override
197 protected boolean validateMissingReference(
198 String uuid, long companyId, long groupId)
199 throws Exception {
200
201 LayoutPrototype layoutPrototype =
202 LayoutPrototypeLocalServiceUtil.
203 fetchLayoutPrototypeByUuidAndCompanyId(uuid, companyId);
204
205 if (layoutPrototype == null) {
206 return false;
207 }
208
209 return true;
210 }
211
212 }