001    /**
002     * Copyright (c) 2000-present 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.LayoutBranchNameException;
018    import com.liferay.portal.NoSuchLayoutBranchException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.staging.StagingUtil;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.LayoutBranch;
024    import com.liferay.portal.model.LayoutBranchConstants;
025    import com.liferay.portal.model.LayoutRevision;
026    import com.liferay.portal.model.LayoutRevisionConstants;
027    import com.liferay.portal.model.LayoutSetBranch;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.base.LayoutBranchLocalServiceBaseImpl;
031    
032    import java.util.List;
033    
034    /**
035     * @author Julio Camarero
036     */
037    public class LayoutBranchLocalServiceImpl
038            extends LayoutBranchLocalServiceBaseImpl {
039    
040            @Override
041            public LayoutBranch addLayoutBranch(
042                            long layoutSetBranchId, long plid, String name, String description,
043                            boolean master, ServiceContext serviceContext)
044                    throws PortalException {
045    
046                    User user = userPersistence.findByPrimaryKey(
047                            serviceContext.getUserId());
048                    LayoutSetBranch layoutSetBranch =
049                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
050    
051                    validate(0, layoutSetBranchId, plid, name);
052    
053                    long layoutBranchId = counterLocalService.increment();
054    
055                    LayoutBranch layoutBranch = layoutBranchPersistence.create(
056                            layoutBranchId);
057    
058                    layoutBranch.setGroupId(layoutSetBranch.getGroupId());
059                    layoutBranch.setCompanyId(user.getCompanyId());
060                    layoutBranch.setUserId(user.getUserId());
061                    layoutBranch.setUserName(user.getFullName());
062                    layoutBranch.setLayoutSetBranchId(layoutSetBranchId);
063                    layoutBranch.setPlid(plid);
064                    layoutBranch.setName(name);
065                    layoutBranch.setDescription(description);
066                    layoutBranch.setMaster(master);
067    
068                    layoutBranchPersistence.update(layoutBranch);
069    
070                    StagingUtil.setRecentLayoutBranchId(
071                            user, layoutBranch.getLayoutSetBranchId(), layoutBranch.getPlid(),
072                            layoutBranch.getLayoutBranchId());
073    
074                    return layoutBranch;
075            }
076    
077            @Override
078            public LayoutBranch addLayoutBranch(
079                            long layoutRevisionId, String name, String description,
080                            boolean master, ServiceContext serviceContext)
081                    throws PortalException {
082    
083                    LayoutRevision layoutRevision =
084                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
085    
086                    LayoutBranch layoutBranch = addLayoutBranch(
087                            layoutRevision.getLayoutSetBranchId(), layoutRevision.getPlid(),
088                            name, description, master, serviceContext);
089    
090                    layoutRevisionService.addLayoutRevision(
091                            layoutBranch.getUserId(), layoutRevision.getLayoutSetBranchId(),
092                            layoutBranch.getLayoutBranchId(),
093                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID, false,
094                            layoutRevision.getPlid(), layoutRevision.getLayoutRevisionId(),
095                            layoutRevision.isPrivateLayout(), layoutRevision.getName(),
096                            layoutRevision.getTitle(), layoutRevision.getDescription(),
097                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
098                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
099                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
100                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
101                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
102                            serviceContext);
103    
104                    return layoutBranch;
105            }
106    
107            @Override
108            public LayoutBranch deleteLayoutBranch(long layoutBranchId)
109                    throws PortalException {
110    
111                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
112                            layoutBranchId);
113    
114                    layoutRevisionLocalService.deleteLayoutRevisions(
115                            layoutBranch.getLayoutSetBranchId(), layoutBranchId,
116                            layoutBranch.getPlid());
117    
118                    return deleteLayoutBranch(layoutBranch);
119            }
120    
121            @Override
122            public void deleteLayoutSetBranchLayoutBranches(long layoutSetBranchId)
123                    throws PortalException {
124    
125                    List<LayoutBranch> layoutBranches =
126                            layoutBranchPersistence.findByLayoutSetBranchId(layoutSetBranchId);
127    
128                    for (LayoutBranch layoutBranch : layoutBranches) {
129                            deleteLayoutBranch(layoutBranch.getLayoutBranchId());
130                    }
131            }
132    
133            @Override
134            public List<LayoutBranch> getLayoutBranches(
135                    long layoutSetBranchId, long plid, int start, int end,
136                    OrderByComparator<LayoutBranch> orderByComparator) {
137    
138                    return layoutBranchPersistence.findByL_P(
139                            layoutSetBranchId, plid, start, end, orderByComparator);
140            }
141    
142            @Override
143            public List<LayoutBranch> getLayoutSetBranchLayoutBranches(
144                    long layoutSetBranchId) {
145    
146                    return layoutBranchPersistence.findByLayoutSetBranchId(
147                            layoutSetBranchId);
148            }
149    
150            @Override
151            public LayoutBranch getMasterLayoutBranch(long layoutSetBranchId, long plid)
152                    throws PortalException {
153    
154                    return layoutBranchPersistence.findByL_P_M_First(
155                            layoutSetBranchId, plid, true, null);
156            }
157    
158            @Override
159            public LayoutBranch getMasterLayoutBranch(
160                            long layoutSetBranchId, long plid, ServiceContext serviceContext)
161                    throws PortalException {
162    
163                    LayoutBranch layoutBranch = layoutBranchPersistence.fetchByL_P_M_First(
164                            layoutSetBranchId, plid, true, null);
165    
166                    if (layoutBranch != null) {
167                            return layoutBranch;
168                    }
169    
170                    return layoutBranchLocalService.addLayoutBranch(
171                            layoutSetBranchId, plid, LayoutBranchConstants.MASTER_BRANCH_NAME,
172                            LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
173                            serviceContext);
174            }
175    
176            @Override
177            public LayoutBranch updateLayoutBranch(
178                            long layoutBranchId, String name, String description,
179                            ServiceContext serviceContext)
180                    throws PortalException {
181    
182                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
183                            layoutBranchId);
184    
185                    validate(
186                            layoutBranch.getLayoutBranchId(),
187                            layoutBranch.getLayoutSetBranchId(), layoutBranch.getPlid(), name);
188    
189                    layoutBranch.setName(name);
190                    layoutBranch.setDescription(description);
191    
192                    layoutBranchPersistence.update(layoutBranch);
193    
194                    return layoutBranch;
195            }
196    
197            protected void validate(
198                            long layoutBranchId, long layoutSetBranchId, long plid, String name)
199                    throws PortalException {
200    
201                    if (Validator.isNull(name) || (name.length() < 4)) {
202                            throw new LayoutBranchNameException(
203                                    LayoutBranchNameException.TOO_SHORT);
204                    }
205    
206                    if (name.length() > 100) {
207                            throw new LayoutBranchNameException(
208                                    LayoutBranchNameException.TOO_LONG);
209                    }
210    
211                    try {
212                            LayoutBranch layoutBranch = layoutBranchPersistence.findByL_P_N(
213                                    layoutSetBranchId, plid, name);
214    
215                            if (layoutBranch.getLayoutBranchId() != layoutBranchId) {
216                                    throw new LayoutBranchNameException(
217                                            LayoutBranchNameException.DUPLICATE);
218                            }
219                    }
220                    catch (NoSuchLayoutBranchException nslbe) {
221                    }
222            }
223    
224    }