001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.staging.LayoutStagingUtil;
020 import com.liferay.portal.kernel.util.ArrayUtil;
021 import com.liferay.portal.kernel.util.ColorSchemeFactoryUtil;
022 import com.liferay.portal.kernel.util.ProxyUtil;
023 import com.liferay.portal.kernel.util.ThemeFactoryUtil;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.model.Group;
026 import com.liferay.portal.model.LayoutSet;
027 import com.liferay.portal.model.LayoutSetBranch;
028 import com.liferay.portal.model.LayoutSetStagingHandler;
029 import com.liferay.portal.service.LayoutSetLocalService;
030 import com.liferay.portal.staging.StagingAdvicesThreadLocal;
031 import com.liferay.portal.util.ClassLoaderUtil;
032
033 import java.io.InputStream;
034
035 import java.lang.reflect.InvocationTargetException;
036 import java.lang.reflect.Method;
037
038 import java.util.ArrayList;
039 import java.util.Date;
040 import java.util.HashSet;
041 import java.util.List;
042 import java.util.Set;
043
044 import org.aopalliance.intercept.MethodInterceptor;
045 import org.aopalliance.intercept.MethodInvocation;
046
047
052 public class LayoutSetLocalServiceStagingAdvice
053 extends LayoutSetLocalServiceImpl implements MethodInterceptor {
054
055 @Override
056 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
057 if (!StagingAdvicesThreadLocal.isEnabled()) {
058 return methodInvocation.proceed();
059 }
060
061 Method method = methodInvocation.getMethod();
062
063 String methodName = method.getName();
064
065 if (!_layoutSetLocalServiceStagingAdviceMethodNames.contains(
066 methodName)) {
067
068 return wrapReturnValue(methodInvocation.proceed());
069 }
070
071 Object returnValue = null;
072
073 Object thisObject = methodInvocation.getThis();
074 Object[] arguments = methodInvocation.getArguments();
075
076 if (methodName.equals("updateLayoutSetPrototypeLinkEnabled") &&
077 (arguments.length == 5)) {
078
079 updateLayoutSetPrototypeLinkEnabled(
080 (LayoutSetLocalService)thisObject, (Long)arguments[0],
081 (Boolean)arguments[1], (Boolean)arguments[2],
082 (String)arguments[3]);
083 }
084 else if (methodName.equals("updateLogo") && (arguments.length == 5)) {
085 updateLogo(
086 (LayoutSetLocalService)thisObject, (Long)arguments[0],
087 (Boolean)arguments[1], (Boolean)arguments[2],
088 (InputStream)arguments[3], (Boolean)arguments[4]);
089 }
090 else if (methodName.equals("updateLookAndFeel") &&
091 (arguments.length == 6)) {
092
093 returnValue = updateLookAndFeel(
094 (LayoutSetLocalService)thisObject, (Long)arguments[0],
095 (Boolean)arguments[1], (String)arguments[2],
096 (String)arguments[3], (String)arguments[4],
097 (Boolean)arguments[5]);
098 }
099 else if (methodName.equals("updateSettings")) {
100 returnValue = updateSettings(
101 (LayoutSetLocalService)thisObject, (Long)arguments[0],
102 (Boolean)arguments[1], (String)arguments[2]);
103 }
104 else {
105 try {
106 Class<?> clazz = getClass();
107
108 Class<?>[] parameterTypes = ArrayUtil.append(
109 new Class<?>[] {LayoutSetLocalService.class},
110 method.getParameterTypes());
111
112 Method layoutSetLocalServiceStagingAdviceMethod =
113 clazz.getMethod(methodName, parameterTypes);
114
115 arguments = ArrayUtil.append(
116 new Object[] {thisObject}, arguments);
117
118 returnValue = layoutSetLocalServiceStagingAdviceMethod.invoke(
119 this, arguments);
120 }
121 catch (InvocationTargetException ite) {
122 throw ite.getTargetException();
123 }
124 catch (NoSuchMethodException nsme) {
125 returnValue = methodInvocation.proceed();
126 }
127 }
128
129 return wrapReturnValue(returnValue);
130 }
131
132 public void updateLayoutSetPrototypeLinkEnabled(
133 LayoutSetLocalService layoutSetLocalService, long groupId,
134 boolean privateLayout, boolean layoutSetPrototypeLinkEnabled,
135 String layoutSetPrototypeUuid)
136 throws PortalException, SystemException {
137
138 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
139 groupId, privateLayout);
140
141 layoutSet = wrapLayoutSet(layoutSet);
142
143 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
144 layoutSet);
145
146 if (layoutSetBranch == null) {
147 layoutSetLocalService.updateLayoutSetPrototypeLinkEnabled(
148 groupId, privateLayout, layoutSetPrototypeLinkEnabled,
149 layoutSetPrototypeUuid);
150
151 return;
152 }
153
154 if (Validator.isNull(layoutSetPrototypeUuid)) {
155 layoutSetPrototypeUuid =
156 layoutSetBranch.getLayoutSetPrototypeUuid();
157 }
158
159 if (Validator.isNull(layoutSetPrototypeUuid) &&
160 layoutSetPrototypeLinkEnabled) {
161
162 throw new IllegalStateException(
163 "Cannot set layoutSetPrototypeLinkEnabled to true when " +
164 "layoutSetPrototypeUuid is null");
165 }
166
167 layoutSetBranch.setLayoutSetPrototypeLinkEnabled(
168 layoutSetPrototypeLinkEnabled);
169 layoutSetBranch.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
170
171 layoutSetBranchPersistence.update(layoutSetBranch);
172 }
173
174 public LayoutSet updateLogo(
175 LayoutSetLocalService layoutSetLocalService, long groupId,
176 boolean privateLayout, boolean logo, InputStream is,
177 boolean cleanUpStream)
178 throws PortalException, SystemException {
179
180 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
181 groupId, privateLayout);
182
183 layoutSet = wrapLayoutSet(layoutSet);
184
185 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
186 layoutSet);
187
188 if (layoutSetBranch == null) {
189 return layoutSetLocalService.updateLogo(
190 groupId, privateLayout, logo, is, cleanUpStream);
191 }
192
193 layoutSetBranch.setModifiedDate(new Date());
194 layoutSetBranch.setLogo(logo);
195
196 if (logo) {
197 long logoId = layoutSetBranch.getLogoId();
198
199 if (logoId <= 0) {
200 logoId = counterLocalService.increment();
201
202 layoutSet.setLogoId(logoId);
203 }
204 }
205 else {
206 layoutSet.setLogoId(0);
207 }
208
209 layoutSetBranchPersistence.update(layoutSetBranch);
210
211 if (logo) {
212 imageLocalService.updateImage(
213 layoutSetBranch.getLogoId(), is, cleanUpStream);
214 }
215 else {
216 imageLocalService.deleteImage(layoutSetBranch.getLogoId());
217 }
218
219 return layoutSet;
220 }
221
222 public LayoutSet updateLookAndFeel(
223 LayoutSetLocalService target, long groupId, boolean privateLayout,
224 String themeId, String colorSchemeId, String css, boolean wapTheme)
225 throws PortalException, SystemException {
226
227 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
228 groupId, privateLayout);
229
230 layoutSet = wrapLayoutSet(layoutSet);
231
232 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
233 layoutSet);
234
235 if (layoutSetBranch == null) {
236 return target.updateLookAndFeel(
237 groupId, privateLayout, themeId, colorSchemeId, css, wapTheme);
238 }
239
240 layoutSetBranch.setModifiedDate(new Date());
241
242 if (Validator.isNull(themeId)) {
243 themeId = ThemeFactoryUtil.getDefaultRegularThemeId(
244 layoutSetBranch.getCompanyId());
245 }
246
247 if (Validator.isNull(colorSchemeId)) {
248 colorSchemeId =
249 ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
250 }
251
252 if (wapTheme) {
253 layoutSetBranch.setWapThemeId(themeId);
254 layoutSetBranch.setWapColorSchemeId(colorSchemeId);
255 }
256 else {
257 layoutSetBranch.setThemeId(themeId);
258 layoutSetBranch.setColorSchemeId(colorSchemeId);
259 layoutSetBranch.setCss(css);
260 }
261
262 layoutSetBranchPersistence.update(layoutSetBranch);
263
264 return layoutSet;
265 }
266
267 public LayoutSet updateSettings(
268 LayoutSetLocalService target, long groupId, boolean privateLayout,
269 String settings)
270 throws PortalException, SystemException {
271
272 LayoutSet layoutSet = layoutSetPersistence.findByG_P(
273 groupId, privateLayout);
274
275 layoutSet = wrapLayoutSet(layoutSet);
276
277 LayoutSetBranch layoutSetBranch = LayoutStagingUtil.getLayoutSetBranch(
278 layoutSet);
279
280 if (layoutSetBranch == null) {
281 return target.updateSettings(groupId, privateLayout, settings);
282 }
283
284 layoutSetBranch.setModifiedDate(new Date());
285 layoutSetBranch.setSettings(settings);
286
287 layoutSetBranchPersistence.update(layoutSetBranch);
288
289 return layoutSet;
290 }
291
292 protected LayoutSet unwrapLayoutSet(LayoutSet layoutSet) {
293 LayoutSetStagingHandler layoutSetStagingHandler =
294 LayoutStagingUtil.getLayoutSetStagingHandler(layoutSet);
295
296 if (layoutSetStagingHandler == null) {
297 return layoutSet;
298 }
299
300 return layoutSetStagingHandler.getLayoutSet();
301 }
302
303 protected LayoutSet wrapLayoutSet(LayoutSet layoutSet) {
304 LayoutSetStagingHandler layoutSetStagingHandler =
305 LayoutStagingUtil.getLayoutSetStagingHandler(layoutSet);
306
307 if (layoutSetStagingHandler != null) {
308 return layoutSet;
309 }
310
311 Group group = null;
312
313 try {
314 group = layoutSet.getGroup();
315 }
316 catch (Exception e) {
317 return layoutSet;
318 }
319
320 if (!LayoutStagingUtil.isBranchingLayoutSet(
321 group, layoutSet.getPrivateLayout())) {
322
323 return layoutSet;
324 }
325
326 return (LayoutSet)ProxyUtil.newProxyInstance(
327 ClassLoaderUtil.getPortalClassLoader(),
328 new Class[] {LayoutSet.class},
329 new LayoutSetStagingHandler(layoutSet));
330 }
331
332 protected List<LayoutSet> wrapLayoutSets(List<LayoutSet> layoutSets) {
333 if (layoutSets.isEmpty()) {
334 return layoutSets;
335 }
336
337 List<LayoutSet> wrappedLayoutSets = new ArrayList<LayoutSet>(
338 layoutSets.size());
339
340 for (int i = 0; i < layoutSets.size(); i++) {
341 LayoutSet wrappedLayoutSet = wrapLayoutSet(layoutSets.get(i));
342
343 wrappedLayoutSets.add(wrappedLayoutSet);
344 }
345
346 return wrappedLayoutSets;
347 }
348
349 protected Object wrapReturnValue(Object returnValue) {
350 if (returnValue instanceof LayoutSet) {
351 returnValue = wrapLayoutSet((LayoutSet)returnValue);
352 }
353 else if (returnValue instanceof List<?>) {
354 List<?> list = (List<?>)returnValue;
355
356 if (!list.isEmpty() && (list.get(0) instanceof LayoutSet)) {
357 returnValue = wrapLayoutSets((List<LayoutSet>)returnValue);
358 }
359 }
360
361 return returnValue;
362 }
363
364 private static Set<String> _layoutSetLocalServiceStagingAdviceMethodNames =
365 new HashSet<String>();
366
367 static {
368 _layoutSetLocalServiceStagingAdviceMethodNames.add(
369 "updateLayoutSetPrototypeLinkEnabled");
370 _layoutSetLocalServiceStagingAdviceMethodNames.add("updateLogo");
371 _layoutSetLocalServiceStagingAdviceMethodNames.add("updateLookAndFeel");
372 _layoutSetLocalServiceStagingAdviceMethodNames.add("updateSettings");
373 }
374
375 }