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.StagnantRowException;
018    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
019    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
020    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class LayoutOwnerIdUpgradeColumnImpl extends BaseUpgradeColumnImpl {
027    
028            public LayoutOwnerIdUpgradeColumnImpl(
029                    String name, UpgradeColumn upgradeColumn, ValueMapper groupIdMapper) {
030    
031                    super(name);
032    
033                    _name = name;
034                    _upgradeColumn = upgradeColumn;
035                    _groupIdMapper = groupIdMapper;
036            }
037    
038            public Object getNewValue(Object oldValue) throws Exception {
039                    _groupId = null;
040                    _privateLayout = null;
041    
042                    String ownerId = (String)_upgradeColumn.getOldValue();
043    
044                    if (_name.equals("groupId")) {
045                            if (ownerId.startsWith("PUB.") || ownerId.startsWith("PRI.")) {
046                                    Long groupId = new Long(GetterUtil.getLong(
047                                            ownerId.substring(4, ownerId.length())));
048    
049                                    _groupId = (Long)_groupIdMapper.getNewValue(groupId);
050    
051                                    return _groupId;
052                            }
053                            else {
054                                    throw new StagnantRowException(ownerId);
055                            }
056                    }
057                    else {
058                            if (ownerId.startsWith("PUB.")) {
059                                    _privateLayout = Boolean.FALSE;
060    
061                                    return _privateLayout;
062                            }
063                            else if (ownerId.startsWith("PRI.")) {
064                                    _privateLayout = Boolean.TRUE;
065    
066                                    return _privateLayout;
067                            }
068                            else {
069                                    throw new StagnantRowException(ownerId);
070                            }
071                    }
072            }
073    
074            public Long getGroupId() {
075                    return _groupId;
076            }
077    
078            public Boolean isPrivateLayout() {
079                    return _privateLayout;
080            }
081    
082            private String _name;
083            private UpgradeColumn _upgradeColumn;
084            private ValueMapper _groupIdMapper;
085            private Long _groupId;
086            private Boolean _privateLayout;
087    
088    }