001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.Date;
031    import java.util.List;
032    import java.util.Locale;
033    import java.util.Map;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Eduardo Lundgren
038     */
039    public class DDMTemplateLocalServiceImpl
040            extends DDMTemplateLocalServiceBaseImpl {
041    
042            public DDMTemplate addTemplate(
043                            long userId, long groupId, long structureId,
044                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
045                            String type, String mode, String language, String script,
046                            ServiceContext serviceContext)
047                    throws PortalException, SystemException {
048    
049                    // Template
050    
051                    User user = userPersistence.findByPrimaryKey(userId);
052                    Date now = new Date();
053    
054                    validate(nameMap, script);
055    
056                    long templateId = counterLocalService.increment();
057    
058                    DDMTemplate template = ddmTemplatePersistence.create(templateId);
059    
060                    template.setUuid(serviceContext.getUuid());
061                    template.setGroupId(groupId);
062                    template.setCompanyId(user.getCompanyId());
063                    template.setUserId(user.getUserId());
064                    template.setUserName(user.getFullName());
065                    template.setCreateDate(serviceContext.getCreateDate(now));
066                    template.setModifiedDate(serviceContext.getModifiedDate(now));
067                    template.setStructureId(structureId);
068                    template.setNameMap(nameMap);
069                    template.setDescriptionMap(descriptionMap);
070                    template.setType(type);
071                    template.setMode(mode);
072                    template.setLanguage(language);
073                    template.setScript(script);
074    
075                    ddmTemplatePersistence.update(template, false);
076    
077                    // Resources
078    
079                    if (serviceContext.getAddGroupPermissions() ||
080                            serviceContext.getAddGuestPermissions()) {
081    
082                            addTemplateResources(
083                                    template, serviceContext.getAddGroupPermissions(),
084                                    serviceContext.getAddGuestPermissions());
085                    }
086                    else {
087                            addTemplateResources(
088                                    template, serviceContext.getGroupPermissions(),
089                                    serviceContext.getGuestPermissions());
090                    }
091    
092                    return template;
093            }
094    
095            public void addTemplateResources(
096                            DDMTemplate template, boolean addGroupPermissions,
097                            boolean addGuestPermissions)
098                    throws PortalException, SystemException {
099    
100                    resourceLocalService.addResources(
101                            template.getCompanyId(), template.getGroupId(),
102                            template.getUserId(), DDMTemplate.class.getName(),
103                            template.getTemplateId(), false, addGroupPermissions,
104                            addGuestPermissions);
105            }
106    
107            public void addTemplateResources(
108                            DDMTemplate template, String[] groupPermissions,
109                            String[] guestPermissions)
110                    throws PortalException, SystemException {
111    
112                    resourceLocalService.addModelResources(
113                            template.getCompanyId(), template.getGroupId(),
114                            template.getUserId(), DDMTemplate.class.getName(),
115                            template.getTemplateId(), groupPermissions, guestPermissions);
116            }
117    
118            public void deleteTemplate(DDMTemplate template)
119                    throws PortalException, SystemException {
120    
121                    // Template
122    
123                    ddmTemplatePersistence.remove(template);
124    
125                    // Resources
126    
127                    resourceLocalService.deleteResource(
128                            template.getCompanyId(), DDMTemplate.class.getName(),
129                            ResourceConstants.SCOPE_INDIVIDUAL, template.getTemplateId());
130            }
131    
132            public void deleteTemplate(long templateId)
133                    throws PortalException, SystemException {
134    
135                    DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey(
136                            templateId);
137    
138                    deleteTemplate(template);
139            }
140    
141            public void deleteTemplates(long groupId)
142                    throws PortalException, SystemException {
143    
144                    List<DDMTemplate> templates = ddmTemplatePersistence.findByGroupId(
145                            groupId);
146    
147                    for (DDMTemplate template : templates) {
148                            deleteTemplate(template);
149                    }
150            }
151    
152            public DDMTemplate getTemplate(long templateId)
153                    throws PortalException, SystemException {
154    
155                    return ddmTemplatePersistence.findByPrimaryKey(templateId);
156            }
157    
158            public List<DDMTemplate> getTemplates(long structureId)
159                    throws SystemException {
160    
161                    return ddmTemplatePersistence.findByStructureId(structureId);
162            }
163    
164            public List<DDMTemplate> getTemplates(long structureId, String type)
165                    throws SystemException {
166    
167                    return ddmTemplatePersistence.findByS_T(structureId, type);
168            }
169    
170            public List<DDMTemplate> getTemplates(
171                            long structureId, String type, String mode)
172                    throws SystemException {
173    
174                    return ddmTemplatePersistence.findByS_T_M(structureId, type, mode);
175            }
176    
177            public List<DDMTemplate> search(
178                            long companyId, long groupId, long structureId, String keywords,
179                            String type, String mode, int start, int end,
180                            OrderByComparator orderByComparator)
181                    throws SystemException {
182    
183                    return ddmTemplateFinder.findByKeywords(
184                            companyId, groupId, structureId, keywords, type, mode, start, end,
185                            orderByComparator);
186            }
187    
188            public List<DDMTemplate> search(
189                            long companyId, long groupId, long structureId, String name,
190                            String description, String type, String mode, String language,
191                            boolean andOperator, int start, int end,
192                            OrderByComparator orderByComparator)
193                    throws SystemException {
194    
195                    return ddmTemplateFinder.findByC_G_S_N_D_T_M_L(
196                            companyId, groupId, structureId, name, description, type, mode,
197                            language, andOperator, start, end, orderByComparator);
198            }
199    
200            public int searchCount(
201                            long companyId, long groupId, long structureId, String keywords,
202                            String type, String mode)
203                    throws SystemException {
204    
205                    return ddmTemplateFinder.countByKeywords(
206                            companyId, groupId, structureId, keywords, type, mode);
207            }
208    
209            public int searchCount(
210                            long companyId, long groupId, long structureId, String name,
211                            String description, String type, String mode, String language,
212                            boolean andOperator)
213                    throws SystemException {
214    
215                    return ddmTemplateFinder.countByC_G_S_N_D_T_M_L(
216                            companyId, groupId, structureId, name, description, type, mode,
217                            language, andOperator);
218            }
219    
220            public DDMTemplate updateTemplate(
221                            long templateId, Map<Locale, String> nameMap,
222                            Map<Locale, String> descriptionMap, String type, String mode,
223                            String language, String script, ServiceContext serviceContext)
224                    throws PortalException, SystemException {
225    
226                    validate(nameMap, script);
227    
228                    DDMTemplate template = ddmTemplateLocalService.getDDMTemplate(
229                            templateId);
230    
231                    template.setModifiedDate(serviceContext.getModifiedDate(null));
232                    template.setNameMap(nameMap);
233                    template.setDescriptionMap(descriptionMap);
234                    template.setType(type);
235                    template.setMode(mode);
236                    template.setLanguage(language);
237                    template.setScript(script);
238    
239                    ddmTemplatePersistence.update(template, false);
240    
241                    return template;
242            }
243    
244            protected void validate(Map<Locale, String> nameMap, String script)
245                    throws PortalException {
246    
247                    validateName(nameMap);
248    
249                    if (Validator.isNull(script)) {
250                            throw new TemplateScriptException();
251                    }
252            }
253    
254            protected void validateName(Map<Locale, String> nameMap)
255                    throws PortalException {
256    
257                    String name = nameMap.get(LocaleUtil.getDefault());
258    
259                    if (Validator.isNull(name)) {
260                            throw new TemplateNameException();
261                    }
262            }
263    
264    }