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.exception.NoSuchLayoutFriendlyURLException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.LayoutFriendlyURL;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.base.LayoutFriendlyURLLocalServiceBaseImpl;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    import java.util.Locale;
031    import java.util.Map;
032    
033    /**
034     * Provides the local service for accessing, adding, deleting, and updating
035     * friendly URLs for layouts.
036     *
037     * <p>
038     * All custom service methods should be put in this class. Whenever methods are
039     * added, rerun ServiceBuilder to copy their definitions into the {@link
040     * com.liferay.portal.service.LayoutFriendlyURLLocalService} interface.
041     * </p>
042     *
043     * <p>
044     * Methods of this service will not have security checks based on the propagated
045     * JAAS credentials because this service can only be accessed from within the
046     * same VM.
047     * </p>
048     *
049     * @author Brian Wing Shun Chan
050     */
051    public class LayoutFriendlyURLLocalServiceImpl
052            extends LayoutFriendlyURLLocalServiceBaseImpl {
053    
054            @Override
055            public LayoutFriendlyURL addLayoutFriendlyURL(
056                            long userId, long companyId, long groupId, long plid,
057                            boolean privateLayout, String friendlyURL, String languageId,
058                            ServiceContext serviceContext)
059                    throws PortalException {
060    
061                    User user = userPersistence.findByPrimaryKey(userId);
062    
063                    long layoutFriendlyURLId = counterLocalService.increment();
064    
065                    LayoutFriendlyURL layoutFriendlyURL =
066                            layoutFriendlyURLPersistence.create(layoutFriendlyURLId);
067    
068                    layoutFriendlyURL.setUuid(serviceContext.getUuid());
069                    layoutFriendlyURL.setGroupId(groupId);
070                    layoutFriendlyURL.setCompanyId(companyId);
071                    layoutFriendlyURL.setUserId(user.getUserId());
072                    layoutFriendlyURL.setUserName(user.getFullName());
073                    layoutFriendlyURL.setPlid(plid);
074                    layoutFriendlyURL.setPrivateLayout(privateLayout);
075                    layoutFriendlyURL.setFriendlyURL(friendlyURL);
076                    layoutFriendlyURL.setLanguageId(languageId);
077    
078                    return layoutFriendlyURLPersistence.update(layoutFriendlyURL);
079            }
080    
081            @Override
082            public List<LayoutFriendlyURL> addLayoutFriendlyURLs(
083                            long userId, long companyId, long groupId, long plid,
084                            boolean privateLayout, Map<Locale, String> friendlyURLMap,
085                            ServiceContext serviceContext)
086                    throws PortalException {
087    
088                    List<LayoutFriendlyURL> layoutFriendlyURLs = new ArrayList<>();
089    
090                    for (Locale locale : LanguageUtil.getAvailableLocales(groupId)) {
091                            String friendlyURL = friendlyURLMap.get(locale);
092    
093                            if (Validator.isNull(friendlyURL)) {
094                                    continue;
095                            }
096    
097                            LayoutFriendlyURL layoutFriendlyURL = addLayoutFriendlyURL(
098                                    userId, companyId, groupId, plid, privateLayout, friendlyURL,
099                                    LocaleUtil.toLanguageId(locale), serviceContext);
100    
101                            layoutFriendlyURLs.add(layoutFriendlyURL);
102                    }
103    
104                    return layoutFriendlyURLs;
105            }
106    
107            @Override
108            public LayoutFriendlyURL deleteLayoutFriendlyURL(
109                    LayoutFriendlyURL layoutFriendlyURL) {
110    
111                    return layoutFriendlyURLPersistence.remove(layoutFriendlyURL);
112            }
113    
114            @Override
115            public void deleteLayoutFriendlyURL(long plid, String languageId) {
116                    LayoutFriendlyURL layoutFriendlyURL =
117                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
118    
119                    if (layoutFriendlyURL != null) {
120                            deleteLayoutFriendlyURL(layoutFriendlyURL);
121                    }
122            }
123    
124            @Override
125            public void deleteLayoutFriendlyURLs(long plid) {
126                    List<LayoutFriendlyURL> layoutFriendlyURLs =
127                            layoutFriendlyURLPersistence.findByPlid(plid);
128    
129                    for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
130                            deleteLayoutFriendlyURL(layoutFriendlyURL);
131                    }
132            }
133    
134            @Override
135            public LayoutFriendlyURL fetchFirstLayoutFriendlyURL(
136                    long groupId, boolean privateLayout, String friendlyURL) {
137    
138                    return layoutFriendlyURLPersistence.fetchByG_P_F_First(
139                            groupId, privateLayout, friendlyURL, null);
140            }
141    
142            @Override
143            public LayoutFriendlyURL fetchLayoutFriendlyURL(
144                    long groupId, boolean privateLayout, String friendlyURL,
145                    String languageId) {
146    
147                    return layoutFriendlyURLPersistence.fetchByG_P_F_L(
148                            groupId, privateLayout, friendlyURL, languageId);
149            }
150    
151            @Override
152            public LayoutFriendlyURL fetchLayoutFriendlyURL(
153                    long plid, String languageId) {
154    
155                    return fetchLayoutFriendlyURL(plid, languageId, true);
156            }
157    
158            @Override
159            public LayoutFriendlyURL fetchLayoutFriendlyURL(
160                    long plid, String languageId, boolean useDefault) {
161    
162                    LayoutFriendlyURL layoutFriendlyURL =
163                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
164    
165                    if ((layoutFriendlyURL == null) && !useDefault) {
166                            return null;
167                    }
168    
169                    if (layoutFriendlyURL == null) {
170                            layoutFriendlyURL = layoutFriendlyURLPersistence.fetchByP_L(
171                                    plid, LocaleUtil.toLanguageId(LocaleUtil.getSiteDefault()));
172                    }
173    
174                    if (layoutFriendlyURL == null) {
175                            layoutFriendlyURL = layoutFriendlyURLPersistence.fetchByPlid_First(
176                                    plid, null);
177                    }
178    
179                    return layoutFriendlyURL;
180            }
181    
182            @Override
183            public LayoutFriendlyURL getLayoutFriendlyURL(long plid, String languageId)
184                    throws PortalException {
185    
186                    return getLayoutFriendlyURL(plid, languageId, true);
187            }
188    
189            @Override
190            public LayoutFriendlyURL getLayoutFriendlyURL(
191                            long plid, String languageId, boolean useDefault)
192                    throws PortalException {
193    
194                    LayoutFriendlyURL layoutFriendlyURL =
195                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
196    
197                    if ((layoutFriendlyURL == null) && !useDefault) {
198                            StringBundler sb = new StringBundler(5);
199    
200                            sb.append("{plid=");
201                            sb.append(plid);
202                            sb.append(", languageId=");
203                            sb.append(languageId);
204                            sb.append("}");
205    
206                            throw new NoSuchLayoutFriendlyURLException(sb.toString());
207                    }
208    
209                    if (layoutFriendlyURL == null) {
210                            layoutFriendlyURL = layoutFriendlyURLPersistence.fetchByP_L(
211                                    plid, LocaleUtil.toLanguageId(LocaleUtil.getSiteDefault()));
212                    }
213    
214                    if (layoutFriendlyURL == null) {
215                            layoutFriendlyURL = layoutFriendlyURLPersistence.findByPlid_First(
216                                    plid, null);
217                    }
218    
219                    return layoutFriendlyURL;
220            }
221    
222            @Override
223            public List<LayoutFriendlyURL> getLayoutFriendlyURLs(long plid) {
224                    return layoutFriendlyURLPersistence.findByPlid(plid);
225            }
226    
227            @Override
228            public List<LayoutFriendlyURL> getLayoutFriendlyURLs(
229                    long plid, String friendlyURL, int start, int end) {
230    
231                    return layoutFriendlyURLPersistence.findByP_F(
232                            plid, friendlyURL, start, end);
233            }
234    
235            @Override
236            public LayoutFriendlyURL updateLayoutFriendlyURL(
237                            long userId, long companyId, long groupId, long plid,
238                            boolean privateLayout, String friendlyURL, String languageId,
239                            ServiceContext serviceContext)
240                    throws PortalException {
241    
242                    LayoutFriendlyURL layoutFriendlyURL =
243                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
244    
245                    if (layoutFriendlyURL == null) {
246                            return addLayoutFriendlyURL(
247                                    userId, companyId, groupId, plid, privateLayout, friendlyURL,
248                                    languageId, serviceContext);
249                    }
250    
251                    layoutFriendlyURL.setFriendlyURL(friendlyURL);
252    
253                    return layoutFriendlyURLPersistence.update(layoutFriendlyURL);
254            }
255    
256            @Override
257            public List<LayoutFriendlyURL> updateLayoutFriendlyURLs(
258                            long userId, long companyId, long groupId, long plid,
259                            boolean privateLayout, Map<Locale, String> friendlyURLMap,
260                            ServiceContext serviceContext)
261                    throws PortalException {
262    
263                    List<LayoutFriendlyURL> layoutFriendlyURLs = new ArrayList<>();
264    
265                    for (Locale locale : LanguageUtil.getAvailableLocales(groupId)) {
266                            String friendlyURL = friendlyURLMap.get(locale);
267                            String languageId = LocaleUtil.toLanguageId(locale);
268    
269                            if (Validator.isNull(friendlyURL)) {
270                                    deleteLayoutFriendlyURL(plid, languageId);
271                            }
272                            else {
273                                    LayoutFriendlyURL layoutFriendlyURL = updateLayoutFriendlyURL(
274                                            userId, companyId, groupId, plid, privateLayout,
275                                            friendlyURL, languageId, serviceContext);
276    
277                                    layoutFriendlyURLs.add(layoutFriendlyURL);
278                            }
279                    }
280    
281                    return layoutFriendlyURLs;
282            }
283    
284    }