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