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.exportimport.kernel.staging.StagingUtil;
018    import com.liferay.portal.kernel.exception.LayoutBranchNameException;
019    import com.liferay.portal.kernel.exception.NoSuchLayoutBranchException;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.model.LayoutBranch;
022    import com.liferay.portal.kernel.model.LayoutBranchConstants;
023    import com.liferay.portal.kernel.model.LayoutRevision;
024    import com.liferay.portal.kernel.model.LayoutRevisionConstants;
025    import com.liferay.portal.kernel.model.LayoutSetBranch;
026    import com.liferay.portal.kernel.model.User;
027    import com.liferay.portal.kernel.service.ServiceContext;
028    import com.liferay.portal.kernel.util.OrderByComparator;
029    import com.liferay.portal.kernel.util.Validator;
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                    layoutRevisionLocalService.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.getCss(),
101                            serviceContext);
102    
103                    return layoutBranch;
104            }
105    
106            @Override
107            public LayoutBranch deleteLayoutBranch(long layoutBranchId)
108                    throws PortalException {
109    
110                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
111                            layoutBranchId);
112    
113                    layoutRevisionLocalService.deleteLayoutRevisions(
114                            layoutBranch.getLayoutSetBranchId(), layoutBranchId,
115                            layoutBranch.getPlid());
116    
117                    recentLayoutBranchLocalService.deleteRecentLayoutBranches(
118                            layoutBranch.getLayoutBranchId());
119    
120                    return deleteLayoutBranch(layoutBranch);
121            }
122    
123            @Override
124            public void deleteLayoutSetBranchLayoutBranches(long layoutSetBranchId)
125                    throws PortalException {
126    
127                    List<LayoutBranch> layoutBranches =
128                            layoutBranchPersistence.findByLayoutSetBranchId(layoutSetBranchId);
129    
130                    for (LayoutBranch layoutBranch : layoutBranches) {
131                            deleteLayoutBranch(layoutBranch.getLayoutBranchId());
132                    }
133            }
134    
135            @Override
136            public List<LayoutBranch> getLayoutBranches(
137                    long layoutSetBranchId, long plid, int start, int end,
138                    OrderByComparator<LayoutBranch> orderByComparator) {
139    
140                    return layoutBranchPersistence.findByL_P(
141                            layoutSetBranchId, plid, start, end, orderByComparator);
142            }
143    
144            @Override
145            public List<LayoutBranch> getLayoutSetBranchLayoutBranches(
146                    long layoutSetBranchId) {
147    
148                    return layoutBranchPersistence.findByLayoutSetBranchId(
149                            layoutSetBranchId);
150            }
151    
152            @Override
153            public LayoutBranch getMasterLayoutBranch(long layoutSetBranchId, long plid)
154                    throws PortalException {
155    
156                    return layoutBranchPersistence.findByL_P_M_First(
157                            layoutSetBranchId, plid, true, null);
158            }
159    
160            @Override
161            public LayoutBranch getMasterLayoutBranch(
162                            long layoutSetBranchId, long plid, ServiceContext serviceContext)
163                    throws PortalException {
164    
165                    LayoutBranch layoutBranch = layoutBranchPersistence.fetchByL_P_M_First(
166                            layoutSetBranchId, plid, true, null);
167    
168                    if (layoutBranch != null) {
169                            return layoutBranch;
170                    }
171    
172                    return layoutBranchLocalService.addLayoutBranch(
173                            layoutSetBranchId, plid, LayoutBranchConstants.MASTER_BRANCH_NAME,
174                            LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
175                            serviceContext);
176            }
177    
178            @Override
179            public LayoutBranch updateLayoutBranch(
180                            long layoutBranchId, String name, String description,
181                            ServiceContext serviceContext)
182                    throws PortalException {
183    
184                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
185                            layoutBranchId);
186    
187                    validate(
188                            layoutBranch.getLayoutBranchId(),
189                            layoutBranch.getLayoutSetBranchId(), layoutBranch.getPlid(), name);
190    
191                    layoutBranch.setName(name);
192                    layoutBranch.setDescription(description);
193    
194                    layoutBranchPersistence.update(layoutBranch);
195    
196                    return layoutBranch;
197            }
198    
199            protected void validate(
200                            long layoutBranchId, long layoutSetBranchId, long plid, String name)
201                    throws PortalException {
202    
203                    if (Validator.isNull(name) || (name.length() < 4)) {
204                            throw new LayoutBranchNameException(
205                                    LayoutBranchNameException.TOO_SHORT);
206                    }
207    
208                    if (name.length() > 100) {
209                            throw new LayoutBranchNameException(
210                                    LayoutBranchNameException.TOO_LONG);
211                    }
212    
213                    try {
214                            LayoutBranch layoutBranch = layoutBranchPersistence.findByL_P_N(
215                                    layoutSetBranchId, plid, name);
216    
217                            if (layoutBranch.getLayoutBranchId() != layoutBranchId) {
218                                    throw new LayoutBranchNameException(
219                                            LayoutBranchNameException.DUPLICATE);
220                            }
221                    }
222                    catch (NoSuchLayoutBranchException nslbe) {
223                    }
224            }
225    
226    }