1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.v4_3_0.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.model.PortletConstants;
27  import com.liferay.portal.model.ResourceConstants;
28  import com.liferay.portal.upgrade.UpgradeException;
29  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
30  import com.liferay.portal.upgrade.util.UpgradeColumn;
31  import com.liferay.portal.upgrade.util.ValueMapper;
32  import com.liferay.portal.util.PortalUtil;
33  
34  import java.util.Map;
35  
36  /**
37   * <a href="ResourcePrimKeyUpgradeColumnImpl.java.html"><b><i>View Source</i>
38   * </b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class ResourcePrimKeyUpgradeColumnImpl extends BaseUpgradeColumnImpl {
44  
45      public ResourcePrimKeyUpgradeColumnImpl(
46          UpgradeColumn nameColumn, ResourceCodeIdUpgradeColumnImpl codeIdColumn,
47          ValueMapper groupIdMapper,
48          Map<Long, ClassPKContainer> classPKContainers,
49          ValueMapper layoutPlidMapper) {
50  
51          super("primKey");
52  
53          _nameColumn = nameColumn;
54          _codeIdColumn = codeIdColumn;
55          _groupIdMapper = groupIdMapper;
56          _classPKContainers = classPKContainers;
57          _layoutPlidMapper = layoutPlidMapper;
58      }
59  
60      public Object getNewValue(Object oldValue) throws Exception {
61          String primKey = (String)oldValue;
62  
63          int scope = _codeIdColumn.getScope();
64  
65          if (scope == ResourceConstants.SCOPE_COMPANY) {
66              return primKey;
67          }
68          else if (scope == ResourceConstants.SCOPE_GROUP) {
69              return String.valueOf(_groupIdMapper.getNewValue(
70                  new Long(GetterUtil.getLong(primKey))));
71          }
72          else if (scope == ResourceConstants.SCOPE_INDIVIDUAL) {
73              String name = (String)_nameColumn.getOldValue();
74  
75              if (name.startsWith("com.liferay.")) {
76                  primKey = getClassPKPrimKey(name, primKey);
77              }
78              else if ((primKey.indexOf("_LAYOUT_") > 0) &&
79                       (primKey.startsWith("PUB.") ||
80                        primKey.startsWith("PRI."))) {
81  
82                  primKey = getLayoutPrimKey(primKey);
83              }
84  
85              return primKey;
86          }
87          else {
88              throw new UpgradeException("Scope " + scope + " is invalid");
89          }
90      }
91  
92      protected String getClassPKPrimKey(String name, String primKey)
93          throws Exception {
94  
95          Long classNameId = new Long(PortalUtil.getClassNameId(name));
96  
97          ClassPKContainer classPKContainer = _classPKContainers.get(classNameId);
98  
99          if (classPKContainer != null) {
100             ValueMapper valueMapper = classPKContainer.getValueMapper();
101 
102             if (valueMapper == null) {
103                 _log.error("Name " + name + " does not have a value mapper");
104             }
105             else {
106                 if (classPKContainer.isLong()) {
107                     primKey = String.valueOf(valueMapper.getNewValue(
108                         new Long(GetterUtil.getLong(primKey))));
109                 }
110                 else {
111                     primKey = String.valueOf(valueMapper.getNewValue(primKey));
112                 }
113             }
114         }
115         else {
116             _log.error("Name " + name + " is invalid");
117         }
118 
119         return primKey;
120     }
121 
122     protected String getLayoutPrimKey(String oldPrimKey) throws Exception {
123         int x = oldPrimKey.indexOf(StringPool.PERIOD, 4);
124         int y = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
125 
126         String oldOwnerId = oldPrimKey.substring(0, x);
127         String layoutId = oldPrimKey.substring(x + 1, y);
128 
129         String oldPlidValue =
130             "{layoutId=" + layoutId + ", ownerId=" + oldOwnerId + "}";
131 
132         Object newPlid = _layoutPlidMapper.getNewValue(oldPlidValue);
133 
134         String newPrimKey = newPlid + oldPrimKey.substring(y);
135 
136         return newPrimKey;
137     }
138 
139     private static Log _log =
140         LogFactoryUtil.getLog(ResourcePrimKeyUpgradeColumnImpl.class);
141 
142     private UpgradeColumn _nameColumn;
143     private ResourceCodeIdUpgradeColumnImpl _codeIdColumn;
144     private ValueMapper _groupIdMapper;
145     private Map<Long, ClassPKContainer> _classPKContainers;
146     private ValueMapper _layoutPlidMapper;
147 
148 }