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.upgrade.util.TempUpgradeColumnImpl;
018    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.util.PortletKeys;
021    
022    import java.sql.Types;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class PrefsOwnerIdUpgradeColumnImpl extends TempUpgradeColumnImpl {
028    
029            public PrefsOwnerIdUpgradeColumnImpl(
030                    ValueMapper companyIdMapper, ValueMapper groupIdMapper,
031                    ValueMapper userIdMapper) {
032    
033                    super("ownerId", new Integer(Types.VARCHAR));
034    
035                    _companyIdMapper = companyIdMapper;
036                    _groupIdMapper = groupIdMapper;
037                    _userIdMapper = userIdMapper;
038            }
039    
040            public Integer getNewColumnType(Integer defaultType) {
041                    return new Integer(Types.BIGINT);
042            }
043    
044            public Object getNewValue(Object oldValue) throws Exception {
045                    _ownerType = null;
046                    _oldGroupId = null;
047                    _newGroupId = null;
048                    _privateLayout = null;
049    
050                    String ownerId = (String)oldValue;
051    
052                    int ownerType = 0;
053    
054                    if (ownerId.startsWith("PUB.") || ownerId.startsWith("PRI.")) {
055                            _privateLayout = Boolean.valueOf(ownerId.startsWith("PRI."));
056    
057                            int pos = ownerId.indexOf(".USER.");
058    
059                            if (pos != -1) {
060                                    _oldGroupId = new Long(GetterUtil.getLong(
061                                            ownerId.substring(4, pos)));
062                                    _newGroupId = (Long)_groupIdMapper.getNewValue(_oldGroupId);
063    
064                                    ownerId = String.valueOf(_userIdMapper.getNewValue(
065                                            ownerId.substring(pos + 6, ownerId.length())));
066                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
067                            }
068                            else {
069                                    _oldGroupId = new Long(GetterUtil.getLong(
070                                            ownerId.substring(4, ownerId.length())));
071                                    _newGroupId = (Long)_groupIdMapper.getNewValue(_oldGroupId);
072    
073                                    ownerId = String.valueOf(PortletKeys.PREFS_OWNER_ID_DEFAULT);
074                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
075                            }
076                    }
077                    else if (ownerId.startsWith("COMPANY.")) {
078                            ownerId = String.valueOf(_companyIdMapper.getNewValue(
079                                    ownerId.substring(8, ownerId.length())));
080                            ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
081                    }
082                    else if (ownerId.startsWith("GROUP.")) {
083                            Long groupId = new Long(GetterUtil.getLong(
084                                    ownerId.substring(6, ownerId.length())));
085    
086                            groupId = (Long)_groupIdMapper.getNewValue(groupId);
087    
088                            ownerId = String.valueOf(groupId);
089                            ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
090                    }
091                    else if (ownerId.startsWith("USER.")) {
092                            ownerId = String.valueOf(_userIdMapper.getNewValue(
093                                    ownerId.substring(5, ownerId.length())));
094                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
095                    }
096                    else {
097                            ownerId = String.valueOf(_userIdMapper.getNewValue(ownerId));
098                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
099                    }
100    
101                    _ownerType = new Integer(ownerType);
102    
103                    return ownerId;
104            }
105    
106            public Integer getOwnerType() {
107                    return _ownerType;
108            }
109    
110            public Long getOldGroupId() {
111                    return _oldGroupId;
112            }
113    
114            public Long getNewGroupId() {
115                    return _newGroupId;
116            }
117    
118            public Boolean isPrivateLayout() {
119                    return _privateLayout;
120            }
121    
122            private ValueMapper _companyIdMapper;
123            private ValueMapper _groupIdMapper;
124            private ValueMapper _userIdMapper;
125            private Integer _ownerType;
126            private Long _oldGroupId;
127            private Long _newGroupId;
128            private Boolean _privateLayout;
129    
130    }