1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.v4_3_0;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.upgrade.UpgradeException;
25  import com.liferay.portal.upgrade.UpgradeProcess;
26  import com.liferay.portal.upgrade.util.DefaultPKMapper;
27  import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
28  import com.liferay.portal.upgrade.util.LazyPKUpgradeColumnImpl;
29  import com.liferay.portal.upgrade.util.PKUpgradeColumnImpl;
30  import com.liferay.portal.upgrade.util.SwapUpgradeColumnImpl;
31  import com.liferay.portal.upgrade.util.UpgradeColumn;
32  import com.liferay.portal.upgrade.util.UpgradeTable;
33  import com.liferay.portal.upgrade.util.ValueMapper;
34  import com.liferay.portal.upgrade.v4_3_0.util.AvailableMappersUtil;
35  import com.liferay.portal.upgrade.v4_3_0.util.ClassNameIdUpgradeColumnImpl;
36  import com.liferay.portal.upgrade.v4_3_0.util.ClassPKContainer;
37  import com.liferay.portal.upgrade.v4_3_0.util.ClassPKUpgradeColumnImpl;
38  import com.liferay.portal.upgrade.v4_3_0.util.MBCategoryIdUpgradeColumnImpl;
39  import com.liferay.portal.upgrade.v4_3_0.util.MBMessageAttachmentsUpgradeColumnImpl;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portlet.blogs.model.BlogsEntry;
42  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
43  import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
44  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
45  import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
46  import com.liferay.portlet.messageboards.model.impl.MBStatsUserImpl;
47  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
48  
49  import java.sql.Types;
50  
51  import java.util.HashMap;
52  import java.util.Map;
53  
54  /**
55   * <a href="UpgradeMessageBoards.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class UpgradeMessageBoards extends UpgradeProcess {
61  
62      public void upgrade() throws UpgradeException {
63          _log.info("Upgrading");
64  
65          try {
66              doUpgrade();
67          }
68          catch (Exception e) {
69              throw new UpgradeException(e);
70          }
71      }
72  
73      protected void doUpgrade() throws Exception {
74  
75          // MBCategory
76  
77          UpgradeColumn upgradeCompanyIdColumn = new SwapUpgradeColumnImpl(
78              "companyId", new Integer(Types.VARCHAR),
79              AvailableMappersUtil.getCompanyIdMapper());
80  
81          UpgradeColumn upgradeGroupIdColumn = new SwapUpgradeColumnImpl(
82              "groupId", AvailableMappersUtil.getGroupIdMapper());
83  
84          UpgradeColumn upgradeUserIdColumn = new SwapUpgradeColumnImpl(
85              "userId", new Integer(Types.VARCHAR),
86              AvailableMappersUtil.getUserIdMapper());
87  
88          PKUpgradeColumnImpl upgradePKColumn =
89              new MBCategoryIdUpgradeColumnImpl();
90  
91          UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
92              MBCategoryImpl.TABLE_NAME, MBCategoryImpl.TABLE_COLUMNS,
93              upgradePKColumn, upgradeGroupIdColumn, upgradeUserIdColumn);
94  
95          upgradeTable.setCreateSQL(MBCategoryImpl.TABLE_SQL_CREATE);
96  
97          upgradeTable.updateTable();
98  
99          ValueMapper categoryIdMapper = new DefaultPKMapper(
100             upgradePKColumn.getValueMapper());
101 
102         AvailableMappersUtil.setMBCategoryIdMapper(categoryIdMapper);
103 
104         UpgradeColumn upgradeParentCategoryIdColumn = new SwapUpgradeColumnImpl(
105             "parentCategoryId", categoryIdMapper);
106 
107         upgradeTable = new DefaultUpgradeTableImpl(
108             MBCategoryImpl.TABLE_NAME, MBCategoryImpl.TABLE_COLUMNS,
109             upgradeParentCategoryIdColumn);
110 
111         upgradeTable.updateTable();
112 
113         UpgradeColumn upgradeCategoryIdColumn = new SwapUpgradeColumnImpl(
114             "categoryId", categoryIdMapper);
115 
116         // MBMessage
117 
118         upgradePKColumn = new PKUpgradeColumnImpl("messageId", true);
119 
120         PKUpgradeColumnImpl upgradeThreadIdPKColumn =
121             new LazyPKUpgradeColumnImpl("threadId");
122 
123         UpgradeColumn upgradeAttachmentsColumn =
124             new MBMessageAttachmentsUpgradeColumnImpl(
125                 upgradePKColumn, upgradeCompanyIdColumn,
126             upgradeThreadIdPKColumn);
127 
128         upgradeTable = new DefaultUpgradeTableImpl(
129             MBMessageImpl.TABLE_NAME, MBMessageImpl.TABLE_COLUMNS,
130             upgradePKColumn, upgradeCompanyIdColumn, upgradeUserIdColumn,
131             upgradeCategoryIdColumn, upgradeThreadIdPKColumn,
132             upgradeAttachmentsColumn);
133 
134         upgradeTable.setCreateSQL(MBMessageImpl.TABLE_SQL_CREATE);
135 
136         upgradeTable.updateTable();
137 
138         ValueMapper messageIdMapper = new DefaultPKMapper(
139             upgradePKColumn.getValueMapper());
140 
141         AvailableMappersUtil.setMBMessageIdMapper(messageIdMapper);
142 
143         ValueMapper threadIdMapper = upgradeThreadIdPKColumn.getValueMapper();
144 
145         AvailableMappersUtil.setMBThreadIdMapper(threadIdMapper);
146 
147         UpgradeColumn upgradeParentMessageIdColumn = new SwapUpgradeColumnImpl(
148             "parentMessageId", messageIdMapper);
149 
150         upgradeTable = new DefaultUpgradeTableImpl(
151             MBMessageImpl.TABLE_NAME, MBMessageImpl.TABLE_COLUMNS,
152             upgradeParentMessageIdColumn);
153 
154         upgradeTable.updateTable();
155 
156         UpgradeColumn upgradeMessageIdColumn = new SwapUpgradeColumnImpl(
157             "messageId", messageIdMapper);
158 
159         UpgradeColumn upgradeRootMessageIdColumn = new SwapUpgradeColumnImpl(
160             "rootMessageId", messageIdMapper);
161 
162         // MBMessageFlag
163 
164         upgradePKColumn = new PKUpgradeColumnImpl("messageFlagId", true);
165 
166         upgradeTable = new DefaultUpgradeTableImpl(
167             MBMessageFlagImpl.TABLE_NAME, MBMessageFlagImpl.TABLE_COLUMNS,
168             upgradePKColumn, upgradeUserIdColumn, upgradeMessageIdColumn);
169 
170         upgradeTable.setCreateSQL(MBMessageFlagImpl.TABLE_SQL_CREATE);
171 
172         upgradeTable.updateTable();
173 
174         // MBStatsUser
175 
176         upgradeTable = new DefaultUpgradeTableImpl(
177             MBStatsUserImpl.TABLE_NAME, MBStatsUserImpl.TABLE_COLUMNS,
178             new PKUpgradeColumnImpl("statsUserId", false),
179             upgradeGroupIdColumn, upgradeUserIdColumn);
180 
181         upgradeTable.setCreateSQL(MBStatsUserImpl.TABLE_SQL_CREATE);
182 
183         upgradeTable.updateTable();
184 
185         // MBThread
186 
187         UpgradeColumn upgradeThreadIdColumn = new SwapUpgradeColumnImpl(
188             "threadId", threadIdMapper);
189 
190         UpgradeColumn upgradeLastPostByUserIdColumn = new SwapUpgradeColumnImpl(
191             "lastPostByUserId", new Integer(Types.VARCHAR),
192             AvailableMappersUtil.getUserIdMapper());
193 
194         upgradeTable = new DefaultUpgradeTableImpl(
195             MBThreadImpl.TABLE_NAME, MBThreadImpl.TABLE_COLUMNS,
196             upgradeThreadIdColumn, upgradeCategoryIdColumn,
197             upgradeRootMessageIdColumn, upgradeLastPostByUserIdColumn);
198 
199         upgradeTable.setCreateSQL(MBThreadImpl.TABLE_SQL_CREATE);
200 
201         upgradeTable.updateTable();
202 
203         // MBDiscussion
204 
205         ClassNameIdUpgradeColumnImpl classNameIdColumn =
206             new ClassNameIdUpgradeColumnImpl();
207 
208         Map<Long, ClassPKContainer> classPKContainers =
209             new HashMap<Long, ClassPKContainer>();
210 
211         classPKContainers.put(
212             new Long(PortalUtil.getClassNameId(BlogsEntry.class.getName())),
213             new ClassPKContainer(
214                 AvailableMappersUtil.getBlogsEntryIdMapper(), true));
215 
216         UpgradeColumn upgradeClassPKColumn = new ClassPKUpgradeColumnImpl(
217             classNameIdColumn, classPKContainers);
218 
219         upgradeTable = new DefaultUpgradeTableImpl(
220             MBDiscussionImpl.TABLE_NAME, MBDiscussionImpl.TABLE_COLUMNS,
221             new PKUpgradeColumnImpl("discussionId", false),
222             classNameIdColumn, upgradeClassPKColumn, upgradeThreadIdColumn);
223 
224         upgradeTable.setCreateSQL(MBDiscussionImpl.TABLE_SQL_CREATE);
225 
226         upgradeTable.updateTable();
227     }
228 
229     private static Log _log = LogFactoryUtil.getLog(UpgradeMessageBoards.class);
230 
231 }