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