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