1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v4_3_0.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.upgrade.UpgradeException;
20  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
21  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
22  import com.liferay.portal.kernel.upgrade.util.ValueMapper;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.model.PortletConstants;
26  import com.liferay.portal.model.ResourceConstants;
27  import com.liferay.portal.util.PortalUtil;
28  
29  import java.util.Map;
30  
31  /**
32   * <a href="ResourcePrimKeyUpgradeColumnImpl.java.html"><b><i>View Source</i>
33   * </b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class ResourcePrimKeyUpgradeColumnImpl extends BaseUpgradeColumnImpl {
38  
39      public ResourcePrimKeyUpgradeColumnImpl(
40          UpgradeColumn nameColumn, ResourceCodeIdUpgradeColumnImpl codeIdColumn,
41          ValueMapper groupIdMapper,
42          Map<Long, ClassPKContainer> classPKContainers,
43          ValueMapper layoutPlidMapper) {
44  
45          super("primKey");
46  
47          _nameColumn = nameColumn;
48          _codeIdColumn = codeIdColumn;
49          _groupIdMapper = groupIdMapper;
50          _classPKContainers = classPKContainers;
51          _layoutPlidMapper = layoutPlidMapper;
52      }
53  
54      public Object getNewValue(Object oldValue) throws Exception {
55          String primKey = (String)oldValue;
56  
57          int scope = _codeIdColumn.getScope();
58  
59          if (scope == ResourceConstants.SCOPE_COMPANY) {
60              return primKey;
61          }
62          else if (scope == ResourceConstants.SCOPE_GROUP) {
63              return String.valueOf(_groupIdMapper.getNewValue(
64                  new Long(GetterUtil.getLong(primKey))));
65          }
66          else if (scope == ResourceConstants.SCOPE_INDIVIDUAL) {
67              String name = (String)_nameColumn.getOldValue();
68  
69              if (name.startsWith("com.liferay.")) {
70                  primKey = getClassPKPrimKey(name, primKey);
71              }
72              else if ((primKey.indexOf("_LAYOUT_") > 0) &&
73                       (primKey.startsWith("PUB.") ||
74                        primKey.startsWith("PRI."))) {
75  
76                  primKey = getLayoutPrimKey(primKey);
77              }
78  
79              return primKey;
80          }
81          else {
82              throw new UpgradeException("Scope " + scope + " is invalid");
83          }
84      }
85  
86      protected String getClassPKPrimKey(String name, String primKey)
87          throws Exception {
88  
89          Long classNameId = new Long(PortalUtil.getClassNameId(name));
90  
91          ClassPKContainer classPKContainer = _classPKContainers.get(classNameId);
92  
93          if (classPKContainer != null) {
94              ValueMapper valueMapper = classPKContainer.getValueMapper();
95  
96              if (valueMapper == null) {
97                  _log.error("Name " + name + " does not have a value mapper");
98              }
99              else {
100                 if (classPKContainer.isLong()) {
101                     primKey = String.valueOf(valueMapper.getNewValue(
102                         new Long(GetterUtil.getLong(primKey))));
103                 }
104                 else {
105                     primKey = String.valueOf(valueMapper.getNewValue(primKey));
106                 }
107             }
108         }
109         else {
110             _log.error("Name " + name + " is invalid");
111         }
112 
113         return primKey;
114     }
115 
116     protected String getLayoutPrimKey(String oldPrimKey) throws Exception {
117         int x = oldPrimKey.indexOf(StringPool.PERIOD, 4);
118         int y = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
119 
120         String oldOwnerId = oldPrimKey.substring(0, x);
121         String layoutId = oldPrimKey.substring(x + 1, y);
122 
123         String oldPlidValue =
124             "{layoutId=" + layoutId + ", ownerId=" + oldOwnerId + "}";
125 
126         Object newPlid = _layoutPlidMapper.getNewValue(oldPlidValue);
127 
128         String newPrimKey = newPlid + oldPrimKey.substring(y);
129 
130         return newPrimKey;
131     }
132 
133     private static Log _log = LogFactoryUtil.getLog(
134         ResourcePrimKeyUpgradeColumnImpl.class);
135 
136     private UpgradeColumn _nameColumn;
137     private ResourceCodeIdUpgradeColumnImpl _codeIdColumn;
138     private ValueMapper _groupIdMapper;
139     private Map<Long, ClassPKContainer> _classPKContainers;
140     private ValueMapper _layoutPlidMapper;
141 
142 }