1
22
23 package com.liferay.portal.upgrade.v5_2_0;
24
25 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.util.ArrayUtil;
29 import com.liferay.portal.model.ResourceConstants;
30 import com.liferay.portal.model.impl.OrganizationImpl;
31 import com.liferay.portal.upgrade.UpgradeException;
32 import com.liferay.portal.upgrade.UpgradeProcess;
33 import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
34 import com.liferay.portal.upgrade.util.TempUpgradeColumnImpl;
35 import com.liferay.portal.upgrade.util.UpgradeColumn;
36 import com.liferay.portal.upgrade.util.UpgradeTable;
37 import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTypeUpgradeColumnImpl;
38
39 import java.sql.Connection;
40 import java.sql.PreparedStatement;
41 import java.sql.ResultSet;
42 import java.sql.Types;
43
44
50 public class UpgradeOrganization extends UpgradeProcess {
51
52 public void upgrade() throws UpgradeException {
53 _log.info("Upgrading");
54
55 try {
56 doUpgrade();
57 }
58 catch (Exception e) {
59 throw new UpgradeException(e);
60 }
61 }
62
63 protected void doUpgrade() throws Exception {
64 UpgradeColumn locationColumn = new TempUpgradeColumnImpl(
65 "location", new Integer(Types.BOOLEAN));
66
67 UpgradeColumn typeColumn = new OrganizationTypeUpgradeColumnImpl(
68 locationColumn);
69
70 Object[][] organizationColumns1 =
71 {{"location", new Integer(Types.BOOLEAN)}};
72 Object[][] organizationColumns2 =
73 OrganizationImpl.TABLE_COLUMNS.clone();
74
75 Object[][] organizationColumns = ArrayUtil.append(
76 organizationColumns1, organizationColumns2);
77
78 UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
79 OrganizationImpl.TABLE_NAME, organizationColumns,
80 locationColumn, typeColumn);
81
82 upgradeTable.updateTable();
83
84 updateLocationResources();
85 }
86
87 protected void updateCodeId(long companyId, int scope) throws Exception {
88 Connection con = null;
89 PreparedStatement ps = null;
90 ResultSet rs = null;
91
92 try {
93 con = DataAccess.getConnection();
94
95 ps = con.prepareStatement(
96 "select codeId from ResourceCode where companyId = ? and " +
97 "name = ? and scope = ?");
98
99 ps.setLong(1, companyId);
100 ps.setString(2, "com.liferay.portal.model.Location");
101 ps.setInt(3, scope);
102
103 rs = ps.executeQuery();
104
105 long oldCodeId = 0;
106
107 if (rs.next()) {
108 oldCodeId = rs.getLong("codeId");
109 }
110
111 ps.setString(2, "com.liferay.portal.model.Organization");
112
113 rs = ps.executeQuery();
114
115 long newCodeId = 0;
116
117 if (rs.next()) {
118 newCodeId = rs.getLong("codeId");
119 }
120
121 ps.close();
122
123 ps = con.prepareStatement(
124 "update Resource_ set codeId = ? where codeId = ?");
125
126 ps.setLong(1, oldCodeId);
127 ps.setLong(2, newCodeId);
128
129 ps.executeUpdate();
130
131 ps.close();
132
133 ps = con.prepareStatement(
134 "delete from ResourceCode where codeId = " + oldCodeId);
135
136 ps.executeUpdate();
137 }
138 finally {
139 DataAccess.cleanUp(con, ps, rs);
140 }
141 }
142
143 protected void updateLocationResources() throws Exception {
144 Connection con = null;
145 PreparedStatement ps = null;
146 ResultSet rs = null;
147
148 try {
149 con = DataAccess.getConnection();
150
151 ps = con.prepareStatement(_GET_COMPANY_IDS);
152
153 rs = ps.executeQuery();
154
155 while (rs.next()) {
156 long companyId = rs.getLong("companyId");
157
158 for (int scope : ResourceConstants.SCOPES) {
159 updateCodeId(companyId, scope);
160 }
161 }
162 }
163 finally {
164 DataAccess.cleanUp(con, ps, rs);
165 }
166 }
167
168 private static final String _GET_COMPANY_IDS =
169 "select companyId from Company";
170
171 private static Log _log = LogFactoryUtil.getLog(UpgradeOrganization.class);
172
173 }