1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v4_3_0.util;
16  
17  import com.liferay.portal.kernel.upgrade.UpgradeException;
18  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
19  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
20  import com.liferay.portal.model.ResourceCode;
21  import com.liferay.portal.model.ResourceConstants;
22  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
23  
24  /**
25   * <a href="ResourceCodeIdUpgradeColumnImpl.java.html"><b><i>View Source</i></b>
26   * </a>
27   *
28   * @author Alexander Chow
29   * @author Brian Wing Shun Chan
30   */
31  public class ResourceCodeIdUpgradeColumnImpl extends BaseUpgradeColumnImpl {
32  
33      public ResourceCodeIdUpgradeColumnImpl(
34          UpgradeColumn companyIdColumn, UpgradeColumn nameColumn,
35          UpgradeColumn scopeColumn) {
36  
37          super("codeId");
38  
39          _companyIdColumn = companyIdColumn;
40          _nameColumn = nameColumn;
41          _scopeColumn = scopeColumn;
42      }
43  
44      public Object getNewValue(Object oldValue) throws Exception {
45          _scope = 0;
46  
47          Long companyId = (Long)_companyIdColumn.getOldValue();
48          String name = (String)_nameColumn.getOldValue();
49          String scope = (String)_scopeColumn.getOldValue();
50  
51          if (scope.equals("company")) {
52              _scope = ResourceConstants.SCOPE_COMPANY;
53          }
54          else if (scope.equals("group")) {
55              _scope = ResourceConstants.SCOPE_GROUP;
56          }
57          else if (scope.equals("groupTemplate")) {
58              _scope = ResourceConstants.SCOPE_GROUP_TEMPLATE;
59          }
60          else if (scope.equals("individual")) {
61              _scope = ResourceConstants.SCOPE_INDIVIDUAL;
62          }
63          else {
64              throw new UpgradeException("Scope " + _scope + " is invalid");
65          }
66  
67          ResourceCode resourceCode =
68              ResourceCodeLocalServiceUtil.getResourceCode(
69                  companyId.longValue(), name, _scope);
70  
71          return new Long(resourceCode.getCodeId());
72      }
73  
74      public int getScope() {
75          return _scope;
76      }
77  
78      private UpgradeColumn _companyIdColumn;
79      private UpgradeColumn _nameColumn;
80      private UpgradeColumn _scopeColumn;
81      private int _scope;
82  
83  }