/
UpdatePackagesModel.java
209 lines (188 loc) · 7.42 KB
/
UpdatePackagesModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* RapidMiner
*
* Copyright (C) 2001-2014 by RapidMiner and the contributors
*
* Complete list of developers available at our web site:
*
* http://rapidminer.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.rapid_i.deployment.update.client;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Observable;
import java.util.Set;
import com.rapidminer.RapidMiner;
import com.rapidminer.deployment.client.wsimport.PackageDescriptor;
import com.rapidminer.gui.tools.SwingTools;
import com.rapidminer.tools.Tools;
import com.rapidminer.tools.plugin.Dependency;
/**
* @author Simon Fischer, Dominik Halfkann
*/
public class UpdatePackagesModel extends Observable {
private final Map<PackageDescriptor, Boolean> selectionMap = new HashMap<PackageDescriptor, Boolean>();
private UpdateServerAccount usAccount;
/** Read the comment of {@link #isPurchased(PackageDescriptor)}. */
private Set<String> purchasedPackages = new HashSet<String>();
private PackageDescriptorCache cache;
public UpdatePackagesModel(PackageDescriptorCache cache, final UpdateServerAccount usAccount) {
this.cache = cache;
this.usAccount = usAccount;
}
/** Should only be accessed within a thread. **/
public void updatePurchasedPackages() {
if (usAccount.isLoggedIn()) {
try {
purchasedPackages = new HashSet<String>(UpdateManager.getAccountService().getLicensedProducts());
UpdatePackagesModel.this.setChanged();
UpdatePackagesModel.this.notifyObservers();
} catch (Exception e1) {
SwingTools.showSimpleErrorMessage("error_accessing_marketplace_account", e1, e1.getMessage());
purchasedPackages = new HashSet<String>();
}
} else {
purchasedPackages = new HashSet<String>();
}
}
public void clearPurchasedPackages() {
if (!usAccount.isLoggedIn()) {
purchasedPackages = new HashSet<String>();
for (Map.Entry<PackageDescriptor, Boolean> selectionEntry : selectionMap.entrySet()) {
if (selectionEntry.getKey().isRestricted() && selectionEntry.getValue()) {
toggleSelectionForInstallation(selectionEntry.getKey());
}
}
}
}
public void setSelectedForInstallation(PackageDescriptor desc, boolean selected) {
selectionMap.put(desc, true);
}
public void forceNotifyObservers() {
setChanged();
notifyObservers();
}
public void toggleSelectionForInstallation(PackageDescriptor desc) {
if (desc != null) {
boolean select = !isSelectedForInstallation(desc);
if (isUpToDate(desc)) {
select = false;
}
if (UpdateManager.PACKAGE_TYPE_RAPIDMINER_PLUGIN.equals(desc.getPackageTypeName())) {
} else if (UpdateManager.PACKAGE_TYPE_STAND_ALONE.equals(desc.getPackageTypeName())) {
String longVersion = RapidMiner.getLongVersion();
String myVersion = ManagedExtension.normalizeVersion(longVersion);
String remoteVersion = ManagedExtension.normalizeVersion(desc.getVersion());
if ((myVersion != null) && (remoteVersion.compareTo(myVersion) <= 0)) {
select = false;
}
}
if (UpdateManager.COMMERCIAL_LICENSE_NAME.equals(desc.getLicenseName()) && !isPurchased(desc)) {
select = false;
SwingTools.showMessageDialog("purchase_package", desc.getName());
}
selectionMap.put(desc, select);
this.setChanged();
this.notifyObservers(desc);
}
}
public void clearFromSelectionMap(List<PackageDescriptor> toClear) {
for(PackageDescriptor desc : toClear) {
selectionMap.remove(desc);
this.setChanged();
this.notifyObservers(desc);
}
}
public boolean isSelectedForInstallation(PackageDescriptor desc) {
Boolean selected = selectionMap.get(desc);
return (selected != null) && selected.booleanValue();
}
public List<PackageDescriptor> getInstallationList() {
List<PackageDescriptor> downloadList = new LinkedList<PackageDescriptor>();
for (Entry<PackageDescriptor, Boolean> entry : selectionMap.entrySet()) {
if (entry.getValue()) {
downloadList.add(entry.getKey());
}
}
return downloadList;
}
public boolean isUpToDate(PackageDescriptor desc) {
ManagedExtension ext = ManagedExtension.get(desc.getPackageId());
if (ext != null) {
String remoteVersion = ManagedExtension.normalizeVersion(desc.getVersion());
String myVersion = ManagedExtension.normalizeVersion(ext.getLatestInstalledVersion());
if ((myVersion != null) && (remoteVersion.compareTo(myVersion) <= 0)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Currently, this is an unused feature. There are no extensions that can be purchased. Don't be afraid, RapidMiner
* is, and will always be, open source and free. However, future extensions like connectors to SAP or other data
* sources requiring proprietary drivers with expensive license fees may only be available on a commercial basis,
* for obvious reasons :-)
*/
public boolean isPurchased(PackageDescriptor desc) {
return purchasedPackages.contains(desc.getPackageId());
}
public String getExtensionURL(PackageDescriptor descriptor) {
return UpdateManager.getBaseUrl() + "/faces/product_details.xhtml?productId=" + descriptor.getPackageId();
}
public String toString(PackageDescriptor descriptor, String changes) {
StringBuilder b = new StringBuilder("<html><body>");
b.append("<h1>");
b.append(descriptor.getName());
if (descriptor.isRestricted()) {
b.append(" <img src=\"icon:///").append("16/currency_euro.png").append("\"/>");
}
b.append("</h1><hr/>");
Date date = new Date(descriptor.getCreationTime().toGregorianCalendar().getTimeInMillis());
String keyStyle=" style=\"padding-right:5px;color:gray;width:80px;\"";
b.append("<table style=\"margin:5px 0 10px 5px\"><thead><tr><td "+keyStyle+">Version</td><td>").append(descriptor.getVersion());
b.append("</td></tr><tr><td "+keyStyle+">Release date</td><td>").append(Tools.formatDate(date));
b.append("</td></tr><tr><td "+keyStyle+">File size</td><td>").append(Tools.formatBytes(descriptor.getSize())).append("</td></tr>");
b.append("</td></tr><tr><td "+keyStyle+">License</td><td>").append(descriptor.getLicenseName()).append("</td></tr>");
if ((descriptor.getDependencies() != null) && !descriptor.getDependencies().isEmpty()) {
b.append("<tr><td "+keyStyle+">Dependencies</td><td>");
boolean first = true;
for (Dependency dependency : Dependency.parse(descriptor.getDependencies())) {
if (!first) {
b.append(", ");
} else {
first = false;
}
b.append(cache.getPackageInfo(dependency.getPluginExtensionId()).getName());
}
b.append("</td></tr>");
}
b.append("</table><div>").append(descriptor.getLongDescription()).append("</div>");
if (changes != null && !changes.trim().equals("")) {
b.append("<h2>Changes</h2>");
b.append(changes);
}
b.append("</body></html>");
return b.toString();
}
}