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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.PortletConstants;
30  import com.liferay.portal.model.ResourceConstants;
31  import com.liferay.portal.upgrade.UpgradeException;
32  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
33  import com.liferay.portal.upgrade.util.UpgradeColumn;
34  import com.liferay.portal.upgrade.util.ValueMapper;
35  import com.liferay.portal.util.PortalUtil;
36  
37  import java.util.Map;
38  
39  /**
40   * <a href="ResourcePrimKeyUpgradeColumnImpl.java.html"><b><i>View Source</i>
41   * </b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ResourcePrimKeyUpgradeColumnImpl extends BaseUpgradeColumnImpl {
47  
48      public ResourcePrimKeyUpgradeColumnImpl(
49          UpgradeColumn nameColumn, ResourceCodeIdUpgradeColumnImpl codeIdColumn,
50          ValueMapper groupIdMapper,
51          Map<Long, ClassPKContainer> classPKContainers,
52          ValueMapper layoutPlidMapper) {
53  
54          super("primKey");
55  
56          _nameColumn = nameColumn;
57          _codeIdColumn = codeIdColumn;
58          _groupIdMapper = groupIdMapper;
59          _classPKContainers = classPKContainers;
60          _layoutPlidMapper = layoutPlidMapper;
61      }
62  
63      public Object getNewValue(Object oldValue) throws Exception {
64          String primKey = (String)oldValue;
65  
66          int scope = _codeIdColumn.getScope();
67  
68          if (scope == ResourceConstants.SCOPE_COMPANY) {
69              return primKey;
70          }
71          else if (scope == ResourceConstants.SCOPE_GROUP) {
72              return String.valueOf(_groupIdMapper.getNewValue(
73                  new Long(GetterUtil.getLong(primKey))));
74          }
75          else if (scope == ResourceConstants.SCOPE_INDIVIDUAL) {
76              String name = (String)_nameColumn.getOldValue();
77  
78              if (name.startsWith("com.liferay.")) {
79                  primKey = getClassPKPrimKey(name, primKey);
80              }
81              else if ((primKey.indexOf("_LAYOUT_") > 0) &&
82                       (primKey.startsWith("PUB.") ||
83                        primKey.startsWith("PRI."))) {
84  
85                  primKey = getLayoutPrimKey(primKey);
86              }
87  
88              return primKey;
89          }
90          else {
91              throw new UpgradeException("Scope " + scope + " is invalid");
92          }
93      }
94  
95      protected String getClassPKPrimKey(String name, String primKey)
96          throws Exception {
97  
98          Long classNameId = new Long(PortalUtil.getClassNameId(name));
99  
100         ClassPKContainer classPKContainer = _classPKContainers.get(classNameId);
101 
102         if (classPKContainer != null) {
103             ValueMapper valueMapper = classPKContainer.getValueMapper();
104 
105             if (valueMapper == null) {
106                 _log.error("Name " + name + " does not have a value mapper");
107             }
108             else {
109                 if (classPKContainer.isLong()) {
110                     primKey = String.valueOf(valueMapper.getNewValue(
111                         new Long(GetterUtil.getLong(primKey))));
112                 }
113                 else {
114                     primKey = String.valueOf(valueMapper.getNewValue(primKey));
115                 }
116             }
117         }
118         else {
119             _log.error("Name " + name + " is invalid");
120         }
121 
122         return primKey;
123     }
124 
125     protected String getLayoutPrimKey(String oldPrimKey) throws Exception {
126         int x = oldPrimKey.indexOf(StringPool.PERIOD, 4);
127         int y = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
128 
129         String oldOwnerId = oldPrimKey.substring(0, x);
130         String layoutId = oldPrimKey.substring(x + 1, y);
131 
132         String oldPlidValue =
133             "{layoutId=" + layoutId + ", ownerId=" + oldOwnerId + "}";
134 
135         Object newPlid = _layoutPlidMapper.getNewValue(oldPlidValue);
136 
137         String newPrimKey = newPlid + oldPrimKey.substring(y);
138 
139         return newPrimKey;
140     }
141 
142     private static Log _log =
143         LogFactoryUtil.getLog(ResourcePrimKeyUpgradeColumnImpl.class);
144 
145     private UpgradeColumn _nameColumn;
146     private ResourceCodeIdUpgradeColumnImpl _codeIdColumn;
147     private ValueMapper _groupIdMapper;
148     private Map<Long, ClassPKContainer> _classPKContainers;
149     private ValueMapper _layoutPlidMapper;
150 
151 }