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