001    /**
002     * Copyright (c) 2000-2013 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.upgrade.v6_2_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    import com.liferay.portal.kernel.util.ArrayUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
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.upgrade.v6_2_0.util.DDMTemplateTable;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
029    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
030    
031    import java.sql.Connection;
032    import java.sql.PreparedStatement;
033    import java.sql.ResultSet;
034    import java.sql.SQLException;
035    
036    import java.util.List;
037    
038    /**
039     * @author Juan Fern??ndez
040     * @author Marcellus Tavares
041     */
042    public class UpgradeDynamicDataMapping extends UpgradeProcess {
043    
044            @Override
045            protected void doUpgrade() throws Exception {
046                    try {
047                            runSQL("alter table DDMTemplate add classNameId LONG");
048    
049                            runSQL("alter table DDMTemplate add templateKey STRING");
050    
051                            runSQL("alter_column_name DDMTemplate structureId classPK LONG");
052                    }
053                    catch (SQLException sqle) {
054                            upgradeTable(
055                                    DDMTemplateTable.TABLE_NAME, DDMTemplateTable.TABLE_COLUMNS,
056                                    DDMTemplateTable.TABLE_SQL_CREATE,
057                                    DDMTemplateTable.TABLE_SQL_ADD_INDEXES);
058                    }
059    
060                    long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
061    
062                    try {
063                            runSQL("update DDMTemplate set classNameId = " + classNameId);
064                    }
065                    catch (Exception e) {
066                            if (_log.isWarnEnabled()) {
067                                    _log.warn(e, e);
068                            }
069                    }
070    
071                    updateStructures();
072    
073                    updateTemplates();
074            }
075    
076            protected void updateMetadataElement(
077                    Element metadataElement, String[] relocatedMetadadaEntryNames,
078                    String[] removedMetadataEntryNames) {
079    
080                    Element parentElement = metadataElement.getParent();
081    
082                    List<Element> entryElements = metadataElement.elements("entry");
083    
084                    for (Element entryElement : entryElements) {
085                            String name = entryElement.attributeValue("name");
086    
087                            if (ArrayUtil.contains(removedMetadataEntryNames, name)) {
088                                    metadataElement.remove(entryElement);
089                            }
090                            else if (ArrayUtil.contains(relocatedMetadadaEntryNames, name)) {
091                                    parentElement.addAttribute(name, entryElement.getText());
092    
093                                    metadataElement.remove(entryElement);
094                            }
095                    }
096            }
097    
098            protected void updateStructure(
099                            long structureId, String structureKey, String xsd)
100                    throws Exception {
101    
102                    Connection con = null;
103                    PreparedStatement ps = null;
104                    ResultSet rs = null;
105    
106                    try {
107                            con = DataAccess.getUpgradeOptimizedConnection();
108    
109                            ps = con.prepareStatement(
110                                    "update DDMStructure set structureKey = ?, xsd = ? where " +
111                                            "structureId = ?");
112    
113                            ps.setString(1, structureKey);
114                            ps.setString(2, xsd);
115                            ps.setLong(3, structureId);
116    
117                            ps.executeUpdate();
118                    }
119                    catch (SQLException sqle) {
120                            if (_log.isWarnEnabled()) {
121                                    _log.warn(sqle, sqle);
122                            }
123                    }
124                    finally {
125                            DataAccess.cleanUp(con, ps, rs);
126                    }
127            }
128    
129            protected void updateStructures() throws Exception {
130                    Connection con = null;
131                    PreparedStatement ps = null;
132                    ResultSet rs = null;
133    
134                    try {
135                            con = DataAccess.getUpgradeOptimizedConnection();
136    
137                            ps = con.prepareStatement(
138                                    "select structureId, structureKey, xsd from DDMStructure");
139    
140                            rs = ps.executeQuery();
141    
142                            while (rs.next()) {
143                                    long structureId = rs.getLong("structureId");
144                                    String structureKey = rs.getString("structureKey");
145                                    String xsd = rs.getString("xsd");
146    
147                                    updateStructure(
148                                            structureId, StringUtil.toUpperCase(structureKey.trim()),
149                                            updateXSD(xsd));
150                            }
151                    }
152                    finally {
153                            DataAccess.cleanUp(con, ps, rs);
154                    }
155            }
156    
157            protected void updateTemplate(
158                            long templateId, String templateKey, String script)
159                    throws Exception {
160    
161                    Connection con = null;
162                    PreparedStatement ps = null;
163                    ResultSet rs = null;
164    
165                    try {
166                            con = DataAccess.getUpgradeOptimizedConnection();
167    
168                            ps = con.prepareStatement(
169                                    "update DDMTemplate set templateKey = ?, script = ? where " +
170                                            "templateId = ?");
171    
172                            ps.setString(1, templateKey);
173                            ps.setString(2, script);
174                            ps.setLong(3, templateId);
175    
176                            ps.executeUpdate();
177                    }
178                    finally {
179                            DataAccess.cleanUp(con, ps, rs);
180                    }
181            }
182    
183            protected void updateTemplates() throws Exception {
184                    Connection con = null;
185                    PreparedStatement ps = null;
186                    ResultSet rs = null;
187    
188                    try {
189                            con = DataAccess.getUpgradeOptimizedConnection();
190    
191                            ps = con.prepareStatement(
192                                    "select templateId, templateKey, script from DDMTemplate " +
193                                            "where language = 'xsd'");
194    
195                            rs = ps.executeQuery();
196    
197                            while (rs.next()) {
198                                    long templateId = rs.getLong("templateId");
199                                    String templateKey = rs.getString("templateKey");
200                                    String script = rs.getString("script");
201    
202                                    updateTemplate(
203                                            templateId, StringUtil.toUpperCase(templateKey.trim()),
204                                            updateXSD(script));
205                            }
206                    }
207                    finally {
208                            DataAccess.cleanUp(con, ps, rs);
209                    }
210            }
211    
212            protected String updateXSD(String xsd) throws Exception {
213                    Document document = SAXReaderUtil.read(xsd);
214    
215                    Element rootElement = document.getRootElement();
216    
217                    List<Element> dynamicElementElements = rootElement.elements(
218                            "dynamic-element");
219    
220                    for (Element dynamicElementElement : dynamicElementElements) {
221                            updateXSDDynamicElement(dynamicElementElement);
222                    }
223    
224                    return DDMXMLUtil.formatXML(document);
225            }
226    
227            protected void updateXSDDynamicElement(Element element) {
228                    Element metadataElement = element.element("meta-data");
229    
230                    updateMetadataElement(
231                            metadataElement,
232                            new String[] {
233                                    "multiple", "readOnly", "repeatable", "required", "showLabel",
234                                    "width",
235                            },
236                            new String[] {
237                                    "acceptFiles", "displayChildLabelAsValue", "fieldCssClass"
238                            });
239    
240                    List<Element> dynamicElementElements = element.elements(
241                            "dynamic-element");
242    
243                    for (Element dynamicElementElement : dynamicElementElements) {
244                            updateXSDDynamicElement(dynamicElementElement);
245                    }
246            }
247    
248            private static Log _log = LogFactoryUtil.getLog(
249                    UpgradeDynamicDataMapping.class);
250    
251    }