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.portlet.dynamicdatamapping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.ResourceConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
026    import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
028    import com.liferay.portlet.dynamicdatamapping.service.base.DDMTemplateLocalServiceBaseImpl;
029    
030    import java.util.ArrayList;
031    import java.util.Date;
032    import java.util.List;
033    import java.util.Locale;
034    import java.util.Map;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Eduardo Lundgren
039     */
040    public class DDMTemplateLocalServiceImpl
041            extends DDMTemplateLocalServiceBaseImpl {
042    
043            public DDMTemplate addTemplate(
044                            long userId, long groupId, long structureId,
045                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
046                            String type, String mode, String language, String script,
047                            ServiceContext serviceContext)
048                    throws PortalException, SystemException {
049    
050                    // Template
051    
052                    User user = userPersistence.findByPrimaryKey(userId);
053                    Date now = new Date();
054    
055                    validate(nameMap, script);
056    
057                    long templateId = counterLocalService.increment();
058    
059                    DDMTemplate template = ddmTemplatePersistence.create(templateId);
060    
061                    template.setUuid(serviceContext.getUuid());
062                    template.setGroupId(groupId);
063                    template.setCompanyId(user.getCompanyId());
064                    template.setUserId(user.getUserId());
065                    template.setUserName(user.getFullName());
066                    template.setCreateDate(serviceContext.getCreateDate(now));
067                    template.setModifiedDate(serviceContext.getModifiedDate(now));
068                    template.setStructureId(structureId);
069                    template.setNameMap(nameMap);
070                    template.setDescriptionMap(descriptionMap);
071                    template.setType(type);
072                    template.setMode(mode);
073                    template.setLanguage(language);
074                    template.setScript(script);
075    
076                    ddmTemplatePersistence.update(template, false);
077    
078                    // Resources
079    
080                    if (serviceContext.isAddGroupPermissions() ||
081                            serviceContext.isAddGuestPermissions()) {
082    
083                            addTemplateResources(
084                                    template, serviceContext.isAddGroupPermissions(),
085                                    serviceContext.isAddGuestPermissions());
086                    }
087                    else {
088                            addTemplateResources(
089                                    template, serviceContext.getGroupPermissions(),
090                                    serviceContext.getGuestPermissions());
091                    }
092    
093                    return template;
094            }
095    
096            public void addTemplateResources(
097                            DDMTemplate template, boolean addGroupPermissions,
098                            boolean addGuestPermissions)
099                    throws PortalException, SystemException {
100    
101                    resourceLocalService.addResources(
102                            template.getCompanyId(), template.getGroupId(),
103                            template.getUserId(), DDMTemplate.class.getName(),
104                            template.getTemplateId(), false, addGroupPermissions,
105                            addGuestPermissions);
106            }
107    
108            public void addTemplateResources(
109                            DDMTemplate template, String[] groupPermissions,
110                            String[] guestPermissions)
111                    throws PortalException, SystemException {
112    
113                    resourceLocalService.addModelResources(
114                            template.getCompanyId(), template.getGroupId(),
115                            template.getUserId(), DDMTemplate.class.getName(),
116                            template.getTemplateId(), groupPermissions, guestPermissions);
117            }
118    
119            public List<DDMTemplate> copyTemplates(
120                            long userId, long structureId, long newStructureId, String type,
121                            ServiceContext serviceContext)
122                    throws PortalException, SystemException {
123    
124                    List<DDMTemplate> newTemplates = new ArrayList<DDMTemplate>();
125    
126                    List<DDMTemplate> oldTemplates = getTemplates(structureId, type);
127    
128                    for (DDMTemplate oldTemplate : oldTemplates) {
129                            DDMTemplate newTemplate = addTemplate(
130                                    userId, oldTemplate.getGroupId(), newStructureId,
131                                    oldTemplate.getNameMap(), oldTemplate.getDescriptionMap(),
132                                    oldTemplate.getType(), oldTemplate.getMode(),
133                                    oldTemplate.getLanguage(), oldTemplate.getScript(),
134                                    serviceContext);
135    
136                            newTemplates.add(newTemplate);
137                    }
138    
139                    return newTemplates;
140            }
141    
142            public void deleteTemplate(DDMTemplate template)
143                    throws PortalException, SystemException {
144    
145                    // Template
146    
147                    ddmTemplatePersistence.remove(template);
148    
149                    // Resources
150    
151                    resourceLocalService.deleteResource(
152                            template.getCompanyId(), DDMTemplate.class.getName(),
153                            ResourceConstants.SCOPE_INDIVIDUAL, template.getTemplateId());
154            }
155    
156            public void deleteTemplate(long templateId)
157                    throws PortalException, SystemException {
158    
159                    DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey(
160                            templateId);
161    
162                    deleteTemplate(template);
163            }
164    
165            public void deleteTemplates(long groupId)
166                    throws PortalException, SystemException {
167    
168                    List<DDMTemplate> templates = ddmTemplatePersistence.findByGroupId(
169                            groupId);
170    
171                    for (DDMTemplate template : templates) {
172                            deleteTemplate(template);
173                    }
174            }
175    
176            public DDMTemplate getTemplate(long templateId)
177                    throws PortalException, SystemException {
178    
179                    return ddmTemplatePersistence.findByPrimaryKey(templateId);
180            }
181    
182            public List<DDMTemplate> getTemplates(long structureId)
183                    throws SystemException {
184    
185                    return ddmTemplatePersistence.findByStructureId(structureId);
186            }
187    
188            public List<DDMTemplate> getTemplates(long structureId, String type)
189                    throws SystemException {
190    
191                    return ddmTemplatePersistence.findByS_T(structureId, type);
192            }
193    
194            public List<DDMTemplate> getTemplates(
195                            long structureId, String type, String mode)
196                    throws SystemException {
197    
198                    return ddmTemplatePersistence.findByS_T_M(structureId, type, mode);
199            }
200    
201            public List<DDMTemplate> search(
202                            long companyId, long groupId, long structureId, String keywords,
203                            String type, String mode, int start, int end,
204                            OrderByComparator orderByComparator)
205                    throws SystemException {
206    
207                    return ddmTemplateFinder.findByKeywords(
208                            companyId, groupId, structureId, keywords, type, mode, start, end,
209                            orderByComparator);
210            }
211    
212            public List<DDMTemplate> search(
213                            long companyId, long groupId, long structureId, String name,
214                            String description, String type, String mode, String language,
215                            boolean andOperator, int start, int end,
216                            OrderByComparator orderByComparator)
217                    throws SystemException {
218    
219                    return ddmTemplateFinder.findByC_G_S_N_D_T_M_L(
220                            companyId, groupId, structureId, name, description, type, mode,
221                            language, andOperator, start, end, orderByComparator);
222            }
223    
224            public int searchCount(
225                            long companyId, long groupId, long structureId, String keywords,
226                            String type, String mode)
227                    throws SystemException {
228    
229                    return ddmTemplateFinder.countByKeywords(
230                            companyId, groupId, structureId, keywords, type, mode);
231            }
232    
233            public int searchCount(
234                            long companyId, long groupId, long structureId, String name,
235                            String description, String type, String mode, String language,
236                            boolean andOperator)
237                    throws SystemException {
238    
239                    return ddmTemplateFinder.countByC_G_S_N_D_T_M_L(
240                            companyId, groupId, structureId, name, description, type, mode,
241                            language, andOperator);
242            }
243    
244            public DDMTemplate updateTemplate(
245                            long templateId, Map<Locale, String> nameMap,
246                            Map<Locale, String> descriptionMap, String type, String mode,
247                            String language, String script, ServiceContext serviceContext)
248                    throws PortalException, SystemException {
249    
250                    validate(nameMap, script);
251    
252                    DDMTemplate template = ddmTemplateLocalService.getDDMTemplate(
253                            templateId);
254    
255                    template.setModifiedDate(serviceContext.getModifiedDate(null));
256                    template.setNameMap(nameMap);
257                    template.setDescriptionMap(descriptionMap);
258                    template.setType(type);
259                    template.setMode(mode);
260                    template.setLanguage(language);
261                    template.setScript(script);
262    
263                    ddmTemplatePersistence.update(template, false);
264    
265                    return template;
266            }
267    
268            protected void validate(Map<Locale, String> nameMap, String script)
269                    throws PortalException {
270    
271                    validateName(nameMap);
272    
273                    if (Validator.isNull(script)) {
274                            throw new TemplateScriptException();
275                    }
276            }
277    
278            protected void validateName(Map<Locale, String> nameMap)
279                    throws PortalException {
280    
281                    String name = nameMap.get(LocaleUtil.getDefault());
282    
283                    if (Validator.isNull(name)) {
284                            throw new TemplateNameException();
285                    }
286            }
287    
288    }