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    /**
062     * @since 8.3
063     */
064    protected String description;
065
066    protected final List<String> impliedDeps = new ArrayList<>();
067
068    public DownloadPackage(String id) {
069        this.id = id;
070    }
071
072    public String getFilename() {
073        return filename;
074    }
075
076    public void setFilename(String filename) {
077        this.filename = filename;
078    }
079
080    public String getLabel() {
081        return label;
082    }
083
084    public void setLabel(String label) {
085        this.label = label;
086    }
087
088    public String getMd5() {
089        return md5;
090    }
091
092    public void setMd5(String md5) {
093        if ("".equals(md5)) {
094            md5 = null;
095        }
096        this.md5 = md5;
097    }
098
099    public File getLocalFile() {
100        return localFile;
101    }
102
103    public void setLocalFile(File localFile) {
104        this.localFile = localFile;
105    }
106
107    public String getId() {
108        return id;
109    }
110
111    public String getBaseUrl() {
112        return baseUrl;
113    }
114
115    public void setBaseUrl(String baseUrl) {
116        if (baseUrl != null && !baseUrl.endsWith("/")) {
117            baseUrl = baseUrl + "/";
118        }
119        this.baseUrl = baseUrl;
120    }
121
122    @Override
123    public boolean equals(Object obj) {
124        if (obj instanceof DownloadPackage) {
125            return ((DownloadPackage) obj).id.equals(id);
126        }
127        return super.equals(obj);
128    }
129
130    public String getDownloadUrl() {
131        if (downloadUrl != null) {
132            return downloadUrl;
133        }
134        return getBaseUrl() + getFilename();
135    }
136
137    public void setDownloadUrl(String url) {
138        downloadUrl = url;
139    }
140
141    public boolean isEnabled() {
142        return enabled;
143    }
144
145    public void setEnabled(boolean enabled) {
146        this.enabled = enabled;
147    }
148
149    @Override
150    public String toString() {
151        StringBuilder sb = new StringBuilder();
152        sb.append("[");
153        sb.append(id);
154        sb.append(" (");
155        sb.append(label);
156        sb.append(" )]");
157        return sb.toString();
158    }
159
160    public String getColor() {
161        return color;
162    }
163
164    public void setColor(String color) {
165        this.color = color;
166    }
167
168    public boolean isAlreadyInLocal() {
169        return getLocalFile() != null && getLocalFile().exists();
170    }
171
172    public List<String> getImpliedDeps() {
173        return impliedDeps;
174    }
175
176    public void addDep(String depId) {
177        impliedDeps.add(depId);
178    }
179
180    public void addDeps(String[] depIds) {
181        for (String depId : depIds) {
182            addDep(depId);
183        }
184    }
185
186    public String getTextColor() {
187        return textColor;
188    }
189
190    public void setTextColor(String textColor) {
191        this.textColor = textColor;
192    }
193
194    public String getShortLabel() {
195        return shortLabel;
196    }
197
198    public void setShortLabel(String shortLabel) {
199        this.shortLabel = shortLabel;
200    }
201
202    /**
203     * @since 5.9.3
204     */
205    public boolean isVirtual() {
206        return virtual;
207    }
208
209    /**
210     * @since 5.9.3
211     */
212    public void setVirtual(boolean virtual) {
213        this.virtual = virtual;
214    }
215
216    /**
217     * @since 8.3
218     */
219    public String getDescription() {
220        return description;
221    }
222
223    /**
224     * @since 8.3
225     */
226    public void setDescription(String description) {
227        this.description = description;
228    }
229
230    /**
231     * @return true if can only be downloaded from the Marketplace, not from {@link DownloadPackage#getDownloadUrl()}
232     * @since 7.1
233     */
234    public boolean isLaterDownload() {
235        return StringUtils.isBlank(getDownloadUrl()) || getFilename() == null || "".equals(getFilename());
236    }
237
238}