Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / handler / update / P2Util.java
1 package eu.etaxonomy.taxeditor.handler.update;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.eclipse.equinox.internal.p2.ui.ProvUI;
9 import org.eclipse.equinox.internal.p2.ui.model.ElementUtils;
10 import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
11 import org.eclipse.equinox.p2.repository.IRepository;
12 import org.eclipse.equinox.p2.repository.IRepositoryManager;
13 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
14 import org.eclipse.equinox.p2.ui.ProvisioningUI;
15
16 import eu.etaxonomy.taxeditor.model.MessagingUtils;
17 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
18 import eu.etaxonomy.taxeditor.util.ApplicationUtil;
19
20 /**
21 * This class is a utility class for updating the editor from a p2 update site,
22 * greatly inspired by the links given below.
23 *
24 * To Do :
25 * - Allow configurable update sites
26 *
27 * Notes :
28 *
29 * @see http://wiki.eclipse.org/Equinox/p2/Adding_Self-Update_to_an_RCP_Application
30 * @see http://bugs.eclipse.org/281226
31 * @see http://www.vogella.com/tutorials/EclipseP2Update/article.html
32 */
33 public class P2Util {
34
35 private static String EDIT_NIGHTLY_UPDATE_SITE = "https://cybertaxonomy.eu/download/taxeditor/update/nightly/";
36 private static String EDIT_NIGHTLY_UPDATE_SITE_NAME = "Taxonomic Editor Nightly";
37
38 private static String EDIT_STABLE_UPDATE_SITE = "https://cybertaxonomy.eu/download/taxeditor/update/stable/";
39 private static String EDIT_STABLE_UPDATE_SITE_NAME = "Taxonomic Editor Stable";
40
41 /**
42 * Retrieve and load the saved list of repositories from the preference store,
43 * making sure that at least the default repository is always loaded.
44 */
45 @SuppressWarnings("restriction")
46 public static void setP2UpdateRepositories() {
47 List<MetadataRepositoryElement> repoElements = new ArrayList<>();
48 List<MetadataRepositoryElement> savedRepoElements = new ArrayList<>();
49
50 MetadataRepositoryElement mre = new MetadataRepositoryElement(null, getP2UpdateRepository(), true);
51 mre.setNickname(getP2UpdateRepositoryName());
52 savedRepoElements.add(mre);
53
54 repoElements.addAll(savedRepoElements);
55
56 ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),repoElements
57 .toArray(new MetadataRepositoryElement[]{} ));
58 }
59
60 public static String getP2UpdateRepositoryName(){
61 return ApplicationUtil.isStable()? EDIT_STABLE_UPDATE_SITE_NAME : EDIT_NIGHTLY_UPDATE_SITE_NAME;
62 }
63
64 public static URI getP2UpdateRepository(){
65 try {
66 return ApplicationUtil.isStable()? new URI(EDIT_STABLE_UPDATE_SITE) : new URI(EDIT_NIGHTLY_UPDATE_SITE);
67 } catch (URISyntaxException e) {
68 MessagingUtils.error(P2Util.class, e);
69 }
70 return null;
71 }
72
73 /**
74 * {@link org.eclipse.equinox.p2.ui.RepositoryManipulationPage} which handles the repsitory site list
75 * in preferences does not create a preference store and hence the changes are not saved. This means
76 * that we need to save it ourselves.
77 *
78 * This method saves the list of current repositories in the preference store as a string with
79 * specific delimiters.
80 */
81
82 public static void saveP2RepositoryPreferences() {
83
84 IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
85
86 URI[] currentlyEnabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
87 URI[] currentlyDisabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_DISABLED);
88
89 List<MetadataRepositoryElement> repoElements = new ArrayList<>();
90
91 for(URI repo : currentlyEnabled) {
92 boolean enabled = true;
93 String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
94 MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
95 element.setNickname(nickname);
96 element.setEnabled(enabled);
97 repoElements.add(element);
98 }
99
100 for(URI repo : currentlyDisabled) {
101 boolean enabled = false;
102 String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
103 MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
104 element.setNickname(nickname);
105 element.setEnabled(enabled);
106 repoElements.add(element);
107 }
108 PreferencesUtil.setP2Repositories(repoElements);
109 }
110 }