001    /**
002     * Copyright (c) 2000-2012 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.portletdisplaytemplate.PortletDisplayTemplateHandler;
023    import com.liferay.portal.kernel.portletdisplaytemplate.PortletDisplayTemplateHandlerRegistryUtil;
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, 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<PortletDisplayTemplateHandler> portletDisplayTemplateHandlers =
094                            PortletDisplayTemplateHandlerRegistryUtil
095                                    .getPortletDisplayTemplateHandlers();
096    
097                    for (PortletDisplayTemplateHandler portletDisplayTemplateHandler :
098                                    portletDisplayTemplateHandlers) {
099    
100                            long classNameId = PortalUtil.getClassNameId(
101                                    portletDisplayTemplateHandler.getClassName());
102    
103                            List<Element> templateElements =
104                                    portletDisplayTemplateHandler.getDefaultTemplateElements();
105    
106                            for (Element templateElement : templateElements) {
107                                    String templateKey = templateElement.elementText(
108                                            "template-key");
109    
110                                    DDMTemplate ddmTemplate =
111                                            DDMTemplateLocalServiceUtil.fetchTemplate(
112                                                    groupId, templateKey);
113    
114                                    if (ddmTemplate != null) {
115                                            continue;
116                                    }
117    
118                                    String name = templateElement.elementText("name");
119                                    String description = templateElement.elementText("description");
120                                    String language = templateElement.elementText("language");
121                                    String scriptFileName = templateElement.elementText(
122                                            "script-file");
123                                    boolean cacheable = GetterUtil.getBoolean(
124                                            templateElement.elementText("cacheable"));
125    
126                                    addDDMTemplate(
127                                            userId, groupId, classNameId, templateKey, name,
128                                            description, language, scriptFileName, cacheable,
129                                            serviceContext);
130                            }
131                    }
132            }
133    
134            protected void doRun(long companyId) throws Exception {
135                    ServiceContext serviceContext = new ServiceContext();
136    
137                    Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);
138    
139                    serviceContext.setScopeGroupId(group.getGroupId());
140    
141                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
142    
143                    serviceContext.setUserId(defaultUserId);
144    
145                    addDDMTemplates(defaultUserId, group.getGroupId(), serviceContext);
146            }
147    
148    }