1
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.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 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39
46 public class ResourcePrimKeyUpgradeColumnImpl extends BaseUpgradeColumnImpl {
47
48 public ResourcePrimKeyUpgradeColumnImpl(
49 UpgradeColumn nameColumn, ResourceCodeIdUpgradeColumnImpl codeIdColumn,
50 ValueMapper groupIdMapper, Map 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 =
100 (ClassPKContainer)_classPKContainers.get(classNameId);
101
102 if (classPKContainer != null) {
103 ValueMapper valueMapper = classPKContainer.getValueMapper();
104
105 if (valueMapper == null) {
106 _log.error("Name " + name + " does not have a value mapper");
107 }
108 else {
109 if (classPKContainer.isLong()) {
110 primKey = String.valueOf(valueMapper.getNewValue(
111 new Long(GetterUtil.getLong(primKey))));
112 }
113 else {
114 primKey = String.valueOf(valueMapper.getNewValue(primKey));
115 }
116 }
117 }
118 else {
119 _log.error("Name " + name + " is invalid");
120 }
121
122 return primKey;
123 }
124
125 protected String getLayoutPrimKey(String oldPrimKey) throws Exception {
126 int x = oldPrimKey.indexOf(StringPool.PERIOD, 4);
127 int y = oldPrimKey.indexOf("_LAYOUT_");
128
129 String oldOwnerId = oldPrimKey.substring(0, x);
130 String layoutId = oldPrimKey.substring(x + 1, y);
131
132 String oldPlidValue =
133 "{layoutId=" + layoutId + ", ownerId=" + oldOwnerId + "}";
134
135 Object newPlid = _layoutPlidMapper.getNewValue(oldPlidValue);
136
137 String newPrimKey = newPlid + oldPrimKey.substring(y);
138
139 return newPrimKey;
140 }
141
142 private static Log _log =
143 LogFactory.getLog(ResourcePrimKeyUpgradeColumnImpl.class);
144
145 private UpgradeColumn _nameColumn;
146 private ResourceCodeIdUpgradeColumnImpl _codeIdColumn;
147 private ValueMapper _groupIdMapper;
148 private Map _classPKContainers;
149 private ValueMapper _layoutPlidMapper;
150
151 }