1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v4_3_0.util;
16  
17  import com.liferay.portal.kernel.upgrade.util.TempUpgradeColumnImpl;
18  import com.liferay.portal.kernel.upgrade.util.ValueMapper;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.util.PortletKeys;
21  
22  import java.sql.Types;
23  
24  /**
25   * <a href="PrefsOwnerIdUpgradeColumnImpl.java.html"><b><i>View Source</i></b>
26   * </a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class PrefsOwnerIdUpgradeColumnImpl extends TempUpgradeColumnImpl {
31  
32      public PrefsOwnerIdUpgradeColumnImpl(
33          ValueMapper companyIdMapper, ValueMapper groupIdMapper,
34          ValueMapper userIdMapper) {
35  
36          super("ownerId", new Integer(Types.VARCHAR));
37  
38          _companyIdMapper = companyIdMapper;
39          _groupIdMapper = groupIdMapper;
40          _userIdMapper = userIdMapper;
41      }
42  
43      public Integer getNewColumnType(Integer defaultType) {
44          return new Integer(Types.BIGINT);
45      }
46  
47      public Object getNewValue(Object oldValue) throws Exception {
48          _ownerType = null;
49          _oldGroupId = null;
50          _newGroupId = null;
51          _privateLayout = null;
52  
53          String ownerId = (String)oldValue;
54  
55          int ownerType = 0;
56  
57          if (ownerId.startsWith("PUB.") || ownerId.startsWith("PRI.")) {
58              _privateLayout = Boolean.valueOf(ownerId.startsWith("PRI."));
59  
60              int pos = ownerId.indexOf(".USER.");
61  
62              if (pos != -1) {
63                  _oldGroupId = new Long(GetterUtil.getLong(
64                      ownerId.substring(4, pos)));
65                  _newGroupId = (Long)_groupIdMapper.getNewValue(_oldGroupId);
66  
67                  ownerId = String.valueOf(_userIdMapper.getNewValue(
68                      ownerId.substring(pos + 6, ownerId.length())));
69                  ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
70              }
71              else {
72                  _oldGroupId = new Long(GetterUtil.getLong(
73                      ownerId.substring(4, ownerId.length())));
74                  _newGroupId = (Long)_groupIdMapper.getNewValue(_oldGroupId);
75  
76                  ownerId = String.valueOf(PortletKeys.PREFS_OWNER_ID_DEFAULT);
77                  ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
78              }
79          }
80          else if (ownerId.startsWith("COMPANY.")) {
81              ownerId = String.valueOf(_companyIdMapper.getNewValue(
82                  ownerId.substring(8, ownerId.length())));
83              ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
84          }
85          else if (ownerId.startsWith("GROUP.")) {
86              Long groupId = new Long(GetterUtil.getLong(
87                  ownerId.substring(6, ownerId.length())));
88  
89              groupId = (Long)_groupIdMapper.getNewValue(groupId);
90  
91              ownerId = String.valueOf(groupId);
92              ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
93          }
94          else if (ownerId.startsWith("USER.")) {
95              ownerId = String.valueOf(_userIdMapper.getNewValue(
96                  ownerId.substring(5, ownerId.length())));
97              ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
98          }
99          else {
100             ownerId = String.valueOf(_userIdMapper.getNewValue(ownerId));
101             ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
102         }
103 
104         _ownerType = new Integer(ownerType);
105 
106         return ownerId;
107     }
108 
109     public Integer getOwnerType() {
110         return _ownerType;
111     }
112 
113     public Long getOldGroupId() {
114         return _oldGroupId;
115     }
116 
117     public Long getNewGroupId() {
118         return _newGroupId;
119     }
120 
121     public Boolean isPrivateLayout() {
122         return _privateLayout;
123     }
124 
125     private ValueMapper _companyIdMapper;
126     private ValueMapper _groupIdMapper;
127     private ValueMapper _userIdMapper;
128     private Integer _ownerType;
129     private Long _oldGroupId;
130     private Long _newGroupId;
131     private Boolean _privateLayout;
132 
133 }