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.upgrade.UpgradeProcess;
27 import com.liferay.portal.kernel.upgrade.util.TempUpgradeColumnImpl;
28 import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
29 import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
30 import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
31 import com.liferay.portal.kernel.util.ArrayUtil;
32 import com.liferay.portal.model.ResourceConstants;
33 import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTable;
34 import com.liferay.portal.upgrade.v5_2_0.util.OrganizationTypeUpgradeColumnImpl;
35
36 import java.sql.Connection;
37 import java.sql.PreparedStatement;
38 import java.sql.ResultSet;
39 import java.sql.Types;
40
41
47 public class UpgradeOrganization extends UpgradeProcess {
48
49 protected void doUpgrade() throws Exception {
50 UpgradeColumn locationColumn = new TempUpgradeColumnImpl(
51 "location", new Integer(Types.BOOLEAN));
52
53 UpgradeColumn typeColumn = new OrganizationTypeUpgradeColumnImpl(
54 locationColumn);
55
56 Object[][] organizationColumns1 =
57 {{"location", new Integer(Types.BOOLEAN)}};
58 Object[][] organizationColumns2 =
59 OrganizationTable.TABLE_COLUMNS.clone();
60
61 Object[][] organizationColumns = ArrayUtil.append(
62 organizationColumns1, organizationColumns2);
63
64 UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
65 OrganizationTable.TABLE_NAME, organizationColumns, locationColumn,
66 typeColumn);
67
68 upgradeTable.updateTable();
69
70 updateLocationResources();
71 }
72
73 protected long getCodeId(long companyId, String name, int scope)
74 throws Exception {
75
76 long codeId = 0;
77
78 Connection con = null;
79 PreparedStatement ps = null;
80 ResultSet rs = null;
81
82 try {
83 con = DataAccess.getConnection();
84
85 ps = con.prepareStatement(
86 "select codeId from ResourceCode where companyId = ? and " +
87 "name = ? and scope = ?");
88
89 ps.setLong(1, companyId);
90 ps.setString(2, name);
91 ps.setInt(3, scope);
92
93 rs = ps.executeQuery();
94
95 if (rs.next()) {
96 codeId = rs.getLong("codeId");
97 }
98 }
99 finally {
100 DataAccess.cleanUp(con, ps, rs);
101 }
102
103 return codeId;
104 }
105
106 protected void updateCodeId(long companyId, int scope) throws Exception {
107 long oldCodeId = getCodeId(
108 companyId, "com.liferay.portal.model.Location", scope);
109 long newCodeId = getCodeId(
110 companyId, "com.liferay.portal.model.Organization", scope);
111
112 runSQL(
113 "update Resource_ set codeId = " + newCodeId + " where codeId = " +
114 oldCodeId);
115
116 runSQL("delete from ResourceCode where codeId = " + oldCodeId);
117 }
118
119 protected void updateLocationResources() throws Exception {
120 Connection con = null;
121 PreparedStatement ps = null;
122 ResultSet rs = null;
123
124 try {
125 con = DataAccess.getConnection();
126
127 ps = con.prepareStatement(_GET_COMPANY_IDS);
128
129 rs = ps.executeQuery();
130
131 while (rs.next()) {
132 long companyId = rs.getLong("companyId");
133
134 for (int scope : ResourceConstants.SCOPES) {
135 updateCodeId(companyId, scope);
136 }
137 }
138 }
139 finally {
140 DataAccess.cleanUp(con, ps, rs);
141 }
142 }
143
144 private static final String _GET_COMPANY_IDS =
145 "select companyId from Company";
146
147 }