001    /**
002     * Copyright (c) 2000-present 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.v7_0_1;
016    
017    import com.liferay.document.library.kernel.model.DLFileEntry;
018    import com.liferay.document.library.kernel.util.RawMetadataProcessor;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.util.LoggingTimer;
021    import com.liferay.portal.kernel.util.PortalUtil;
022    
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Sergio Gonz??lez
028     */
029    public class UpgradeDocumentLibrary extends UpgradeProcess {
030    
031            protected long addRawMetadataProcessorClassName() throws Exception {
032                    long classNameId = PortalUtil.getClassNameId(
033                            RawMetadataProcessor.class);
034    
035                    if (classNameId != 0) {
036                            return classNameId;
037                    }
038    
039                    try (PreparedStatement ps = connection.prepareStatement(
040                                    "insert into ClassName_ (mvccVersion, classNameId, value) " +
041                                            "values (?, ?, ?)")) {
042    
043                            classNameId = increment();
044    
045                            ps.setLong(1, 0);
046                            ps.setLong(2, classNameId);
047                            ps.setString(3, RawMetadataProcessor.class.getName());
048    
049                            ps.executeUpdate();
050                    }
051    
052                    return classNameId;
053            }
054    
055            @Override
056            protected void doUpgrade() throws Exception {
057                    updateTikaRawMetadataDDMStructure();
058    
059                    updateTikaRawMetadataFileEntryMetadata();
060            }
061    
062            protected long getDDMStructureId(String structureKey, long classNameId)
063                    throws Exception {
064    
065                    try (LoggingTimer loggingTimer = new LoggingTimer();
066                            PreparedStatement ps = connection.prepareStatement(
067                                    "select structureId from DDMStructure where structureKey = ? " +
068                                            "and classNameId = ?")) {
069    
070                            ps.setString(1, structureKey);
071                            ps.setLong(2, classNameId);
072    
073                            ResultSet rs = ps.executeQuery();
074    
075                            if (rs.next()) {
076                                    return rs.getLong("structureId");
077                            }
078    
079                            return 0;
080                    }
081            }
082    
083            protected void updateTikaRawMetadataDDMStructure() throws Exception {
084                    long classNameId = addRawMetadataProcessorClassName();
085    
086                    long ddmStructureId = getDDMStructureId("TIKARAWMETADATA", classNameId);
087    
088                    if (ddmStructureId != 0) {
089                            return;
090                    }
091    
092                    try (PreparedStatement ps = connection.prepareStatement(
093                                    "update DDMStructure set classNameId = ? where structureKey " +
094                                            "= ?")) {
095    
096                            ps.setLong(1, classNameId);
097                            ps.setString(2, "TIKARAWMETADATA");
098    
099                            ps.execute();
100                    }
101            }
102    
103            protected void updateTikaRawMetadataFileEntryMetadata() throws Exception {
104                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
105                            long oldDDMStructureId = getDDMStructureId(
106                                    "TIKARAWMETADATA",
107                                    PortalUtil.getClassNameId(DLFileEntry.class));
108    
109                            if (oldDDMStructureId == 0) {
110                                    return;
111                            }
112    
113                            long newDDMStructureId = getDDMStructureId(
114                                    "TIKARAWMETADATA",
115                                    PortalUtil.getClassNameId(RawMetadataProcessor.class));
116    
117                            if (newDDMStructureId == 0) {
118                                    return;
119                            }
120    
121                            try (PreparedStatement ps = connection.prepareStatement(
122                                            "update DLFileEntryMetadata set DDMStructureId = ? where " +
123                                                    "DDMStructureId = ?")) {
124    
125                                    ps.setLong(1, newDDMStructureId);
126                                    ps.setLong(2, oldDDMStructureId);
127    
128                                    ps.execute();
129                            }
130                    }
131            }
132    
133    }