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