001/*
002 * (C) Copyright 2011-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 *     tdelprat
018 *
019 */
020package org.nuxeo.wizard.download;
021
022import java.io.File;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.lang.StringUtils;
027
028/**
029 * @author Tiry (tdelprat@nuxeo.com)
030 * @since 5.5
031 */
032public class DownloadPackage {
033
034    protected String baseUrl;
035
036    protected String filename;
037
038    protected String label;
039
040    protected String md5;
041
042    /**
043     * @since 5.9.3 Virtual package, no download nor install, typically CAP
044     */
045    protected boolean virtual;
046
047    protected File localFile;
048
049    protected String color;
050
051    protected String textColor;
052
053    protected boolean enabled;
054
055    protected final String id;
056
057    protected String downloadUrl;
058
059    protected String shortLabel;
060
061    protected final List<String> impliedDeps = new ArrayList<>();
062
063    public DownloadPackage(String id) {
064        this.id = id;
065    }
066
067    public String getFilename() {
068        return filename;
069    }
070
071    public void setFilename(String filename) {
072        this.filename = filename;
073    }
074
075    public String getLabel() {
076        return label;
077    }
078
079    public void setLabel(String label) {
080        this.label = label;
081    }
082
083    public String getMd5() {
084        return md5;
085    }
086
087    public void setMd5(String md5) {
088        if ("".equals(md5)) {
089            md5 = null;
090        }
091        this.md5 = md5;
092    }
093
094    public File getLocalFile() {
095        return localFile;
096    }
097
098    public void setLocalFile(File localFile) {
099        this.localFile = localFile;
100    }
101
102    public String getId() {
103        return id;
104    }
105
106    public String getBaseUrl() {
107        return baseUrl;
108    }
109
110    public void setBaseUrl(String baseUrl) {
111        if (baseUrl != null && !baseUrl.endsWith("/")) {
112            baseUrl = baseUrl + "/";
113        }
114        this.baseUrl = baseUrl;
115    }
116
117    @Override
118    public boolean equals(Object obj) {
119        if (obj instanceof DownloadPackage) {
120            return ((DownloadPackage) obj).id.equals(id);
121        }
122        return super.equals(obj);
123    }
124
125    public String getDownloadUrl() {
126        if (downloadUrl != null) {
127            return downloadUrl;
128        }
129        return getBaseUrl() + getFilename();
130    }
131
132    public void setDownloadUrl(String url) {
133        downloadUrl = url;
134    }
135
136    public boolean isEnabled() {
137        return enabled;
138    }
139
140    public void setEnabled(boolean enabled) {
141        this.enabled = enabled;
142    }
143
144    @Override
145    public String toString() {
146        StringBuilder sb = new StringBuilder();
147        sb.append("[");
148        sb.append(id);
149        sb.append(" (");
150        sb.append(label);
151        sb.append(" )]");
152        return sb.toString();
153    }
154
155    public String getColor() {
156        return color;
157    }
158
159    public void setColor(String color) {
160        this.color = color;
161    }
162
163    public boolean isAlreadyInLocal() {
164        return getLocalFile() != null && getLocalFile().exists();
165    }
166
167    public List<String> getImpliedDeps() {
168        return impliedDeps;
169    }
170
171    public void addDep(String depId) {
172        impliedDeps.add(depId);
173    }
174
175    public void addDeps(String[] depIds) {
176        for (String depId : depIds) {
177            addDep(depId);
178        }
179    }
180
181    public String getTextColor() {
182        return textColor;
183    }
184
185    public void setTextColor(String textColor) {
186        this.textColor = textColor;
187    }
188
189    public String getShortLabel() {
190        return shortLabel;
191    }
192
193    public void setShortLabel(String shortLabel) {
194        this.shortLabel = shortLabel;
195    }
196
197    /**
198     * @since 5.9.3
199     */
200    public boolean isVirtual() {
201        return virtual;
202    }
203
204    /**
205     * @since 5.9.3
206     */
207    public void setVirtual(boolean virtual) {
208        this.virtual = virtual;
209    }
210
211    /**
212     * @return true if can only be downloaded from the Marketplace, not from {@link DownloadPackage#getDownloadUrl()}
213     * @since 7.1
214     */
215    public boolean isLaterDownload() {
216        return StringUtils.isBlank(getDownloadUrl()) || getFilename() == null || "".equals(getFilename());
217    }
218
219}