001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.LayoutSetBranchNameException;
018    import com.liferay.portal.NoSuchLayoutSetBranchException;
019    import com.liferay.portal.RequiredLayoutSetBranchException;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.staging.StagingUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.model.Image;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.model.LayoutBranch;
028    import com.liferay.portal.model.LayoutBranchConstants;
029    import com.liferay.portal.model.LayoutConstants;
030    import com.liferay.portal.model.LayoutRevision;
031    import com.liferay.portal.model.LayoutRevisionConstants;
032    import com.liferay.portal.model.LayoutSet;
033    import com.liferay.portal.model.LayoutSetBranch;
034    import com.liferay.portal.model.LayoutSetBranchConstants;
035    import com.liferay.portal.model.ResourceConstants;
036    import com.liferay.portal.model.User;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portal.service.base.LayoutSetBranchLocalServiceBaseImpl;
039    
040    import java.util.Date;
041    import java.util.List;
042    
043    /**
044     * @author Raymond Augé
045     * @author Brian Wing Shun Chan
046     * @author Julio Camarero
047     */
048    public class LayoutSetBranchLocalServiceImpl
049            extends LayoutSetBranchLocalServiceBaseImpl {
050    
051            public LayoutSetBranch addLayoutSetBranch(
052                            long userId, long groupId, boolean privateLayout, String name,
053                            String description, boolean master, long copyLayoutSetBranchId,
054                            ServiceContext serviceContext)
055                    throws PortalException, SystemException {
056    
057                    // Layout branch
058    
059                    User user = userPersistence.findByPrimaryKey(userId);
060                    Date now = new Date();
061    
062                    validate(0, groupId, privateLayout, name, master);
063    
064                    boolean logo = false;
065                    long logoId = 0;
066                    String themeId = null;
067                    String colorSchemeId = null;
068                    String wapThemeId = null;
069                    String wapColorSchemeId = null;
070                    String css = null;
071                    String settings = null;
072    
073                    if (copyLayoutSetBranchId > 0) {
074                            LayoutSetBranch copyLayoutSetBranch = getLayoutSetBranch(
075                                    copyLayoutSetBranchId);
076    
077                            logo = copyLayoutSetBranch.getLogo();
078                            logoId = copyLayoutSetBranch.getLogoId();
079                            themeId = copyLayoutSetBranch.getThemeId();
080                            colorSchemeId = copyLayoutSetBranch.getColorSchemeId();
081                            wapThemeId = copyLayoutSetBranch.getWapThemeId();
082                            wapColorSchemeId = copyLayoutSetBranch.getWapColorSchemeId();
083                            css = copyLayoutSetBranch.getCss();
084                            settings = copyLayoutSetBranch.getSettings();
085                    }
086                    else {
087                            LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
088                                    groupId, privateLayout);
089    
090                            logo = layoutSet.getLogo();
091                            logoId = layoutSet.getLogoId();
092                            themeId = layoutSet.getThemeId();
093                            colorSchemeId = layoutSet.getColorSchemeId();
094                            wapThemeId = layoutSet.getWapThemeId();
095                            wapColorSchemeId = layoutSet.getWapColorSchemeId();
096                            css = layoutSet.getCss();
097                            settings = layoutSet.getSettings();
098                    }
099    
100                    long layoutSetBranchId = counterLocalService.increment();
101    
102                    LayoutSetBranch layoutSetBranch = layoutSetBranchPersistence.create(
103                            layoutSetBranchId);
104    
105                    layoutSetBranch.setGroupId(groupId);
106                    layoutSetBranch.setCompanyId(user.getCompanyId());
107                    layoutSetBranch.setUserId(user.getUserId());
108                    layoutSetBranch.setUserName(user.getFullName());
109                    layoutSetBranch.setCreateDate(serviceContext.getCreateDate(now));
110                    layoutSetBranch.setModifiedDate(serviceContext.getModifiedDate(now));
111                    layoutSetBranch.setPrivateLayout(privateLayout);
112                    layoutSetBranch.setName(name);
113                    layoutSetBranch.setDescription(description);
114                    layoutSetBranch.setMaster(master);
115                    layoutSetBranch.setLogo(logo);
116                    layoutSetBranch.setLogoId(logoId);
117    
118                    if (logo) {
119                            Image logoImage = imageLocalService.getImage(logoId);
120    
121                            long layoutSetBranchLogoId = counterLocalService.increment();
122    
123                            imageLocalService.updateImage(
124                                    layoutSetBranchLogoId, logoImage.getTextObj(),
125                                    logoImage.getType(), logoImage.getHeight(),
126                                    logoImage.getWidth(), logoImage.getSize());
127    
128                            layoutSetBranch.setLogoId(layoutSetBranchLogoId);
129                    }
130    
131                    layoutSetBranch.setThemeId(themeId);
132                    layoutSetBranch.setColorSchemeId(colorSchemeId);
133                    layoutSetBranch.setWapThemeId(wapThemeId);
134                    layoutSetBranch.setWapColorSchemeId(wapColorSchemeId);
135                    layoutSetBranch.setCss(css);
136                    layoutSetBranch.setSettings(settings);
137    
138                    layoutSetBranchPersistence.update(layoutSetBranch, false);
139    
140                    // Resources
141    
142                    resourceLocalService.addResources(
143                            user.getCompanyId(), layoutSetBranch.getGroupId(), user.getUserId(),
144                            LayoutSetBranch.class.getName(),
145                            layoutSetBranch.getLayoutSetBranchId(), false, true, false);
146    
147                    // Layout revisions
148    
149                    if (layoutSetBranch.isMaster() ||
150                            (copyLayoutSetBranchId == LayoutSetBranchConstants.ALL_BRANCHES)) {
151    
152                            List<Layout> layouts = layoutPersistence.findByG_P(
153                                    layoutSetBranch.getGroupId(),
154                                    layoutSetBranch.getPrivateLayout());
155    
156                            for (Layout layout : layouts) {
157                                    LayoutBranch layoutBranch =
158                                            layoutBranchLocalService.addLayoutBranch(
159                                                    layoutSetBranchId, layout.getPlid(),
160                                                    LayoutBranchConstants.MASTER_BRANCH_NAME,
161                                                    LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
162                                                    serviceContext);
163    
164                                    LayoutRevision lastLayoutRevision =
165                                            layoutRevisionLocalService.fetchLastLayoutRevision(
166                                                    layout.getPlid(), true);
167    
168                                    if (lastLayoutRevision != null) {
169                                            layoutRevisionLocalService.addLayoutRevision(
170                                                    userId, layoutSetBranchId,
171                                                    layoutBranch.getLayoutBranchId(),
172                                                    LayoutRevisionConstants.
173                                                            DEFAULT_PARENT_LAYOUT_REVISION_ID,
174                                                    true, lastLayoutRevision.getPlid(),
175                                                    lastLayoutRevision.getLayoutRevisionId(),
176                                                    lastLayoutRevision.getPrivateLayout(),
177                                                    lastLayoutRevision.getName(),
178                                                    lastLayoutRevision.getTitle(),
179                                                    lastLayoutRevision.getDescription(),
180                                                    lastLayoutRevision.getKeywords(),
181                                                    lastLayoutRevision.getRobots(),
182                                                    lastLayoutRevision.getTypeSettings(),
183                                                    lastLayoutRevision.isIconImage(),
184                                                    lastLayoutRevision.getIconImageId(),
185                                                    lastLayoutRevision.getThemeId(),
186                                                    lastLayoutRevision.getColorSchemeId(),
187                                                    lastLayoutRevision.getWapThemeId(),
188                                                    lastLayoutRevision.getWapColorSchemeId(),
189                                                    lastLayoutRevision.getCss(), serviceContext);
190                                    }
191                                    else {
192                                            layoutRevisionLocalService.addLayoutRevision(
193                                                    userId, layoutSetBranchId,
194                                                    layoutBranch.getLayoutBranchId(),
195                                                    LayoutRevisionConstants.
196                                                            DEFAULT_PARENT_LAYOUT_REVISION_ID,
197                                                    false, layout.getPlid(), LayoutConstants.DEFAULT_PLID,
198                                                    layout.getPrivateLayout(), layout.getName(),
199                                                    layout.getTitle(), layout.getDescription(),
200                                                    layout.getKeywords(), layout.getRobots(),
201                                                    layout.getTypeSettings(), layout.isIconImage(),
202                                                    layout.getIconImageId(), layout.getThemeId(),
203                                                    layout.getColorSchemeId(), layout.getWapThemeId(),
204                                                    layout.getWapColorSchemeId(), layout.getCss(),
205                                                    serviceContext);
206                                    }
207                            }
208                    }
209                    else if (copyLayoutSetBranchId > 0) {
210                            List<LayoutRevision> layoutRevisions =
211                                    layoutRevisionLocalService.getLayoutRevisions(
212                                            copyLayoutSetBranchId, true);
213    
214                            for (LayoutRevision layoutRevision : layoutRevisions) {
215                                    LayoutBranch layoutBranch =
216                                            layoutBranchLocalService.addLayoutBranch(
217                                                    layoutSetBranchId, layoutRevision.getPlid(),
218                                                    LayoutBranchConstants.MASTER_BRANCH_NAME,
219                                                    LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
220                                                    serviceContext);
221    
222                                    layoutRevisionLocalService.addLayoutRevision(
223                                            userId, layoutSetBranchId, layoutBranch.getLayoutBranchId(),
224                                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
225                                            true, layoutRevision.getPlid(),
226                                            layoutRevision.getLayoutRevisionId(),
227                                            layoutRevision.getPrivateLayout(), layoutRevision.getName(),
228                                            layoutRevision.getTitle(), layoutRevision.getDescription(),
229                                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
230                                            layoutRevision.getTypeSettings(),
231                                            layoutRevision.isIconImage(),
232                                            layoutRevision.getIconImageId(),
233                                            layoutRevision.getThemeId(),
234                                            layoutRevision.getColorSchemeId(),
235                                            layoutRevision.getWapThemeId(),
236                                            layoutRevision.getWapColorSchemeId(),
237                                            layoutRevision.getCss(), serviceContext);
238                            }
239                    }
240    
241                    return layoutSetBranch;
242            }
243    
244            @Override
245            public void deleteLayoutSetBranch(LayoutSetBranch layoutSetBranch)
246                    throws PortalException, SystemException {
247    
248                    deleteLayoutSetBranch(layoutSetBranch, false);
249            }
250    
251            public void deleteLayoutSetBranch(
252                            LayoutSetBranch layoutSetBranch, boolean includeMaster)
253                    throws PortalException, SystemException {
254    
255                    // Layout branch
256    
257                    if (!includeMaster && layoutSetBranch.isMaster()) {
258                            throw new RequiredLayoutSetBranchException();
259                    }
260    
261                    layoutSetBranchPersistence.remove(layoutSetBranch);
262    
263                    // Resources
264    
265                    resourceLocalService.deleteResource(
266                            layoutSetBranch.getCompanyId(), LayoutSetBranch.class.getName(),
267                            ResourceConstants.SCOPE_INDIVIDUAL,
268                            layoutSetBranch.getLayoutSetBranchId());
269    
270                    // Layout branches
271    
272                    layoutBranchLocalService.deleteLayoutSetBranchLayoutBranches(
273                            layoutSetBranch.getLayoutSetBranchId());
274    
275                    // Layout revisions
276    
277                    layoutRevisionLocalService.deleteLayoutSetBranchLayoutRevisions(
278                            layoutSetBranch.getLayoutSetBranchId());
279            }
280    
281            @Override
282            public void deleteLayoutSetBranch(long layoutSetBranchId)
283                    throws PortalException, SystemException {
284    
285                    LayoutSetBranch layoutSetBranch =
286                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
287    
288                    deleteLayoutSetBranch(layoutSetBranch);
289            }
290    
291            public void deleteLayoutSetBranches(long groupId, boolean privateLayout)
292                    throws PortalException, SystemException {
293    
294                    deleteLayoutSetBranches(groupId, privateLayout, false);
295            }
296    
297            public void deleteLayoutSetBranches(
298                            long groupId, boolean privateLayout, boolean includeMaster)
299                    throws PortalException, SystemException {
300    
301                    List<LayoutSetBranch> layoutSetBranches =
302                            layoutSetBranchPersistence.findByG_P(groupId, privateLayout);
303    
304                    for (LayoutSetBranch layoutSetBranch : layoutSetBranches) {
305                            deleteLayoutSetBranch(layoutSetBranch, includeMaster);
306                    }
307            }
308    
309            public LayoutSetBranch getLayoutSetBranch(
310                            long groupId, boolean privateLayout, String name)
311                    throws PortalException, SystemException {
312    
313                    return layoutSetBranchPersistence.findByG_P_N(
314                            groupId, privateLayout, name);
315            }
316    
317            public List<LayoutSetBranch> getLayoutSetBranches(
318                            long groupId, boolean privateLayout)
319                    throws SystemException {
320    
321                    return layoutSetBranchPersistence.findByG_P(groupId, privateLayout);
322            }
323    
324            public LayoutSetBranch getMasterLayoutSetBranch(
325                            long groupId, boolean privateLayout)
326                    throws PortalException, SystemException {
327    
328                    return layoutSetBranchFinder.findByMaster(groupId, privateLayout);
329            }
330    
331            /**
332             * @deprecated {@link #getUserLayoutSetBranch(long, long, boolean, long, long)}
333             */
334            public LayoutSetBranch getUserLayoutSetBranch(
335                            long userId, long groupId, boolean privateLayout,
336                            long layoutSetBranchId)
337                    throws PortalException, SystemException {
338    
339                    return getUserLayoutSetBranch(
340                            userId, groupId, privateLayout, 0, layoutSetBranchId);
341            }
342    
343            public LayoutSetBranch getUserLayoutSetBranch(
344                            long userId, long groupId, boolean privateLayout, long layoutSetId,
345                            long layoutSetBranchId)
346                    throws PortalException, SystemException {
347    
348                    if (layoutSetBranchId <= 0) {
349                            User user = userPersistence.findByPrimaryKey(userId);
350    
351                            if (layoutSetId <= 0) {
352                                    LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
353                                            groupId, privateLayout);
354    
355                                    layoutSetId = layoutSet.getLayoutSetId();
356                            }
357    
358                            layoutSetBranchId = StagingUtil.getRecentLayoutSetBranchId(
359                                    user, layoutSetId);
360                    }
361    
362                    if (layoutSetBranchId > 0) {
363                            try {
364                                    return getLayoutSetBranch(layoutSetBranchId);
365                            }
366                            catch (NoSuchLayoutSetBranchException nslsbe) {
367                            }
368                    }
369    
370                    return getMasterLayoutSetBranch(groupId, privateLayout);
371            }
372    
373            public LayoutSetBranch mergeLayoutSetBranch(
374                            long layoutSetBranchId, long mergeLayoutSetBranchId,
375                            ServiceContext serviceContext)
376                    throws PortalException, SystemException {
377    
378                    LayoutSetBranch layoutSetBranch =
379                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
380    
381                    List<LayoutRevision> layoutRevisions =
382                            layoutRevisionLocalService.getLayoutRevisions(
383                                    mergeLayoutSetBranchId, true);
384    
385                    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
386    
387                    for (LayoutRevision layoutRevision : layoutRevisions) {
388                            layoutRevisionLocalService.addLayoutRevision(
389                                    layoutRevision.getUserId(),
390                                    layoutSetBranch.getLayoutSetBranchId(),
391                                    layoutRevision.getLayoutBranchId(),
392                                    LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
393                                    false, layoutRevision.getPlid(),
394                                    layoutRevision.getLayoutRevisionId(),
395                                    layoutRevision.isPrivateLayout(), layoutRevision.getName(),
396                                    layoutRevision.getTitle(), layoutRevision.getDescription(),
397                                    layoutRevision.getKeywords(), layoutRevision.getRobots(),
398                                    layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
399                                    layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
400                                    layoutRevision.getColorSchemeId(),
401                                    layoutRevision.getWapThemeId(),
402                                    layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
403                                    serviceContext);
404                    }
405    
406                    return layoutSetBranch;
407            }
408    
409            public LayoutSetBranch updateLayoutSetBranch(
410                            long layoutSetBranchId, String name, String description,
411                            ServiceContext serviceContext)
412                    throws PortalException, SystemException {
413    
414                    LayoutSetBranch layoutSetBranch =
415                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
416    
417                    validate(
418                            layoutSetBranch.getLayoutSetBranchId(),
419                            layoutSetBranch.getGroupId(), layoutSetBranch.getPrivateLayout(),
420                            name, layoutSetBranch.isMaster());
421    
422                    layoutSetBranch.setName(name);
423                    layoutSetBranch.setDescription(description);
424    
425                    layoutSetBranchPersistence.update(layoutSetBranch, false);
426    
427                    return layoutSetBranch;
428            }
429    
430            protected void validate(
431                            long layoutSetBranchId, long groupId, boolean privateLayout,
432                            String name, boolean master)
433                    throws PortalException, SystemException {
434    
435                    if (Validator.isNull(name) || (name.length() < 4)) {
436                            throw new LayoutSetBranchNameException(
437                                    LayoutSetBranchNameException.TOO_SHORT);
438                    }
439    
440                    if (name.length() > 100) {
441                            throw new LayoutSetBranchNameException(
442                                    LayoutSetBranchNameException.TOO_LONG);
443                    }
444    
445                    try {
446                            LayoutSetBranch layoutSetBranch =
447                                    layoutSetBranchPersistence.findByG_P_N(
448                                            groupId, privateLayout, name);
449    
450                            if (layoutSetBranch.getLayoutSetBranchId() != layoutSetBranchId) {
451                                    throw new LayoutSetBranchNameException(
452                                            LayoutSetBranchNameException.DUPLICATE);
453                            }
454                    }
455                    catch (NoSuchLayoutSetBranchException nsbe) {
456                    }
457    
458                    if (master) {
459                            try {
460                                    LayoutSetBranch masterLayoutSetBranch =
461                                            layoutSetBranchFinder.findByMaster(groupId, privateLayout);
462    
463                                    if (layoutSetBranchId !=
464                                                    masterLayoutSetBranch.getLayoutSetBranchId()) {
465    
466                                            throw new LayoutSetBranchNameException(
467                                                    LayoutSetBranchNameException.MASTER);
468                                    }
469                            }
470                            catch (NoSuchLayoutSetBranchException nsbe) {
471                            }
472                    }
473            }
474    
475    }