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