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