001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v4_3_0.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.upgrade.UpgradeException;
020    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
022    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.model.PortletConstants;
026    import com.liferay.portal.model.ResourceConstants;
027    import com.liferay.portal.util.PortalUtil;
028    
029    import java.util.Map;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class ResourcePrimKeyUpgradeColumnImpl extends BaseUpgradeColumnImpl {
035    
036            public ResourcePrimKeyUpgradeColumnImpl(
037                    UpgradeColumn nameColumn, ResourceCodeIdUpgradeColumnImpl codeIdColumn,
038                    ValueMapper groupIdMapper,
039                    Map<Long, ClassPKContainer> classPKContainers,
040                    ValueMapper layoutPlidMapper) {
041    
042                    super("primKey");
043    
044                    _nameColumn = nameColumn;
045                    _codeIdColumn = codeIdColumn;
046                    _groupIdMapper = groupIdMapper;
047                    _classPKContainers = classPKContainers;
048                    _layoutPlidMapper = layoutPlidMapper;
049            }
050    
051            public Object getNewValue(Object oldValue) throws Exception {
052                    String primKey = (String)oldValue;
053    
054                    int scope = _codeIdColumn.getScope();
055    
056                    if (scope == ResourceConstants.SCOPE_COMPANY) {
057                            return primKey;
058                    }
059                    else if (scope == ResourceConstants.SCOPE_GROUP) {
060                            return String.valueOf(_groupIdMapper.getNewValue(
061                                    new Long(GetterUtil.getLong(primKey))));
062                    }
063                    else if (scope == ResourceConstants.SCOPE_INDIVIDUAL) {
064                            String name = (String)_nameColumn.getOldValue();
065    
066                            if (name.startsWith("com.liferay.")) {
067                                    primKey = getClassPKPrimKey(name, primKey);
068                            }
069                            else if ((primKey.indexOf("_LAYOUT_") > 0) &&
070                                             (primKey.startsWith("PUB.") ||
071                                              primKey.startsWith("PRI."))) {
072    
073                                    primKey = getLayoutPrimKey(primKey);
074                            }
075    
076                            return primKey;
077                    }
078                    else {
079                            throw new UpgradeException("Scope " + scope + " is invalid");
080                    }
081            }
082    
083            protected String getClassPKPrimKey(String name, String primKey)
084                    throws Exception {
085    
086                    Long classNameId = new Long(PortalUtil.getClassNameId(name));
087    
088                    ClassPKContainer classPKContainer = _classPKContainers.get(classNameId);
089    
090                    if (classPKContainer != null) {
091                            ValueMapper valueMapper = classPKContainer.getValueMapper();
092    
093                            if (valueMapper == null) {
094                                    _log.error("Name " + name + " does not have a value mapper");
095                            }
096                            else {
097                                    if (classPKContainer.isLong()) {
098                                            primKey = String.valueOf(valueMapper.getNewValue(
099                                                    new Long(GetterUtil.getLong(primKey))));
100                                    }
101                                    else {
102                                            primKey = String.valueOf(valueMapper.getNewValue(primKey));
103                                    }
104                            }
105                    }
106                    else {
107                            _log.error("Name " + name + " is invalid");
108                    }
109    
110                    return primKey;
111            }
112    
113            protected String getLayoutPrimKey(String oldPrimKey) throws Exception {
114                    int x = oldPrimKey.indexOf(StringPool.PERIOD, 4);
115                    int y = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
116    
117                    String oldOwnerId = oldPrimKey.substring(0, x);
118                    String layoutId = oldPrimKey.substring(x + 1, y);
119    
120                    String oldPlidValue =
121                            "{layoutId=" + layoutId + ", ownerId=" + oldOwnerId + "}";
122    
123                    Object newPlid = _layoutPlidMapper.getNewValue(oldPlidValue);
124    
125                    String newPrimKey = newPlid + oldPrimKey.substring(y);
126    
127                    return newPrimKey;
128            }
129    
130            private static Log _log = LogFactoryUtil.getLog(
131                    ResourcePrimKeyUpgradeColumnImpl.class);
132    
133            private UpgradeColumn _nameColumn;
134            private ResourceCodeIdUpgradeColumnImpl _codeIdColumn;
135            private ValueMapper _groupIdMapper;
136            private Map<Long, ClassPKContainer> _classPKContainers;
137            private ValueMapper _layoutPlidMapper;
138    
139    }