001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.NoSuchLayoutRevisionException;
018    import com.liferay.portal.NoSuchPortletPreferencesException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.staging.StagingUtil;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
026    import com.liferay.portal.model.LayoutRevision;
027    import com.liferay.portal.model.LayoutRevisionConstants;
028    import com.liferay.portal.model.LayoutSetBranch;
029    import com.liferay.portal.model.PortletPreferences;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.service.base.LayoutRevisionLocalServiceBaseImpl;
033    import com.liferay.portal.util.comparator.LayoutRevisionCreateDateComparator;
034    
035    import java.util.Date;
036    import java.util.List;
037    
038    /**
039     * @author Raymond Augé
040     * @author Brian Wing Shun Chan
041     */
042    public class LayoutRevisionLocalServiceImpl
043            extends LayoutRevisionLocalServiceBaseImpl {
044    
045            public LayoutRevision addLayoutRevision(
046                            long userId, long layoutSetBranchId, long layoutBranchId,
047                            long parentLayoutRevisionId, boolean head,  long plid,
048                            boolean privateLayout, String name, String title,
049                            String description, String keywords, String robots,
050                            String typeSettings, boolean iconImage, long iconImageId,
051                            String themeId, String colorSchemeId, String wapThemeId,
052                            String wapColorSchemeId, String css, ServiceContext serviceContext)
053                    throws PortalException, SystemException {
054    
055                    // Layout revision
056    
057                    User user = userPersistence.findByPrimaryKey(userId);
058                    LayoutSetBranch layoutSetBranch =
059                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
060                    parentLayoutRevisionId = getParentLayoutRevisionId(
061                            layoutSetBranchId, parentLayoutRevisionId, plid);
062                    Date now = new Date();
063    
064                    long layoutRevisionId = counterLocalService.increment();
065    
066                    LayoutRevision layoutRevision = layoutRevisionPersistence.create(
067                            layoutRevisionId);
068    
069                    layoutRevision.setGroupId(layoutSetBranch.getGroupId());
070                    layoutRevision.setCompanyId(user.getCompanyId());
071                    layoutRevision.setUserId(user.getUserId());
072                    layoutRevision.setUserName(user.getFullName());
073                    layoutRevision.setCreateDate(serviceContext.getCreateDate(now));
074                    layoutRevision.setModifiedDate(serviceContext.getModifiedDate(now));
075                    layoutRevision.setLayoutSetBranchId(layoutSetBranchId);
076                    layoutRevision.setLayoutBranchId(layoutBranchId);
077                    layoutRevision.setParentLayoutRevisionId(parentLayoutRevisionId);
078                    layoutRevision.setHead(head);
079    
080                    long mergeLayoutRevisionId = ParamUtil.getLong(
081                            serviceContext, "mergeLayoutRevisionId");
082    
083                    if (mergeLayoutRevisionId > 0) {
084                            layoutRevision.setMajor(true);
085                    }
086    
087                    layoutRevision.setPlid(plid);
088                    layoutRevision.setPrivateLayout(privateLayout);
089                    layoutRevision.setName(name);
090                    layoutRevision.setTitle(title);
091                    layoutRevision.setDescription(description);
092                    layoutRevision.setKeywords(keywords);
093                    layoutRevision.setRobots(robots);
094                    layoutRevision.setTypeSettings(typeSettings);
095    
096                    if (iconImage) {
097                            layoutRevision.setIconImage(iconImage);
098                            layoutRevision.setIconImageId(iconImageId);
099                    }
100    
101                    layoutRevision.setThemeId(themeId);
102                    layoutRevision.setColorSchemeId(colorSchemeId);
103                    layoutRevision.setWapThemeId(wapThemeId);
104                    layoutRevision.setWapColorSchemeId(wapColorSchemeId);
105                    layoutRevision.setCss(css);
106                    layoutRevision.setStatus(WorkflowConstants.STATUS_DRAFT);
107                    layoutRevision.setStatusDate(serviceContext.getModifiedDate(now));
108    
109                    layoutRevisionPersistence.update(layoutRevision, false);
110    
111                    // Portlet preferences
112    
113                    if (parentLayoutRevisionId ==
114                                    LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID) {
115    
116                            parentLayoutRevisionId = layoutRevision.getPlid();
117                    }
118    
119                    long sourceParentLayoutRevisionId = parentLayoutRevisionId;
120    
121                    if (mergeLayoutRevisionId > 0) {
122                            sourceParentLayoutRevisionId = mergeLayoutRevisionId;
123                    }
124    
125                    copyPortletPreferences(
126                            layoutRevision, sourceParentLayoutRevisionId, serviceContext);
127    
128                    // Workflow
129    
130                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
131                            user.getCompanyId(), layoutRevision.getGroupId(), user.getUserId(),
132                            LayoutRevision.class.getName(),
133                            layoutRevision.getLayoutRevisionId(), layoutRevision,
134                            serviceContext);
135    
136                    return layoutRevision;
137            }
138    
139            public void deleteLayoutLayoutRevisions(long plid)
140                    throws PortalException, SystemException {
141    
142                    for (LayoutRevision layoutRevision : getLayoutRevisions(plid)) {
143                            layoutRevisionLocalService.deleteLayoutRevision(layoutRevision);
144                    }
145            }
146    
147            @Override
148            public void deleteLayoutRevision(LayoutRevision layoutRevision)
149                    throws PortalException, SystemException {
150    
151                    if (layoutRevision.hasChildren()) {
152                            for (LayoutRevision curLayoutRevision :
153                                            layoutRevision.getChildren()) {
154    
155                                    curLayoutRevision.setParentLayoutRevisionId(
156                                            layoutRevision.getParentLayoutRevisionId());
157    
158                                    layoutRevisionPersistence.update(curLayoutRevision, false);
159                            }
160                    }
161    
162                    List<PortletPreferences> portletPreferencesList =
163                            portletPreferencesLocalService.getPortletPreferencesByPlid(
164                                    layoutRevision.getLayoutRevisionId());
165    
166                    for (PortletPreferences portletPreferences : portletPreferencesList) {
167                            try {
168                                    portletPreferencesLocalService.deletePortletPreferences(
169                                            portletPreferences.getPortletPreferencesId());
170                            }
171                            catch (NoSuchPortletPreferencesException nsppe) {
172                            }
173                    }
174    
175                    layoutRevisionPersistence.remove(layoutRevision);
176            }
177    
178            @Override
179            public void deleteLayoutRevision(long layoutRevisionId)
180                    throws PortalException, SystemException {
181    
182                    LayoutRevision layoutRevision =
183                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
184    
185                    layoutRevisionLocalService.deleteLayoutRevision(layoutRevision);
186            }
187    
188            public void deleteLayoutRevisions(long layoutSetBranchId, long plid)
189                    throws PortalException, SystemException {
190    
191                    for (LayoutRevision layoutRevision : getLayoutRevisions(
192                                    layoutSetBranchId, plid)) {
193    
194                            layoutRevisionLocalService.deleteLayoutRevision(layoutRevision);
195                    }
196            }
197    
198            public void deleteLayoutRevisions(
199                            long layoutSetBranchId, long layoutBranchId, long plid)
200                    throws PortalException, SystemException {
201    
202                    List<LayoutRevision> layoutRevisions =
203                            layoutRevisionPersistence.findByL_L_P(
204                                    layoutSetBranchId, layoutBranchId, plid);
205    
206                    for (LayoutRevision layoutRevision : layoutRevisions) {
207                            layoutRevisionLocalService.deleteLayoutRevision(layoutRevision);
208                    }
209            }
210    
211            public void deleteLayoutSetBranchLayoutRevisions(long layoutSetBranchId)
212                    throws PortalException, SystemException {
213    
214                    List<LayoutRevision> layoutRevisions =
215                            layoutRevisionPersistence.findByLayoutSetBranchId(
216                                    layoutSetBranchId);
217    
218                    for (LayoutRevision layoutRevision : layoutRevisions) {
219                            layoutRevisionLocalService.deleteLayoutRevision(layoutRevision);
220                    }
221            }
222    
223            public List<LayoutRevision> getChildLayoutRevisions(
224                            long layoutSetBranchId, long parentLayoutRevisionId, long plid)
225                    throws SystemException {
226    
227                    return layoutRevisionPersistence.findByL_P_P(
228                            layoutSetBranchId, parentLayoutRevisionId, plid);
229            }
230    
231            public List<LayoutRevision> getChildLayoutRevisions(
232                            long layoutSetBranchId, long parentLayoutRevision, long plid,
233                            int start, int end, OrderByComparator orderByComparator)
234                    throws SystemException {
235    
236                    return layoutRevisionPersistence.findByL_P_P(
237                            layoutSetBranchId, parentLayoutRevision, plid, start, end,
238                            orderByComparator);
239            }
240    
241            public int getChildLayoutRevisionsCount(
242                            long layoutSetBranchId, long parentLayoutRevision, long plid)
243                    throws SystemException {
244    
245                    return layoutRevisionPersistence.countByL_P_P(
246                            layoutSetBranchId, parentLayoutRevision, plid);
247            }
248    
249            @Override
250            public LayoutRevision getLayoutRevision(long layoutRevisionId)
251                    throws PortalException, SystemException {
252    
253                    return layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
254            }
255    
256            public LayoutRevision getLayoutRevision(
257                            long layoutSetBranchId, long plid, boolean head)
258                    throws PortalException, SystemException {
259    
260                    return layoutRevisionPersistence.findByL_H_P(
261                            layoutSetBranchId, head, plid);
262            }
263    
264            public LayoutRevision getLayoutRevision(
265                            long layoutSetBranchId, long layoutBranchId, long plid)
266                    throws PortalException, SystemException {
267    
268                    List<LayoutRevision> layoutRevisions =
269                            layoutRevisionPersistence.findByL_L_P(
270                                    layoutSetBranchId, layoutBranchId, plid, 0, 1,
271                                    new LayoutRevisionCreateDateComparator(false));
272    
273                    if (!layoutRevisions.isEmpty()) {
274                            return layoutRevisions.get(0);
275                    }
276    
277                    throw new NoSuchLayoutRevisionException();
278            }
279    
280            public List<LayoutRevision> getLayoutRevisions(long plid)
281                    throws SystemException {
282    
283                    return layoutRevisionPersistence.findByPlid(plid);
284            }
285    
286            public List<LayoutRevision> getLayoutRevisions(
287                            long layoutSetBranchId, boolean head)
288                    throws SystemException {
289    
290                    return layoutRevisionPersistence.findByL_H(layoutSetBranchId, head);
291            }
292    
293            public List<LayoutRevision> getLayoutRevisions(
294                            long layoutSetBranchId, int status)
295                    throws SystemException {
296    
297                    return layoutRevisionPersistence.findByL_S(layoutSetBranchId, status);
298            }
299    
300            public List<LayoutRevision> getLayoutRevisions(
301                            long layoutSetBranchId, long plid)
302                    throws SystemException {
303    
304                    return layoutRevisionPersistence.findByL_P(layoutSetBranchId, plid);
305            }
306    
307            public List<LayoutRevision> getLayoutRevisions(
308                            long layoutSetBranchId, long plid, int status)
309                    throws SystemException {
310    
311                    return layoutRevisionPersistence.findByL_P_S(
312                            layoutSetBranchId, plid, status);
313            }
314    
315            public List<LayoutRevision> getLayoutRevisions(
316                            long layoutSetBranchId, long layoutBranchId, long plid,
317                            int start, int end, OrderByComparator orderByComparator)
318                    throws SystemException {
319    
320                    return layoutRevisionPersistence.findByL_L_P(
321                            layoutSetBranchId, layoutBranchId, plid, start, end,
322                            orderByComparator);
323            }
324    
325            public int getLayoutRevisionsCount(
326                            long layoutSetBranchId, long layoutBranchId, long plid)
327                    throws SystemException {
328    
329                    return layoutRevisionPersistence.countByL_L_P(
330                            layoutSetBranchId, layoutBranchId, plid);
331            }
332    
333            public LayoutRevision updateLayoutRevision(
334                            long userId, long layoutRevisionId, long layoutBranchId,
335                            String name, String title, String description, String keywords,
336                            String robots, String typeSettings, boolean iconImage,
337                            long iconImageId, String themeId, String colorSchemeId,
338                            String wapThemeId, String wapColorSchemeId, String css,
339                            ServiceContext serviceContext)
340                    throws PortalException, SystemException {
341    
342                    // Layout revision
343    
344                    User user = userPersistence.findByPrimaryKey(userId);
345                    LayoutRevision oldLayoutRevision =
346                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
347                    Date now = new Date();
348    
349                    LayoutRevision layoutRevision = null;
350    
351                    int workflowAction = serviceContext.getWorkflowAction();
352    
353                    if (workflowAction != WorkflowConstants.ACTION_PUBLISH) {
354                            long newLayoutRevisionId = counterLocalService.increment();
355    
356                            layoutRevision = layoutRevisionPersistence.create(
357                                    newLayoutRevisionId);
358    
359                            layoutRevision.setGroupId(oldLayoutRevision.getGroupId());
360                            layoutRevision.setCompanyId(oldLayoutRevision.getCompanyId());
361                            layoutRevision.setUserId(user.getUserId());
362                            layoutRevision.setUserName(user.getFullName());
363                            layoutRevision.setCreateDate(serviceContext.getCreateDate(now));
364                            layoutRevision.setModifiedDate(serviceContext.getModifiedDate(now));
365                            layoutRevision.setLayoutSetBranchId(
366                                    oldLayoutRevision.getLayoutSetBranchId());
367                            layoutRevision.setParentLayoutRevisionId(
368                                    oldLayoutRevision.getLayoutRevisionId());
369                            layoutRevision.setHead(false);
370                            layoutRevision.setLayoutBranchId(layoutBranchId);
371                            layoutRevision.setPlid(oldLayoutRevision.getPlid());
372                            layoutRevision.setPrivateLayout(
373                                    oldLayoutRevision.isPrivateLayout());
374                            layoutRevision.setName(name);
375                            layoutRevision.setTitle(title);
376                            layoutRevision.setDescription(description);
377                            layoutRevision.setKeywords(keywords);
378                            layoutRevision.setRobots(robots);
379                            layoutRevision.setTypeSettings(typeSettings);
380    
381                            if (iconImage) {
382                                    layoutRevision.setIconImage(iconImage);
383                                    layoutRevision.setIconImageId(iconImageId);
384                            }
385    
386                            layoutRevision.setThemeId(themeId);
387                            layoutRevision.setColorSchemeId(colorSchemeId);
388                            layoutRevision.setWapThemeId(wapThemeId);
389                            layoutRevision.setWapColorSchemeId(wapColorSchemeId);
390                            layoutRevision.setCss(css);
391                            layoutRevision.setStatus(WorkflowConstants.STATUS_DRAFT);
392                            layoutRevision.setStatusDate(serviceContext.getModifiedDate(now));
393    
394                            layoutRevisionPersistence.update(layoutRevision, false);
395    
396                            // Portlet preferences
397    
398                            copyPortletPreferences(
399                                    layoutRevision, layoutRevision.getParentLayoutRevisionId(),
400                                    serviceContext);
401    
402                            StagingUtil.deleteRecentLayoutRevisionId(
403                                    user, layoutRevision.getLayoutSetBranchId(),
404                                    layoutRevision.getPlid());
405    
406                            StagingUtil.setRecentLayoutBranchId(
407                                    user, layoutRevision.getLayoutSetBranchId(),
408                                    layoutRevision.getPlid(), layoutRevision.getLayoutBranchId());
409                    }
410                    else {
411                            layoutRevision = oldLayoutRevision;
412                    }
413    
414                    boolean major = ParamUtil.getBoolean(serviceContext, "major");
415    
416                    if (major) {
417                            updateMajor(layoutRevision);
418                    }
419    
420                    // Workflow
421    
422                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
423                            layoutRevision.getCompanyId(), layoutRevision.getGroupId(),
424                            userId, LayoutRevision.class.getName(),
425                            layoutRevision.getLayoutRevisionId(), layoutRevision,
426                            serviceContext);
427    
428                    return layoutRevision;
429            }
430    
431            public LayoutRevision updateStatus(
432                            long userId, long layoutRevisionId, int status,
433                            ServiceContext serviceContext)
434                    throws PortalException, SystemException {
435    
436                    User user = userPersistence.findByPrimaryKey(userId);
437    
438                    LayoutRevision layoutRevision =
439                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
440    
441                    layoutRevision.setStatus(status);
442                    layoutRevision.setStatusByUserId(user.getUserId());
443                    layoutRevision.setStatusByUserName(user.getFullName());
444                    layoutRevision.setStatusDate(new Date());
445    
446                    if (status == WorkflowConstants.STATUS_APPROVED) {
447                            layoutRevision.setHead(true);
448    
449                            List<LayoutRevision> layoutRevisions =
450                                    layoutRevisionPersistence.findByL_P(
451                                            layoutRevision.getLayoutSetBranchId(),
452                                            layoutRevision.getPlid());
453    
454                            for (LayoutRevision curLayoutRevision : layoutRevisions) {
455                                    if (curLayoutRevision.getLayoutRevisionId() !=
456                                                    layoutRevision.getLayoutRevisionId()) {
457    
458                                            curLayoutRevision.setHead(false);
459    
460                                            layoutRevisionPersistence.update(curLayoutRevision, false);
461                                    }
462                            }
463                    }
464                    else {
465                            layoutRevision.setHead(false);
466    
467                            List<LayoutRevision> layoutRevisions =
468                                    layoutRevisionPersistence.findByL_P_S(
469                                            layoutRevision.getLayoutSetBranchId(),
470                                            layoutRevision.getPlid(),
471                                            WorkflowConstants.STATUS_APPROVED);
472    
473                            for (LayoutRevision curLayoutRevision : layoutRevisions) {
474                                    if (curLayoutRevision.getLayoutRevisionId() !=
475                                                    layoutRevision.getLayoutRevisionId()) {
476    
477                                            curLayoutRevision.setHead(true);
478    
479                                            layoutRevisionPersistence.update(curLayoutRevision, false);
480    
481                                            break;
482                                    }
483                            }
484                    }
485    
486                    layoutRevisionPersistence.update(layoutRevision, false);
487    
488                    return layoutRevision;
489            }
490    
491            protected void copyPortletPreferences(
492                            LayoutRevision layoutRevision, long parentLayoutRevisionId,
493                            ServiceContext serviceContext)
494                    throws SystemException {
495    
496                    List<PortletPreferences> portletPreferencesList =
497                            portletPreferencesLocalService.getPortletPreferencesByPlid(
498                                    parentLayoutRevisionId);
499    
500                    for (PortletPreferences portletPreferences : portletPreferencesList) {
501                            portletPreferencesLocalService.addPortletPreferences(
502                                    layoutRevision.getCompanyId(), portletPreferences.getOwnerId(),
503                                    portletPreferences.getOwnerType(),
504                                    layoutRevision.getLayoutRevisionId(),
505                                    portletPreferences.getPortletId(), null,
506                                    portletPreferences.getPreferences());
507                    }
508            }
509    
510            protected long getParentLayoutRevisionId(
511                            long layoutSetBranchId, long parentLayoutRevisionId, long plid)
512                    throws SystemException {
513    
514                    LayoutRevision parentLayoutRevision = null;
515    
516                    if (parentLayoutRevisionId > 0) {
517                            parentLayoutRevision = layoutRevisionPersistence.fetchByPrimaryKey(
518                                    parentLayoutRevisionId);
519    
520                            if (parentLayoutRevision == null) {
521                                    List<LayoutRevision> layoutRevisions =
522                                            layoutRevisionPersistence.findByL_P(
523                                                    layoutSetBranchId, plid, 0, 1);
524    
525                                    if (!layoutRevisions.isEmpty()) {
526                                            parentLayoutRevision = layoutRevisions.get(0);
527                                    }
528                            }
529                    }
530    
531                    if (parentLayoutRevision != null) {
532                            return parentLayoutRevision.getLayoutRevisionId();
533                    }
534    
535                    return LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID;
536            }
537    
538            protected LayoutRevision updateMajor(LayoutRevision layoutRevision)
539                    throws PortalException, SystemException {
540    
541                    long parentLayoutRevisionId =
542                            layoutRevision.getParentLayoutRevisionId();
543    
544                    boolean fork = false;
545    
546                    while (parentLayoutRevisionId !=
547                                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID) {
548    
549                            LayoutRevision parentLayoutRevision =
550                                    layoutRevisionPersistence.findByPrimaryKey(
551                                            parentLayoutRevisionId);
552    
553                            if (parentLayoutRevision.isMajor()) {
554                                    break;
555                            }
556    
557                            parentLayoutRevisionId =
558                                    parentLayoutRevision.getParentLayoutRevisionId();
559    
560                            if (parentLayoutRevision.getChildren().size() > 1) {
561                                    fork = true;
562                            }
563    
564                            if (!fork) {
565                                    layoutRevisionLocalService.deleteLayoutRevision(
566                                            parentLayoutRevision);
567                            }
568                    }
569    
570                    layoutRevision.setParentLayoutRevisionId(parentLayoutRevisionId);
571                    layoutRevision.setMajor(true);
572    
573                    layoutRevisionPersistence.update(layoutRevision, false);
574    
575                    return layoutRevision;
576            }
577    
578    }