1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PrefsOwnerIdUpgradeColumnImpl extends TempUpgradeColumnImpl {
39  
40      public PrefsOwnerIdUpgradeColumnImpl(
41          ValueMapper companyIdMapper, ValueMapper groupIdMapper,
42          ValueMapper userIdMapper) {
43  
44          super("ownerId", new Integer(Types.VARCHAR));
45  
46          _companyIdMapper = companyIdMapper;
47          _groupIdMapper = groupIdMapper;
48          _userIdMapper = userIdMapper;
49      }
50  
51      public Integer getNewColumnType(Integer defaultType) {
52          return new Integer(Types.BIGINT);
53      }
54  
55      public Object getNewValue(Object oldValue) throws Exception {
56          _ownerType = null;
57          _oldGroupId = null;
58          _newGroupId = null;
59          _privateLayout = null;
60  
61          String ownerId = (String)oldValue;
62  
63          int ownerType = 0;
64  
65          if (ownerId.startsWith("PUB.") || ownerId.startsWith("PRI.")) {
66              _privateLayout = Boolean.valueOf(ownerId.startsWith("PRI."));
67  
68              int pos = ownerId.indexOf(".USER.");
69  
70              if (pos != -1) {
71                  _oldGroupId = new Long(GetterUtil.getLong(
72                      ownerId.substring(4, pos)));
73                  _newGroupId = (Long)_groupIdMapper.getNewValue(_oldGroupId);
74  
75                  ownerId = String.valueOf(_userIdMapper.getNewValue(
76                      ownerId.substring(pos + 6, ownerId.length())));
77                  ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
78              }
79              else {
80                  _oldGroupId = new Long(GetterUtil.getLong(
81                      ownerId.substring(4, ownerId.length())));
82                  _newGroupId = (Long)_groupIdMapper.getNewValue(_oldGroupId);
83  
84                  ownerId = String.valueOf(PortletKeys.PREFS_OWNER_ID_DEFAULT);
85                  ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
86              }
87          }
88          else if (ownerId.startsWith("COMPANY.")) {
89              ownerId = String.valueOf(_companyIdMapper.getNewValue(
90                  ownerId.substring(8, ownerId.length())));
91              ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
92          }
93          else if (ownerId.startsWith("GROUP.")) {
94              Long groupId = new Long(GetterUtil.getLong(
95                  ownerId.substring(6, ownerId.length())));
96  
97              groupId = (Long)_groupIdMapper.getNewValue(groupId);
98  
99              ownerId = String.valueOf(groupId);
100             ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
101         }
102         else if (ownerId.startsWith("USER.")) {
103             ownerId = String.valueOf(_userIdMapper.getNewValue(
104                 ownerId.substring(5, ownerId.length())));
105             ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
106         }
107         else {
108             ownerId = String.valueOf(_userIdMapper.getNewValue(ownerId));
109             ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
110         }
111 
112         _ownerType = new Integer(ownerType);
113 
114         return ownerId;
115     }
116 
117     public Integer getOwnerType() {
118         return _ownerType;
119     }
120 
121     public Long getOldGroupId() {
122         return _oldGroupId;
123     }
124 
125     public Long getNewGroupId() {
126         return _newGroupId;
127     }
128 
129     public Boolean isPrivateLayout() {
130         return _privateLayout;
131     }
132 
133     private ValueMapper _companyIdMapper;
134     private ValueMapper _groupIdMapper;
135     private ValueMapper _userIdMapper;
136     private Integer _ownerType;
137     private Long _oldGroupId;
138     private Long _newGroupId;
139     private Boolean _privateLayout;
140 
141 }