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.UpgradeException;
018    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
019    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
020    import com.liferay.portal.model.ResourceCode;
021    import com.liferay.portal.model.ResourceConstants;
022    import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
023    
024    /**
025     * @author Alexander Chow
026     * @author Brian Wing Shun Chan
027     */
028    public class ResourceCodeIdUpgradeColumnImpl extends BaseUpgradeColumnImpl {
029    
030            public ResourceCodeIdUpgradeColumnImpl(
031                    UpgradeColumn companyIdColumn, UpgradeColumn nameColumn,
032                    UpgradeColumn scopeColumn) {
033    
034                    super("codeId");
035    
036                    _companyIdColumn = companyIdColumn;
037                    _nameColumn = nameColumn;
038                    _scopeColumn = scopeColumn;
039            }
040    
041            public Object getNewValue(Object oldValue) throws Exception {
042                    _scope = 0;
043    
044                    Long companyId = (Long)_companyIdColumn.getOldValue();
045                    String name = (String)_nameColumn.getOldValue();
046                    String scope = (String)_scopeColumn.getOldValue();
047    
048                    if (scope.equals("company")) {
049                            _scope = ResourceConstants.SCOPE_COMPANY;
050                    }
051                    else if (scope.equals("group")) {
052                            _scope = ResourceConstants.SCOPE_GROUP;
053                    }
054                    else if (scope.equals("groupTemplate")) {
055                            _scope = ResourceConstants.SCOPE_GROUP_TEMPLATE;
056                    }
057                    else if (scope.equals("individual")) {
058                            _scope = ResourceConstants.SCOPE_INDIVIDUAL;
059                    }
060                    else {
061                            throw new UpgradeException("Scope " + _scope + " is invalid");
062                    }
063    
064                    ResourceCode resourceCode =
065                            ResourceCodeLocalServiceUtil.getResourceCode(
066                                    companyId.longValue(), name, _scope);
067    
068                    return new Long(resourceCode.getCodeId());
069            }
070    
071            public int getScope() {
072                    return _scope;
073            }
074    
075            private UpgradeColumn _companyIdColumn;
076            private UpgradeColumn _nameColumn;
077            private UpgradeColumn _scopeColumn;
078            private int _scope;
079    
080    }