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