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),
087 layoutPrototype);
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);
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 }
148
149 protected void exportLayouts(
150 PortletDataContext portletDataContext,
151 LayoutPrototype layoutPrototype, Element layoutPrototypeElement)
152 throws Exception {
153
154 List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
155 layoutPrototype.getGroupId(), true,
156 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
157
158 for (Layout layout : layouts) {
159 StagedModelDataHandlerUtil.exportReferenceStagedModel(
160 portletDataContext, layoutPrototype, layout,
161 PortletDataContext.REFERENCE_TYPE_EMBEDDED);
162 }
163 }
164
165 protected void importLayouts(
166 PortletDataContext portletDataContext,
167 LayoutPrototype layoutPrototype, long importedGroupId)
168 throws PortalException {
169
170 long groupId = portletDataContext.getGroupId();
171 boolean privateLayout = portletDataContext.isPrivateLayout();
172 long scopeGroupId = portletDataContext.getScopeGroupId();
173
174 try {
175 portletDataContext.setGroupId(importedGroupId);
176 portletDataContext.setPrivateLayout(true);
177 portletDataContext.setScopeGroupId(importedGroupId);
178
179 StagedModelDataHandlerUtil.importReferenceStagedModels(
180 portletDataContext, layoutPrototype, Layout.class);
181 }
182 finally {
183 portletDataContext.setGroupId(groupId);
184 portletDataContext.setPrivateLayout(privateLayout);
185 portletDataContext.setScopeGroupId(scopeGroupId);
186 }
187 }
188
189 @Override
190 protected boolean validateMissingReference(
191 String uuid, long companyId, long groupId)
192 throws Exception {
193
194 LayoutPrototype layoutPrototype =
195 LayoutPrototypeLocalServiceUtil.
196 fetchLayoutPrototypeByUuidAndCompanyId(uuid, companyId);
197
198 if (layoutPrototype == null) {
199 return false;
200 }
201
202 return true;
203 }
204
205 }