001    /**
002     * Copyright (c) 2000-present 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;
016    
017    import com.liferay.counter.kernel.model.Counter;
018    import com.liferay.counter.kernel.service.CounterLocalService;
019    import com.liferay.counter.kernel.service.persistence.CounterFinder;
020    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
021    import com.liferay.portal.kernel.model.ResourcePermission;
022    import com.liferay.portal.kernel.upgrade.UpgradeException;
023    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
024    import com.liferay.portal.spring.aop.ServiceWrapperProxyUtil;
025    
026    import java.io.Closeable;
027    
028    import java.util.List;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public abstract class Pre7UpgradeProcess extends UpgradeProcess {
034    
035            @Override
036            public void upgrade() throws UpgradeException {
037                    try (Closeable closeable = ServiceWrapperProxyUtil.injectFieldProxy(
038                                    PortalBeanLocatorUtil.locate(
039                                            CounterLocalService.class.getName()),
040                                    "counterFinder", Pre7CounterFinderImpl.class)) {
041    
042                            super.upgrade();
043                    }
044                    catch (UpgradeException ue) {
045                            throw ue;
046                    }
047                    catch (Exception e) {
048                            throw new UpgradeException(e);
049                    }
050            }
051    
052            private static class Pre7CounterFinderImpl implements CounterFinder {
053    
054                    @Override
055                    public List<String> getNames() {
056                            return _counterFinder.getNames();
057                    }
058    
059                    @Override
060                    public String getRegistryName() {
061                            return _counterFinder.getRegistryName();
062                    }
063    
064                    @Override
065                    public long increment() {
066                            return _counterFinder.increment(
067                                    "com.liferay.counter.model.Counter");
068                    }
069    
070                    @Override
071                    public long increment(String name) {
072                            if (name.equals(Counter.class.getName())) {
073                                    name = "com.liferay.counter.model.Counter";
074                            }
075                            else if (name.equals(ResourcePermission.class.getName())) {
076                                    name = "com.liferay.portal.model.ResourcePermission";
077                            }
078    
079                            return _counterFinder.increment(name);
080                    }
081    
082                    @Override
083                    public long increment(String name, int size) {
084                            if (name.equals(Counter.class.getName())) {
085                                    name = "com.liferay.counter.model.Counter";
086                            }
087                            else if (name.equals(ResourcePermission.class.getName())) {
088                                    name = "com.liferay.portal.model.ResourcePermission";
089                            }
090    
091                            return _counterFinder.increment(name, size);
092                    }
093    
094                    @Override
095                    public void invalidate() {
096                            _counterFinder.invalidate();
097                    }
098    
099                    @Override
100                    public void rename(String oldName, String newName) {
101                            _counterFinder.rename(oldName, newName);
102                    }
103    
104                    @Override
105                    public void reset(String name) {
106                            _counterFinder.reset(name);
107                    }
108    
109                    @Override
110                    public void reset(String name, long size) {
111                            _counterFinder.reset(name, size);
112                    }
113    
114                    private Pre7CounterFinderImpl(CounterFinder counterFinder) {
115                            _counterFinder = counterFinder;
116                    }
117    
118                    private final CounterFinder _counterFinder;
119    
120            }
121    
122    }