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.portlet.dynamicdatamapping.util;
016    
017    import com.liferay.portal.kernel.bean.BeanParamUtil;
018    import com.liferay.portal.kernel.json.JSONArray;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.template.StringTemplateResource;
022    import com.liferay.portal.kernel.template.Template;
023    import com.liferay.portal.kernel.template.TemplateConstants;
024    import com.liferay.portal.kernel.template.TemplateContextType;
025    import com.liferay.portal.kernel.template.TemplateManagerUtil;
026    import com.liferay.portal.kernel.template.TemplateResource;
027    import com.liferay.portal.kernel.template.TemplateVariableDefinition;
028    import com.liferay.portal.kernel.template.TemplateVariableGroup;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.UniqueList;
031    import com.liferay.portal.template.TemplateContextHelper;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.WebKeys;
035    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
036    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
037    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
038    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
039    
040    import java.lang.reflect.Field;
041    import java.lang.reflect.Method;
042    
043    import java.util.List;
044    import java.util.Map;
045    
046    import javax.servlet.http.HttpServletRequest;
047    
048    /**
049     * @author Juan Fernández
050     * @author Jorge Ferrer
051     */
052    public class DDMTemplateHelperImpl implements DDMTemplateHelper {
053    
054            public DDMStructure fetchStructure(DDMTemplate template) {
055                    try {
056                            long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
057    
058                            if (template.getClassNameId() == classNameId) {
059                                    return DDMStructureLocalServiceUtil.fetchDDMStructure(
060                                            template.getClassPK());
061                            }
062                    }
063                    catch (Exception e) {
064                    }
065    
066                    return null;
067            }
068    
069            public String getAutocompleteJSON(HttpServletRequest request)
070                    throws Exception {
071    
072                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
073    
074                    JSONObject typesJSONObject = JSONFactoryUtil.createJSONObject();
075                    JSONObject variablesJSONObject = JSONFactoryUtil.createJSONObject();
076    
077                    for (TemplateVariableDefinition templateVariableDefinition :
078                                    getAutocompleteTemplateVariableDefinitions(request)) {
079    
080                            Class<?> clazz = templateVariableDefinition.getClazz();
081    
082                            if (clazz == null) {
083                                    variablesJSONObject.put(
084                                            templateVariableDefinition.getName(), StringPool.BLANK);
085                            }
086                            else {
087                                    if (!typesJSONObject.has(clazz.getName())) {
088                                            typesJSONObject.put(
089                                                    clazz.getName(), getAutocompleteClassJSONObject(clazz));
090                                    }
091    
092                                    variablesJSONObject.put(
093                                            templateVariableDefinition.getName(),
094                                            getAutocompleteVariableJSONObject(clazz));
095                            }
096                    }
097    
098                    jsonObject.put("types", typesJSONObject);
099                    jsonObject.put("variables", variablesJSONObject);
100    
101                    return jsonObject.toString();
102            }
103    
104            protected JSONObject getAutocompleteClassJSONObject(Class<?> clazz) {
105                    JSONObject typeJSONObject = JSONFactoryUtil.createJSONObject();
106    
107                    for (Field field : clazz.getFields()) {
108                            JSONObject fieldJSONObject = getAutocompleteVariableJSONObject(
109                                    field.getType());
110    
111                            typeJSONObject.put(field.getName(), fieldJSONObject);
112                    }
113    
114                    for (Method method : clazz.getMethods()) {
115                            JSONObject methodJSONObject = JSONFactoryUtil.createJSONObject();
116    
117                            JSONArray parametersTypesArray = JSONFactoryUtil.createJSONArray();
118    
119                            Class<?>[] parameterTypes = method.getParameterTypes();
120    
121                            for (Class<?> parameterType : parameterTypes) {
122                                    parametersTypesArray.put(parameterType.getCanonicalName());
123                            }
124    
125                            methodJSONObject.put("argumentTypes", parametersTypesArray);
126    
127                            Class<?> returnTypeClass = method.getReturnType();
128    
129                            methodJSONObject.put("returnType", returnTypeClass.getName());
130    
131                            methodJSONObject.put("type", "Method");
132    
133                            typeJSONObject.put(method.getName(), methodJSONObject);
134                    }
135    
136                    return typeJSONObject;
137            }
138    
139            protected List<TemplateVariableDefinition>
140                            getAutocompleteTemplateVariableDefinitions(
141                                    HttpServletRequest request)
142                    throws Exception {
143    
144                    List<TemplateVariableDefinition> templateVariableDefinitions =
145                            new UniqueList<TemplateVariableDefinition>();
146    
147                    // Declared variables
148    
149                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
150                            WebKeys.THEME_DISPLAY);
151    
152                    DDMTemplate ddmTemplate = (DDMTemplate)request.getAttribute(
153                            WebKeys.DYNAMIC_DATA_MAPPING_TEMPLATE);
154    
155                    long classPK = BeanParamUtil.getLong(ddmTemplate, request, "classPK");
156                    long classNameId = BeanParamUtil.getLong(
157                            ddmTemplate, request, "classNameId");
158    
159                    if (classPK > 0) {
160                            DDMStructure ddmStructure = DDMStructureServiceUtil.getStructure(
161                                    classPK);
162    
163                            classNameId = ddmStructure.getClassNameId();
164                    }
165                    else if (ddmTemplate != null) {
166                            classNameId = ddmTemplate.getClassNameId();
167                    }
168    
169                    Map<String, TemplateVariableGroup> templateVariableGroups =
170                            TemplateContextHelper.getTemplateVariableGroups(
171                                    classNameId, classPK, themeDisplay.getLocale());
172    
173                    for (TemplateVariableGroup templateVariableGroup :
174                                    templateVariableGroups.values()) {
175    
176                            if (!templateVariableGroup.isAutocompleteEnabled()) {
177                                    continue;
178                            }
179    
180                            templateVariableDefinitions.addAll(
181                                    templateVariableGroup.getTemplateVariableDefinitions());
182                    }
183    
184                    // Other variables
185    
186                    TemplateResource templateResource = new StringTemplateResource(
187                            _TEMPLATE_ID, _TEMPLATE_CONTENT);
188    
189                    Template template = TemplateManagerUtil.getTemplate(
190                            TemplateConstants.LANG_TYPE_FTL, templateResource,
191                            TemplateContextType.STANDARD);
192    
193                    template.prepare(request);
194    
195                    for (String key : template.getKeys()) {
196                            Object value = template.get(key);
197    
198                            if (value == null) {
199                                    continue;
200                            }
201    
202                            TemplateVariableDefinition variableDefinition =
203                                    new TemplateVariableDefinition(key, value.getClass(), key);
204    
205                            templateVariableDefinitions.add(variableDefinition);
206                    }
207    
208                    return templateVariableDefinitions;
209            }
210    
211            protected JSONObject getAutocompleteVariableJSONObject(Class<?> clazz) {
212                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
213    
214                    jsonObject.put("type", clazz.getName());
215    
216                    return jsonObject;
217            }
218    
219            private static final String _TEMPLATE_CONTENT = "# Placeholder";
220    
221            private static final String _TEMPLATE_ID = "0";
222    
223    }