source: src/main/java/genius/gui/negosession/ContentProxy.java@ 346

Last change on this file since 346 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 6.3 KB
Line 
1package genius.gui.negosession;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import genius.core.repository.DomainRepItem;
7import genius.core.repository.MultiPartyProtocolRepItem;
8import genius.core.repository.ParticipantRepItem;
9import genius.core.repository.PartyRepItem;
10import genius.core.repository.ProfileRepItem;
11import genius.core.repository.RepItem;
12import genius.core.repository.Repository;
13import genius.core.repository.RepositoryFactory;
14
15/**
16 * Proxy service to the {@link genius.core.repository.Repository}. This abstracts
17 * all the plumbing and lets the GUI/Config just grab the requested values
18 */
19public final class ContentProxy {
20 /**
21 * Use the repository object to fetch protocols
22 *
23 * @return A list of MultiPartyProtocolRepItem represented in
24 * multipartprotocolsrepository.xml
25 */
26 public static List<MultiPartyProtocolRepItem> fetchProtocols() {
27 // initialize original list and casted list
28 List<MultiPartyProtocolRepItem> items = RepositoryFactory.getMultiPartyProtocolRepository().getItems();
29 List<MultiPartyProtocolRepItem> itemsCasted = new ArrayList<MultiPartyProtocolRepItem>(items.size());
30
31 // cast list element-wise
32 for (RepItem item : items)
33 itemsCasted.add((MultiPartyProtocolRepItem) item);
34
35 // return casted list
36 return itemsCasted;
37 }
38
39 /**
40 * Use the repository object to fetch domains
41 *
42 * @return A list of MultiPartyProtocolRepItem represented in
43 * domainrepository.xml
44 */
45
46 public static List<DomainRepItem> fetchDomains() {
47
48 Repository<DomainRepItem> domainrep = RepositoryFactory.get_domain_repos();
49 List<DomainRepItem> domList = new ArrayList<DomainRepItem>();
50 for (DomainRepItem domain : domainrep.getItems()) {
51 domList.add(domain);
52 }
53 return domList;
54 }
55
56
57
58 /**
59 * Use the repository object to fetch non-mediator parties
60 *
61 * @return All parties as defined in partyrepository.xml that have mediator
62 * not set (or set to false)
63 */
64 public static List<ParticipantRepItem> fetchParties() {
65 // initialize original list and casted list
66 List<ParticipantRepItem> items = new ArrayList<>();
67 items.addAll(RepositoryFactory.get_party_repository().getItems());
68 items.addAll(RepositoryFactory.getBoaPartyRepository().getList().getList());
69 List<ParticipantRepItem> itemsCasted = new ArrayList<>();
70
71 // cast list element-wise and remove mediators
72 for (RepItem item : items) {
73 ParticipantRepItem prItem = (ParticipantRepItem) item;
74 if (!prItem.isMediator())
75 itemsCasted.add(prItem);
76 }
77
78 // return casted list
79 return itemsCasted;
80 }
81
82 /**
83 * Use the repository object to fetch non-mediator parties
84 *
85 * @param protocol
86 * The protocol the requested parties should support
87 * @return All parties as defined in partyrepository.xml that have mediator
88 * not set (or set to false) and have protocol set to the given
89 * protocol
90 */
91 public static List<ParticipantRepItem> fetchPartiesForProtocol(MultiPartyProtocolRepItem protocol) {
92 List<ParticipantRepItem> items = fetchParties();
93 List<ParticipantRepItem> filtered = new ArrayList<ParticipantRepItem>();
94 for (ParticipantRepItem item : items)
95 if (item.getProtocolClassPath().equals(protocol.getClassPath()))
96 filtered.add(item);
97
98 return filtered;
99 }
100
101 /**
102 * Use the repository object to fetch mediator parties
103 *
104 * @return All the parties as defined in partyrepository.xml that have
105 * mediator set to true
106 */
107 public static List<PartyRepItem> fetchMediators() {
108 // initialize original list and casted list
109 List<PartyRepItem> items = RepositoryFactory.get_party_repository().getItems();
110 List<PartyRepItem> itemsCasted = new ArrayList<PartyRepItem>(items.size());
111
112 // cast list element-wise and only add mediators
113 for (RepItem item : items) {
114 PartyRepItem prItem = (PartyRepItem) item;
115 if (prItem.isMediator())
116 itemsCasted.add(prItem);
117 }
118
119 // return casted list
120 return itemsCasted;
121 }
122
123 /**
124 * Use the repository object to fetch mediator parties
125 *
126 * @param protocol
127 * The protocol the requested mediator should support
128 * @return All the parties as defined in partyrepository.xml that have
129 * mediator set to true and have protocol set to the given protocol
130 */
131 public static List<PartyRepItem> fetchMediatorsForProtocol(MultiPartyProtocolRepItem protocol) {
132 List<PartyRepItem> items = fetchMediators();
133 List<PartyRepItem> filtered = new ArrayList<PartyRepItem>(items.size());
134 for (PartyRepItem item : items)
135 if (item.getProtocolClassPath().equals(protocol.getClassPath()))
136 filtered.add(item);
137
138 return filtered;
139 }
140
141 /**
142 * Use the repository object to fetch profiles
143 *
144 * @return All the profiles as defined in domainrepository.xmls
145 */
146 public static List<ProfileRepItem> fetchProfiles() {
147 try {
148 Repository<DomainRepItem> domainrep = RepositoryFactory.get_domain_repos();
149 ArrayList<ProfileRepItem> profiles = new ArrayList<ProfileRepItem>();
150 for (RepItem domain : domainrep.getItems()) {
151 for (ProfileRepItem profile : ((DomainRepItem) domain).getProfiles())
152 profiles.add(profile);
153 }
154 return profiles;
155 } catch (Exception e) {
156 e.printStackTrace();
157 return new ArrayList<ProfileRepItem>();
158 }
159
160 }
161
162 /**
163 * Use the repository object to fetch profiles corresponding to a PARTICULAR domain
164 *
165 * @return All the profiles associated with the domain defined in domainrepository.xmls
166 */
167
168 public static List<ProfileRepItem> fetchDomainSpecificProfiles(DomainRepItem domRepItem) {
169 List<ProfileRepItem> profiles = new ArrayList<ProfileRepItem>();
170 for (ProfileRepItem profile : domRepItem.getProfiles())
171 profiles.add(profile);
172 return profiles;
173 }
174
175
176
177 /**
178 * When fetching a list of type PartyRepItem, you must also provide the
179 * protocol. Otherwise it can be null.
180 *
181 * @param elementType
182 * can be ProfileRepItem.class or PartyRepItem.class and must be
183 * equal to T.
184 * @param protocol
185 * @return list of objects of type T in the repository of type T.
186 */
187 public static <T extends RepItem> List<T> fetchList(Class<T> elementType, MultiPartyProtocolRepItem protocol) {
188 if (elementType == ProfileRepItem.class) {
189 return (List<T>) fetchProfiles();
190 }
191 if (elementType == PartyRepItem.class) {
192 return (List<T>) fetchPartiesForProtocol(protocol);
193 }
194 throw new IllegalArgumentException("unhandled type " + elementType);
195 }
196}
Note: See TracBrowser for help on using the repository browser.