1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v4_3_0.util;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.upgrade.util.TempUpgradeColumnImpl;
27  import com.liferay.portal.upgrade.util.ValueMapper;
28  import com.liferay.portal.util.PortletKeys;
29  
30  import java.sql.Types;
31  
32  /**
33   * <a href="PrefsOwnerIdUpgradeColumnImpl.java.html"><b><i>View Source</i></b>
34   * </a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class PrefsOwnerIdUpgradeColumnImpl extends TempUpgradeColumnImpl {
40  
41      public PrefsOwnerIdUpgradeColumnImpl(
42          ValueMapper companyIdMapper, ValueMapper groupIdMapper,
43          ValueMapper userIdMapper) {
44  
45          super("ownerId", new Integer(Types.VARCHAR));
46  
47          _companyIdMapper = companyIdMapper;
48          _groupIdMapper = groupIdMapper;
49          _userIdMapper = userIdMapper;
50      }
51  
52      public Integer getNewColumnType(Integer defaultType) {
53          return new Integer(Types.BIGINT);
54      }
55  
56      public Object getNewValue(Object oldValue) throws Exception {
57          _ownerType = null;
58          _oldGroupId = null;
59          _newGroupId = null;
60          _privateLayout = null;
61  
62          String ownerId = (String)oldValue;
63  
64          int ownerType = 0;
65  
66          if (ownerId.startsWith("PUB.") || ownerId.startsWith("PRI.")) {
67              _privateLayout = Boolean.valueOf(ownerId.startsWith("PRI."));
68  
69              int pos = ownerId.indexOf(".USER.");
70  
71              if (pos != -1) {
72                  _oldGroupId = new Long(GetterUtil.getLong(
73                      ownerId.substring(4, pos)));
74                  _newGroupId = (Long)_groupIdMapper.getNewValue(_oldGroupId);
75  
76                  ownerId = String.valueOf(_userIdMapper.getNewValue(
77                      ownerId.substring(pos + 6, ownerId.length())));
78                  ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
79              }
80              else {
81                  _oldGroupId = new Long(GetterUtil.getLong(
82                      ownerId.substring(4, ownerId.length())));
83                  _newGroupId = (Long)_groupIdMapper.getNewValue(_oldGroupId);
84  
85                  ownerId = String.valueOf(PortletKeys.PREFS_OWNER_ID_DEFAULT);
86                  ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
87              }
88          }
89          else if (ownerId.startsWith("COMPANY.")) {
90              ownerId = String.valueOf(_companyIdMapper.getNewValue(
91                  ownerId.substring(8, ownerId.length())));
92              ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
93          }
94          else if (ownerId.startsWith("GROUP.")) {
95              Long groupId = new Long(GetterUtil.getLong(
96                  ownerId.substring(6, ownerId.length())));
97  
98              groupId = (Long)_groupIdMapper.getNewValue(groupId);
99  
100             ownerId = String.valueOf(groupId);
101             ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
102         }
103         else if (ownerId.startsWith("USER.")) {
104             ownerId = String.valueOf(_userIdMapper.getNewValue(
105                 ownerId.substring(5, ownerId.length())));
106             ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
107         }
108         else {
109             ownerId = String.valueOf(_userIdMapper.getNewValue(ownerId));
110             ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
111         }
112 
113         _ownerType = new Integer(ownerType);
114 
115         return ownerId;
116     }
117 
118     public Integer getOwnerType() {
119         return _ownerType;
120     }
121 
122     public Long getOldGroupId() {
123         return _oldGroupId;
124     }
125 
126     public Long getNewGroupId() {
127         return _newGroupId;
128     }
129 
130     public Boolean isPrivateLayout() {
131         return _privateLayout;
132     }
133 
134     private ValueMapper _companyIdMapper;
135     private ValueMapper _groupIdMapper;
136     private ValueMapper _userIdMapper;
137     private Integer _ownerType;
138     private Long _oldGroupId;
139     private Long _newGroupId;
140     private Boolean _privateLayout;
141 
142 }