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.DefaultUpgradeTableImpl;
27  import com.liferay.portal.upgrade.util.PKUpgradeColumnImpl;
28  import com.liferay.portal.upgrade.util.SwapUpgradeColumnImpl;
29  import com.liferay.portal.upgrade.util.UpgradeColumn;
30  import com.liferay.portal.upgrade.util.UpgradeTable;
31  import com.liferay.portal.upgrade.util.ValueMapper;
32  import com.liferay.portal.upgrade.v4_3_0.util.AvailableMappersUtil;
33  import com.liferay.portal.upgrade.v4_3_0.util.PollsChoiceIdUpgradeColumnImpl;
34  import com.liferay.portal.upgrade.v4_3_0.util.PollsVoteChoiceIdUpgradeColumnImpl;
35  import com.liferay.portlet.polls.model.impl.PollsChoiceImpl;
36  import com.liferay.portlet.polls.model.impl.PollsQuestionImpl;
37  import com.liferay.portlet.polls.model.impl.PollsVoteImpl;
38  
39  import java.sql.Types;
40  
41  /**
42   * <a href="UpgradePolls.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class UpgradePolls extends UpgradeProcess {
48  
49      public void upgrade() throws UpgradeException {
50          _log.info("Upgrading");
51  
52          try {
53              doUpgrade();
54          }
55          catch (Exception e) {
56              throw new UpgradeException(e);
57          }
58      }
59  
60      protected void doUpgrade() throws Exception {
61  
62          // PollsQuestion
63  
64          UpgradeColumn upgradeGroupIdColumn = new SwapUpgradeColumnImpl(
65              "groupId", AvailableMappersUtil.getGroupIdMapper());
66  
67          UpgradeColumn upgradeUserIdColumn = new SwapUpgradeColumnImpl(
68              "userId", new Integer(Types.VARCHAR),
69              AvailableMappersUtil.getUserIdMapper());
70  
71          PKUpgradeColumnImpl upgradePKColumn = new PKUpgradeColumnImpl(
72              "questionId", true);
73  
74          UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
75              PollsQuestionImpl.TABLE_NAME, PollsQuestionImpl.TABLE_COLUMNS,
76              upgradePKColumn, upgradeGroupIdColumn, upgradeUserIdColumn);
77  
78          upgradeTable.setCreateSQL(PollsQuestionImpl.TABLE_SQL_CREATE);
79  
80          upgradeTable.updateTable();
81  
82          ValueMapper questionIdMapper = upgradePKColumn.getValueMapper();
83  
84          AvailableMappersUtil.setPollsQuestionIdMapper(questionIdMapper);
85  
86          UpgradeColumn upgradeQuestionIdColumn = new SwapUpgradeColumnImpl(
87              "questionId", questionIdMapper);
88  
89          // PollsChoice
90  
91          PKUpgradeColumnImpl upgradeChoiceId =
92              new PollsChoiceIdUpgradeColumnImpl(upgradeQuestionIdColumn);
93  
94          upgradeTable = new DefaultUpgradeTableImpl(
95              PollsChoiceImpl.TABLE_NAME, PollsChoiceImpl.TABLE_COLUMNS,
96              upgradeQuestionIdColumn, upgradeChoiceId);
97  
98          upgradeTable.setCreateSQL(PollsChoiceImpl.TABLE_SQL_CREATE);
99  
100         upgradeTable.updateTable();
101 
102         ValueMapper choiceIdMapper = upgradeChoiceId.getValueMapper();
103 
104         // PollsVote
105 
106         UpgradeColumn upgradeVoteChoiceIdColumn =
107             new PollsVoteChoiceIdUpgradeColumnImpl(
108                 upgradeQuestionIdColumn, choiceIdMapper);
109 
110         upgradeTable = new DefaultUpgradeTableImpl(
111             PollsVoteImpl.TABLE_NAME, PollsVoteImpl.TABLE_COLUMNS,
112             new PKUpgradeColumnImpl("voteId", false), upgradeUserIdColumn,
113             upgradeQuestionIdColumn, upgradeVoteChoiceIdColumn);
114 
115         upgradeTable.setCreateSQL(PollsVoteImpl.TABLE_SQL_CREATE);
116 
117         upgradeTable.updateTable();
118     }
119 
120     private static Log _log = LogFactoryUtil.getLog(UpgradePolls.class);
121 
122 }