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.upgrade.v6_2_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
023    import com.liferay.portal.upgrade.v6_2_0.util.DDMTemplateTable;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
026    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
027    
028    import java.sql.Connection;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    import java.sql.SQLException;
032    
033    import java.util.List;
034    
035    /**
036     * @author Juan Fernández
037     * @author Marcellus Tavares
038     */
039    public class UpgradeDynamicDataMapping extends UpgradeProcess {
040    
041            @Override
042            protected void doUpgrade() throws Exception {
043                    try {
044                            runSQL("alter table DDMTemplate add classNameId LONG");
045    
046                            runSQL("alter table DDMTemplate add templateKey STRING");
047    
048                            runSQL("alter_column_name DDMTemplate structureId classPK LONG");
049                    }
050                    catch (SQLException sqle) {
051                            upgradeTable(
052                                    DDMTemplateTable.TABLE_NAME, DDMTemplateTable.TABLE_COLUMNS,
053                                    DDMTemplateTable.TABLE_SQL_CREATE,
054                                    DDMTemplateTable.TABLE_SQL_ADD_INDEXES);
055                    }
056    
057                    long classNameId = PortalUtil.getClassNameId(DDMTemplate.class);
058    
059                    runSQL("update DDMTemplate set classNameId = " + classNameId);
060    
061                    updateStructures();
062            }
063    
064            protected void updateMetadataElement(
065                    Element metadataElement, String[] relocatedMetadadaEntryNames,
066                    String[] removedMetadataEntryNames) {
067    
068                    Element parentElement = metadataElement.getParent();
069    
070                    List<Element> entryElements = metadataElement.elements("entry");
071    
072                    for (Element entryElement : entryElements) {
073                            String name = entryElement.attributeValue("name");
074    
075                            if (ArrayUtil.contains(removedMetadataEntryNames, name)) {
076                                    metadataElement.remove(entryElement);
077                            }
078                            else if (ArrayUtil.contains(relocatedMetadadaEntryNames, name)) {
079                                    parentElement.addAttribute(name, entryElement.getText());
080    
081                                    metadataElement.remove(entryElement);
082                            }
083                    }
084            }
085    
086            protected void updateStructure(
087                            long structureId, String structureKey, String xsd)
088                    throws Exception {
089    
090                    Connection con = null;
091                    PreparedStatement ps = null;
092                    ResultSet rs = null;
093    
094                    try {
095                            con = DataAccess.getUpgradeOptimizedConnection();
096    
097                            ps = con.prepareStatement(
098                                    "update DDMStructure set structureKey = ?, xsd = ? where " +
099                                            "structureId = ?");
100    
101                            ps.setString(1, structureKey);
102                            ps.setString(2, xsd);
103                            ps.setLong(3, structureId);
104    
105                            ps.executeUpdate();
106                    }
107                    finally {
108                            DataAccess.cleanUp(con, ps, rs);
109                    }
110            }
111    
112            protected void updateStructures() throws Exception {
113                    Connection con = null;
114                    PreparedStatement ps = null;
115                    ResultSet rs = null;
116    
117                    try {
118                            con = DataAccess.getUpgradeOptimizedConnection();
119    
120                            ps = con.prepareStatement("select * from DDMStructure");
121    
122                            rs = ps.executeQuery();
123    
124                            while (rs.next()) {
125                                    long structureId = rs.getLong("structureId");
126                                    String structureKey = rs.getString("structureKey");
127                                    String xsd = rs.getString("xsd");
128    
129                                    updateStructure(
130                                            structureId, structureKey.trim().toUpperCase(),
131                                            updateXSD(xsd));
132                            }
133                    }
134                    finally {
135                            DataAccess.cleanUp(con, ps, rs);
136                    }
137            }
138    
139            protected String updateXSD(String xsd) throws Exception {
140                    Document document = SAXReaderUtil.read(xsd);
141    
142                    Element rootElement = document.getRootElement();
143    
144                    List<Element> dynamicElementElements = rootElement.elements(
145                            "dynamic-element");
146    
147                    for (Element dynamicElementElement : dynamicElementElements) {
148                            updateXSDDynamicElement(dynamicElementElement);
149                    }
150    
151                    return DDMXMLUtil.formatXML(document);
152            }
153    
154            protected void updateXSDDynamicElement(Element element) {
155                    Element metadataElement = element.element("meta-data");
156    
157                    updateMetadataElement(
158                            metadataElement,
159                            new String[] {
160                                    "multiple", "readOnly", "repeatable", "required", "showLabel",
161                                    "width",
162                            },
163                            new String[] {
164                                    "displayChildLabelAsValue", "fieldCssClass"
165                            });
166    
167                    List<Element> dynamicElementElements = element.elements(
168                            "dynamic-element");
169    
170                    for (Element dynamicElementElement : dynamicElementElements) {
171                            updateXSDDynamicElement(dynamicElementElement);
172                    }
173            }
174    
175    }