001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.connect.update.task.update;
020
021import java.util.ArrayList;
022import java.util.Iterator;
023import java.util.List;
024
025import org.nuxeo.common.utils.FileVersion;
026
027/**
028 * Versions are stored in the same order they are registered (in historical package install order). That means when
029 * rollbacking an update the file system will be modified only if the last version was rollbacked. And the version that
030 * will become the current version will be the last version in the version list.
031 *
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class Entry implements Iterable<Version> {
035
036    protected String key;
037
038    protected Version baseVersion;
039
040    protected List<Version> versions;
041
042    public Entry(String key) {
043        this.key = key;
044        versions = new ArrayList<Version>();
045    }
046
047    public final boolean isEmpty() {
048        return versions.isEmpty();
049    }
050
051    public final Version getFirstVersion() {
052        return versions.isEmpty() ? null : versions.get(0);
053    }
054
055    public final Version getLastVersion() {
056        return versions.isEmpty() ? null : versions.get(versions.size() - 1);
057    }
058
059    /**
060     * @param includeUpgradeOnly
061     * @return Last version not deployed in upgradeOnly mode
062     * @since 5.7
063     */
064    public Version getLastVersion(boolean includeUpgradeOnly) {
065        if (includeUpgradeOnly) {
066            return getLastVersion();
067        }
068        for (int i = versions.size() - 1; i >= 0; i--) {
069            for (UpdateOptions opt : versions.get(i).packages.values()) {
070                if (opt.upgradeOnly == false) {
071                    return versions.get(i);
072                }
073            }
074        }
075        return null;
076    }
077
078    public final String getKey() {
079        return key;
080    }
081
082    public final void setBaseVersion(Version baseVersion) {
083        this.baseVersion = baseVersion;
084    }
085
086    public final boolean hasBaseVersion() {
087        return baseVersion != null;
088    }
089
090    public final Version getBaseVersion() {
091        return baseVersion;
092    }
093
094    public final boolean isLastVersion(Version v) {
095        return v == getLastVersion();
096    }
097
098    public boolean removeVersion(Version version) {
099        return versions.remove(version);
100    }
101
102    public Version addVersion(Version version) {
103        if (versions.contains(version)) {
104            throw new VersionAlreadyExistException(version.getVersion());
105        }
106        versions.add(version);
107        return version;
108    }
109
110    public Version getVersion(String version) {
111        for (Version v : versions) {
112            String ov = v.getVersion();
113            if ((ov != null && ov.equals(version)) || (ov == null && version == null)) {
114                return v;
115            }
116        }
117        return null;
118    }
119
120    public Version getGreatestVersion() {
121        Version result = null;
122        FileVersion fv = null;
123        for (Version v : versions) {
124            if (fv == null) {
125                fv = v.getFileVersion();
126                result = v;
127            } else {
128                FileVersion fv2 = v.getFileVersion();
129                if (fv.lessThan(fv2)) {
130                    result = v;
131                    fv = fv2;
132                }
133            }
134        }
135        return result;
136    }
137
138    /**
139     * @param version
140     */
141    public Version getOrCreateVersion(String version) {
142        Version v = getVersion(version);
143        if (v == null) {
144            return addVersion(new Version(version));
145        }
146        return v;
147    }
148
149    public List<Version> getVersions() {
150        return versions;
151    }
152
153    @Override
154    public Iterator<Version> iterator() {
155        return versions.iterator();
156    }
157
158    @Override
159    public String toString() {
160        return key;
161    }
162
163}