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