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.events;
016    
017    import com.liferay.portal.kernel.events.ActionException;
018    import com.liferay.portal.kernel.events.SimpleAction;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.template.TemplateHandler;
022    import com.liferay.portal.kernel.template.TemplateHandlerRegistryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
032    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
033    import com.liferay.util.ContentUtil;
034    
035    import java.util.HashMap;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * @author Juan Fern??ndez
042     */
043    public class AddDefaultDDMTemplatesAction extends SimpleAction {
044    
045            @Override
046            public void run(String[] ids) throws ActionException {
047                    try {
048                            doRun(GetterUtil.getLong(ids[0]));
049                    }
050                    catch (Exception e) {
051                            throw new ActionException(e);
052                    }
053            }
054    
055            protected void addDDMTemplate(
056                            long userId, long groupId, long classNameId, String templateKey,
057                            String name, String description, String language,
058                            String scriptFileName, boolean cacheable,
059                            ServiceContext serviceContext)
060                    throws PortalException {
061    
062                    DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
063                            groupId, classNameId, templateKey);
064    
065                    if (ddmTemplate != null) {
066                            return;
067                    }
068    
069                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
070                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
071    
072                    Locale[] locales = LanguageUtil.getAvailableLocales(groupId);
073    
074                    for (Locale locale : locales) {
075                            nameMap.put(locale, LanguageUtil.get(locale, name));
076                            descriptionMap.put(locale, LanguageUtil.get(locale, description));
077                    }
078    
079                    String script = ContentUtil.get(scriptFileName);
080    
081                    DDMTemplateLocalServiceUtil.addTemplate(
082                            userId, groupId, classNameId, 0, templateKey, nameMap,
083                            descriptionMap, DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, null,
084                            language, script, cacheable, false, null, null, serviceContext);
085            }
086    
087            protected void addDDMTemplates(
088                            long userId, long groupId, ServiceContext serviceContext)
089                    throws Exception {
090    
091                    List<TemplateHandler> templateHandlers =
092                            TemplateHandlerRegistryUtil.getTemplateHandlers();
093    
094                    for (TemplateHandler templateHandler : templateHandlers) {
095                            long classNameId = PortalUtil.getClassNameId(
096                                    templateHandler.getClassName());
097    
098                            List<Element> templateElements =
099                                    templateHandler.getDefaultTemplateElements();
100    
101                            for (Element templateElement : templateElements) {
102                                    String templateKey = templateElement.elementText(
103                                            "template-key");
104    
105                                    DDMTemplate ddmTemplate =
106                                            DDMTemplateLocalServiceUtil.fetchTemplate(
107                                                    groupId, classNameId, templateKey);
108    
109                                    if (ddmTemplate != null) {
110                                            continue;
111                                    }
112    
113                                    String name = templateElement.elementText("name");
114                                    String description = templateElement.elementText("description");
115                                    String language = templateElement.elementText("language");
116                                    String scriptFileName = templateElement.elementText(
117                                            "script-file");
118                                    boolean cacheable = GetterUtil.getBoolean(
119                                            templateElement.elementText("cacheable"));
120    
121                                    addDDMTemplate(
122                                            userId, groupId, classNameId, templateKey, name,
123                                            description, language, scriptFileName, cacheable,
124                                            serviceContext);
125                            }
126                    }
127            }
128    
129            protected void doRun(long companyId) throws Exception {
130                    ServiceContext serviceContext = new ServiceContext();
131    
132                    Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);
133    
134                    serviceContext.setScopeGroupId(group.getGroupId());
135    
136                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
137    
138                    serviceContext.setUserId(defaultUserId);
139    
140                    addDDMTemplates(defaultUserId, group.getGroupId(), serviceContext);
141            }
142    
143    }