1 | package geniusweb.domaineditor.model;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
12 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
13 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
14 |
|
---|
15 | import geniusweb.domaineditor.ProfileEditor;
|
---|
16 | import geniusweb.issuevalue.Domain;
|
---|
17 | import geniusweb.issuevalue.ValueSet;
|
---|
18 | import tudelft.utilities.listener.DefaultListenable;
|
---|
19 | import tudelft.utilities.logging.Reporter;
|
---|
20 | import tudelft.utilities.mvc.model.DefaultMapModel;
|
---|
21 | import tudelft.utilities.mvc.model.MapModel;
|
---|
22 | import tudelft.utilities.mvc.model.Model;
|
---|
23 | import tudelft.utilities.mvc.model.StringModel;
|
---|
24 | import tudelft.utilities.mvc.model.TypedModel;
|
---|
25 | import tudelft.utilities.mvc.model.events.Changed;
|
---|
26 | import tudelft.utilities.mvc.model.events.Event;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Contains a mutable {@link Domain}. Listeners are notified about any changes
|
---|
30 | * and can act accordingly. Notifications contain WHAT changed: the name of the
|
---|
31 | * issue, or "" if the NAME changed of the domain. For internal use in the
|
---|
32 | * {@link ProfileEditor} and related
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class DomainModel extends DefaultListenable<Event>
|
---|
36 | implements TypedModel<Domain> {
|
---|
37 | private static final ObjectMapper jackson = new ObjectMapper();
|
---|
38 | private final StringModel domainname;
|
---|
39 | private final MapModel<StringModel, ValueSetModel> issuesmodel;
|
---|
40 | private final Reporter log;
|
---|
41 |
|
---|
42 | public DomainModel(StringModel name,
|
---|
43 | MapModel<StringModel, ValueSetModel> issues, Reporter log) {
|
---|
44 | if (issues.getMinimumSize() != 1)
|
---|
45 | throw new IllegalArgumentException(
|
---|
46 | "domain must have minimum size of at least 1 issue");
|
---|
47 | this.domainname = name;
|
---|
48 | this.issuesmodel = issues;
|
---|
49 | this.log = log;
|
---|
50 | DomainModel that = this;
|
---|
51 | domainname.addListener((e) -> notifyListeners(new Changed(this, e)));
|
---|
52 | // dont wrap issuemodel events into another Changed, it's already in one
|
---|
53 | issuesmodel.addListener((e) -> notifyListeners(e));
|
---|
54 | }
|
---|
55 |
|
---|
56 | public DomainModel(Reporter log) {
|
---|
57 | this(new SimpleStringModel("domainname", log),
|
---|
58 | new DefaultMapModel<StringModel, ValueSetModel>(
|
---|
59 | Collections.singletonMap(new StringModel("issue", log),
|
---|
60 | new DiscreteValueSetModel(log)),
|
---|
61 | log) {
|
---|
62 | @Override
|
---|
63 | public int getMinimumSize() {
|
---|
64 | return 1;
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public String getColumnName(int n) {
|
---|
69 | return n == 0 ? "issue" : "value";
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public ValueSetModel create(StringModel key) {
|
---|
74 | throw new UnsupportedOperationException();
|
---|
75 | }
|
---|
76 |
|
---|
77 | }, log);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Create the domain from a file.
|
---|
82 | *
|
---|
83 | * @param file the file containing the domain
|
---|
84 | * @throws Exception if problem with the file
|
---|
85 | */
|
---|
86 | public static DomainModel create(File file, Reporter log)
|
---|
87 | throws JsonParseException, JsonMappingException, IOException {
|
---|
88 | Domain domain = jackson.readValue(file, Domain.class);
|
---|
89 | // FIXME
|
---|
90 | return new DomainModel(new StringModel(domain.getName(), log), null,
|
---|
91 | log);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | *
|
---|
96 | * @return the model containing the domain name.
|
---|
97 | */
|
---|
98 | public StringModel getName() {
|
---|
99 | return domainname;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | *
|
---|
104 | * @return the model containing the issues-values map
|
---|
105 | */
|
---|
106 | public MapModel<StringModel, ValueSetModel> getIssues() {
|
---|
107 | return issuesmodel;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | *
|
---|
112 | * @return the {@link Domain} contained in this model.
|
---|
113 | */
|
---|
114 | @Override
|
---|
115 | public Domain getCurrentValue() throws IllegalStateException {
|
---|
116 | Map<String, ValueSet> issues = new HashMap<>();
|
---|
117 | for (int n = 0; n < issuesmodel.getKeys().getSize(); n++) {
|
---|
118 | StringModel key = issuesmodel.getKeys().get(n);
|
---|
119 | issues.put(key.getValue(),
|
---|
120 | issuesmodel.getValue(key).getCurrentValue());
|
---|
121 | }
|
---|
122 |
|
---|
123 | return new Domain(domainname.getValue(), issues);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Set new {@link Domain}
|
---|
128 | *
|
---|
129 | * @param obj the new {@link Domain}. This assumes the old domain can be
|
---|
130 | * completely deleted and replaced with this new one. New
|
---|
131 | * {@link Model}s are created where needed.
|
---|
132 | */
|
---|
133 | @Override
|
---|
134 | public void setCurrentValue(Domain dom) throws IllegalArgumentException {
|
---|
135 | domainname.setValue(dom.getName());
|
---|
136 |
|
---|
137 | // collect old issues
|
---|
138 | List<StringModel> oldissues = new LinkedList<>();
|
---|
139 | for (int n = 0; n < issuesmodel.getKeys().getSize(); n++)
|
---|
140 | oldissues.add(issuesmodel.getKeys().get(n));
|
---|
141 |
|
---|
142 | // add new ones first (minimum size)
|
---|
143 | for (String iss : dom.getIssues()) {
|
---|
144 | ValueSetModel valsetmodel = ValueSetModelType.createModel(
|
---|
145 | ValueSetModelType.fromModel(dom.getValues(iss)), log);
|
---|
146 | valsetmodel.setCurrentValue(dom.getValues(iss));
|
---|
147 | issuesmodel.put(new StringModel(iss, log), valsetmodel);
|
---|
148 | }
|
---|
149 | // remove the old issues
|
---|
150 | for (StringModel iss : oldissues)
|
---|
151 | issuesmodel.remove(iss);
|
---|
152 | }
|
---|
153 | }
|
---|