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<>();
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     * @return Last version not deployed in upgradeOnly mode
061     * @since 5.7
062     */
063    public Version getLastVersion(boolean includeUpgradeOnly) {
064        if (includeUpgradeOnly) {
065            return getLastVersion();
066        }
067        for (int i = versions.size() - 1; i >= 0; i--) {
068            for (UpdateOptions opt : versions.get(i).packages.values()) {
069                if (!opt.upgradeOnly) {
070                    return versions.get(i);
071                }
072            }
073        }
074        return null;
075    }
076
077    public final String getKey() {
078        return key;
079    }
080
081    public final void setBaseVersion(Version baseVersion) {
082        this.baseVersion = baseVersion;
083    }
084
085    public final boolean hasBaseVersion() {
086        return baseVersion != null;
087    }
088
089    public final Version getBaseVersion() {
090        return baseVersion;
091    }
092
093    public final boolean isLastVersion(Version v) {
094        return v == getLastVersion();
095    }
096
097    public boolean removeVersion(Version version) {
098        return versions.remove(version);
099    }
100
101    public Version addVersion(Version version) {
102        if (versions.contains(version)) {
103            throw new VersionAlreadyExistException(version.getVersion());
104        }
105        versions.add(version);
106        return version;
107    }
108
109    public Version getVersion(String version) {
110        for (Version v : versions) {
111            String ov = v.getVersion();
112            if ((ov != null && ov.equals(version)) || (ov == null && version == null)) {
113                return v;
114            }
115        }
116        return null;
117    }
118
119    public Version getGreatestVersion() {
120        Version result = null;
121        FileVersion fv = null;
122        for (Version v : versions) {
123            if (fv == null) {
124                fv = v.getFileVersion();
125                result = v;
126            } else {
127                FileVersion fv2 = v.getFileVersion();
128                if (fv.lessThan(fv2)) {
129                    result = v;
130                    fv = fv2;
131                }
132            }
133        }
134        return result;
135    }
136
137    public Version getOrCreateVersion(String version) {
138        Version v = getVersion(version);
139        if (v == null) {
140            return addVersion(new Version(version));
141        }
142        return v;
143    }
144
145    public List<Version> getVersions() {
146        return versions;
147    }
148
149    @Override
150    public Iterator<Version> iterator() {
151        return versions.iterator();
152    }
153
154    @Override
155    public String toString() {
156        return key;
157    }
158
159}