001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.events.ActionException;
018    import com.liferay.portal.kernel.metadata.RawMetadataProcessorUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
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.documentlibrary.NoSuchFileEntryTypeException;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
035    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
036    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
037    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
038    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
039    
040    import java.io.StringReader;
041    
042    import java.lang.reflect.Field;
043    
044    import java.util.HashMap;
045    import java.util.List;
046    import java.util.Locale;
047    import java.util.Map;
048    
049    /**
050     * @author Sergio González
051     * @author Miguel Pastor
052     */
053    public class AddDefaultDocumentLibraryStructuresAction
054            extends BaseDefaultDDMStructureAction {
055    
056            @Override
057            public void run(String[] ids) throws ActionException {
058                    try {
059                            doRun(GetterUtil.getLong(ids[0]));
060                    }
061                    catch (Exception e) {
062                            throw new ActionException(e);
063                    }
064            }
065    
066            protected void addDLFileEntryType(
067                            long userId, long groupId, String dlFileEntryTypeName,
068                            String dlFileEntryTypeDescription, String ddmStructureName,
069                            ServiceContext serviceContext)
070                    throws Exception {
071    
072                    String ddmStructureKey = ddmStructureName;
073    
074                    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
075                            groupId, ddmStructureKey);
076    
077                    if (ddmStructure == null) {
078                            return;
079                    }
080    
081                    long[] ddmStructureId = new long[] {ddmStructure.getStructureId()};
082    
083                    try {
084                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(
085                                    groupId, dlFileEntryTypeName);
086                    }
087                    catch (NoSuchFileEntryTypeException nsfete) {
088                            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
089                                    userId, groupId, dlFileEntryTypeName,
090                                    dlFileEntryTypeDescription, ddmStructureId, serviceContext);
091                    }
092            }
093    
094            protected void addDLFileEntryTypes(
095                            long userId, long groupId, ServiceContext serviceContext)
096                    throws Exception {
097    
098                    addDLFileEntryType(
099                            userId, groupId, DLFileEntryTypeConstants.NAME_IMAGE,
100                            "Image Document Type", "Default Image's Metadata Set",
101                            serviceContext);
102    
103                    addDLFileEntryType(
104                            userId, groupId, DLFileEntryTypeConstants.NAME_VIDEO,
105                            "Video Document Type", "Default Video's Metadata Set",
106                            serviceContext);
107            }
108    
109            protected void addDLRawMetadataStructures(
110                    long userId, long groupId, ServiceContext serviceContext)
111                    throws Exception {
112    
113                    String xsd = buildDLRawMetadataXML(
114                            RawMetadataProcessorUtil.getFields());
115    
116                    Document document = SAXReaderUtil.read(new StringReader(xsd));
117    
118                    Element rootElement = document.getRootElement();
119    
120                    List<Element> structureElements = rootElement.elements("structure");
121    
122                    for (Element structureElement : structureElements) {
123                            String name = structureElement.elementText("name");
124                            String description = structureElement.elementText("description");
125    
126                            Element structureElementRootElement = structureElement.element(
127                                    "root");
128    
129                            String structureElementRootXML =
130                                    structureElementRootElement.asXML();
131    
132                            DDMStructure ddmStructure =
133                                    DDMStructureLocalServiceUtil.fetchStructure(groupId, name);
134    
135                            if (ddmStructure != null) {
136                                    ddmStructure.setXsd(structureElementRootXML);
137    
138                                    DDMStructureLocalServiceUtil.updateDDMStructure(ddmStructure);
139                            }
140                            else {
141                                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
142    
143                                    nameMap.put(LocaleUtil.getDefault(), name);
144    
145                                    Map<Locale, String> descriptionMap =
146                                            new HashMap<Locale, String>();
147    
148                                    descriptionMap.put(LocaleUtil.getDefault(), description);
149    
150                                    DDMStructureLocalServiceUtil.addStructure(
151                                            userId, groupId,
152                                            PortalUtil.getClassNameId(DLFileEntry.class),
153                                            name, nameMap, descriptionMap, structureElementRootXML,
154                                            "xml", DDMStructureConstants.TYPE_DEFAULT, serviceContext);
155                            }
156                    }
157            }
158    
159            protected String buildDLRawMetadataElementXML(String name, Field field) {
160                    StringBundler sb = new StringBundler(12);
161    
162                    sb.append("<dynamic-element dataType=\"string\" name=\"");
163                    sb.append(field.getName());
164                    sb.append("\" type=\"text\">");
165                    sb.append("<meta-data locale=\"en_US\">");
166                    sb.append("<entry name=\"label\"><![CDATA[metadata.");
167                    sb.append(name);
168                    sb.append(StringPool.PERIOD);
169                    sb.append(field.getName());
170                    sb.append("]]></entry><entry name=\"predefinedValue\">");
171                    sb.append("<![CDATA[]]></entry><entry name=\"required\">");
172                    sb.append("<![CDATA[false]]></entry><entry name=\"showLabel\">");
173                    sb.append("<![CDATA[true]]></entry></meta-data></dynamic-element>");
174    
175                    return sb.toString();
176            }
177    
178            protected String buildDLRawMetadataStructureXML(
179                    String name, Field[] fields) {
180    
181                    StringBundler sb = new StringBundler(8 + fields.length);
182    
183                    sb.append("<structure><name><![CDATA[");
184                    sb.append(name);
185                    sb.append("]]></name>");
186                    sb.append("<description><![CDATA[");
187                    sb.append(name);
188                    sb.append("]]></description>");
189                    sb.append(
190                            "<root available-locales=\"en_US\" default-locale=\"en_US\">");
191    
192                    for (Field field : fields) {
193                            sb.append(buildDLRawMetadataElementXML(name, field));
194                    }
195    
196                    sb.append("</root></structure>");
197    
198                    return sb.toString();
199            }
200    
201            protected String buildDLRawMetadataXML(Map<String, Field[]> fields) {
202                    StringBundler sb = new StringBundler(2 + fields.size());
203    
204                    sb.append("<?xml version=\"1.0\"?><root>");
205    
206                    for (String key : fields.keySet()) {
207                            sb.append(buildDLRawMetadataStructureXML(key, fields.get(key)));
208                    }
209    
210                    sb.append("</root>");
211    
212                    return sb.toString();
213            }
214    
215            protected void doRun(long companyId) throws Exception {
216                    ServiceContext serviceContext = new ServiceContext();
217    
218                    Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);
219    
220                    serviceContext.setScopeGroupId(group.getGroupId());
221    
222                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
223    
224                    serviceContext.setUserId(defaultUserId);
225    
226                    addDDMStructures(
227                            defaultUserId, group.getGroupId(),
228                            PortalUtil.getClassNameId(DLFileEntryMetadata.class),
229                            "document-library-structures.xml", serviceContext);
230                    addDLFileEntryTypes(defaultUserId, group.getGroupId(), serviceContext);
231                    addDLRawMetadataStructures(
232                            defaultUserId, group.getGroupId(), serviceContext);
233            }
234    
235    }