1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v6_0_0;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portlet.PortletPreferencesImpl;
21  import com.liferay.portlet.PortletPreferencesSerializer;
22  
23  import java.sql.Connection;
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  
27  /**
28   * <a href="UpgradeAssetPublisher.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Julio Camarero
31   */
32  public class UpgradeAssetPublisher extends UpgradeProcess {
33  
34      protected void deletePortletPreferences(long portletPreferencesId)
35          throws Exception {
36  
37          runSQL(
38              "delete from PortletPreferences where portletPreferencesId = " +
39                  portletPreferencesId);
40      }
41  
42      protected void doUpgrade() throws Exception {
43          updatePortletPreferences();
44      }
45  
46      protected Object[] getLayout(long plid) throws Exception {
47          Object[] layout = null;
48  
49          Connection con = null;
50          PreparedStatement ps = null;
51          ResultSet rs = null;
52  
53          try {
54              con = DataAccess.getConnection();
55  
56              ps = con.prepareStatement(_GET_LAYOUT);
57  
58              ps.setLong(1, plid);
59  
60              rs = ps.executeQuery();
61  
62              while (rs.next()) {
63                  long companyId = rs.getLong("companyId");
64  
65                  layout = new Object[] {companyId};
66              }
67          }
68          finally {
69              DataAccess.cleanUp(con, ps, rs);
70          }
71  
72          return layout;
73      }
74  
75      protected void updatePortletPreferences() throws Exception {
76          Connection con = null;
77          PreparedStatement ps = null;
78          ResultSet rs = null;
79  
80          try {
81              con = DataAccess.getConnection();
82  
83              ps = con.prepareStatement(
84                  "select portletPreferencesId, ownerId, ownerType, plid, " +
85                      "portletId, preferences from PortletPreferences where " +
86                          "portletId like '101_INSTANCE_%' ");
87  
88              rs = ps.executeQuery();
89  
90              while (rs.next()) {
91                  long portletPreferencesId = rs.getLong("portletPreferencesId");
92                  long ownerId = rs.getLong("ownerId");
93                  int ownerType = rs.getInt("ownerType");
94                  long plid = rs.getLong("plid");
95                  String portletId = rs.getString("portletId");
96                  String preferences = rs.getString("preferences");
97  
98                  Object[] layout = getLayout(plid);
99  
100                 if (layout != null) {
101                     long companyId = (Long)layout[0];
102 
103                     String newPreferences = upgradePreferences(
104                         companyId, ownerId, ownerType, plid, portletId,
105                         preferences);
106 
107                     updatePortletPreferences(
108                         portletPreferencesId, newPreferences);
109                 }
110                 else {
111                     deletePortletPreferences(portletPreferencesId);
112                 }
113             }
114         }
115         finally {
116             DataAccess.cleanUp(con, ps, rs);
117         }
118     }
119 
120     protected void updatePortletPreferences(
121             long portletPreferencesId, String preferences)
122         throws Exception {
123 
124         Connection con = null;
125         PreparedStatement ps = null;
126 
127         try {
128             con = DataAccess.getConnection();
129 
130             ps = con.prepareStatement(
131                 "update PortletPreferences set preferences = ? where " +
132                     "portletPreferencesId = " + portletPreferencesId);
133 
134             ps.setString(1, preferences);
135 
136             ps.executeUpdate();
137         }
138         finally {
139             DataAccess.cleanUp(con, ps);
140         }
141     }
142 
143     protected String upgradePreferences(
144             long companyId, long ownerId, int ownerType, long plid,
145             String portletId, String xml)
146         throws Exception {
147 
148         PortletPreferencesImpl preferences =
149             PortletPreferencesSerializer.fromXML(
150                 companyId, ownerId, ownerType, plid, portletId, xml);
151 
152         long layoutId = GetterUtil.getLong(
153             preferences.getValue("lfr-scope-layout-id", null));
154 
155         preferences.reset("lfr-scope-layout-id");
156 
157         if (layoutId != 0) {
158             preferences.setValues(
159                 "scope-ids", new String[] {"Layout_" + layoutId});
160 
161             preferences.setValue("default-scope", Boolean.FALSE.toString());
162         }
163 
164         long classNameId = GetterUtil.getLong(
165             preferences.getValue("class-name-id", null));
166 
167         preferences.reset("class-name-id");
168 
169         if (classNameId != 0) {
170             preferences.setValues(
171                 "class-name-ids", new String[] {String.valueOf(classNameId)});
172 
173             preferences.setValue("any-asset-type", Boolean.FALSE.toString());
174         }
175 
176         boolean andOperator = GetterUtil.getBoolean(
177             preferences.getValue("and-operator", null));
178 
179         preferences.reset("and-operator");
180 
181         String[] assetTagNames = preferences.getValues("entries", null);
182         String[] notAssetTagNames = preferences.getValues("not-entries", null);
183 
184         int i = 0;
185 
186         if (assetTagNames != null) {
187             preferences.reset("entries");
188 
189             preferences.setValue("queryContains" + i, Boolean.TRUE.toString());
190             preferences.setValue(
191                 "queryAndOperator" + i, String.valueOf(andOperator));
192             preferences.setValue("queryName" + i, "assetTags");
193             preferences.setValues("queryValues" + i, assetTagNames);
194 
195             i++;
196         }
197 
198         if (notAssetTagNames != null) {
199             preferences.reset("not-entries");
200 
201             preferences.setValue("queryContains" + i, Boolean.FALSE.toString());
202             preferences.setValue(
203                 "queryAndOperator" + i, String.valueOf(andOperator));
204             preferences.setValue("queryName" + i, "assetTags");
205             preferences.setValues("queryValues" + i, notAssetTagNames);
206 
207             i++;
208         }
209 
210         return PortletPreferencesSerializer.toXML(preferences);
211     }
212 
213     private static final String _GET_LAYOUT =
214         "select * from Layout where plid = ?";
215 
216 }