1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import com.framsticks.util.lang.Casting; |
---|
4 | import com.framsticks.util.lang.Numbers; |
---|
5 | import org.apache.log4j.Logger; |
---|
6 | |
---|
7 | import java.util.*; |
---|
8 | |
---|
9 | /** |
---|
10 | * @author Piotr Sniegowski |
---|
11 | */ |
---|
12 | public class UniqueListAccess extends ListAccess { |
---|
13 | |
---|
14 | private static final Logger log = Logger.getLogger(UniqueListAccess.class.getName()); |
---|
15 | |
---|
16 | Map<String, Object> map; |
---|
17 | |
---|
18 | final String uidName; |
---|
19 | |
---|
20 | public UniqueListAccess(AccessInterface elementAccess, String uidName) { |
---|
21 | super(elementAccess); |
---|
22 | this.uidName = uidName; |
---|
23 | } |
---|
24 | |
---|
25 | @Override |
---|
26 | public Map<String, Object> createAccessee() { |
---|
27 | return new HashMap<String, Object>(); |
---|
28 | } |
---|
29 | |
---|
30 | @Override |
---|
31 | public Param getParam(int i) { |
---|
32 | log.error("accesing unique list through index"); |
---|
33 | assert false; |
---|
34 | return null; |
---|
35 | //log.error("accesing unique list through index"); |
---|
36 | //return null; |
---|
37 | //return Param.build().setId(Integer.toString(i)).setName(elementAccess.getId()).type("o " + elementAccess.getId()).build(); |
---|
38 | } |
---|
39 | |
---|
40 | @Override |
---|
41 | public Param getParam(String id) { |
---|
42 | Integer i = Numbers.parse(id, Integer.class); |
---|
43 | if (i != null) { |
---|
44 | return getParam(i); |
---|
45 | } |
---|
46 | //TODO list access here |
---|
47 | return Param.build().id(id).name(elementAccess.getId()).type("o " + elementAccess.getId()).finish(); |
---|
48 | } |
---|
49 | |
---|
50 | @Override |
---|
51 | public String getId() { |
---|
52 | return "l " + elementAccess.getId() + " " + uidName; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public int getParamCount() { |
---|
57 | return map.size(); |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public <T> T get(int i, Class<T> type) { |
---|
62 | log.error("accesing unique list through index"); |
---|
63 | assert false; |
---|
64 | return null; |
---|
65 | //Object[] values = map.values().toArray(); |
---|
66 | //return Casting.tryCast(i < values.length ? values[i] : null, type); |
---|
67 | } |
---|
68 | |
---|
69 | @Override |
---|
70 | public <T> T get(String id, Class<T> type) { |
---|
71 | Integer i = Numbers.parse(id, Integer.class); |
---|
72 | if (i != null) { |
---|
73 | return get(i, type); |
---|
74 | } |
---|
75 | return Casting.tryCast(type, map.containsKey(id) ? map.get(id) : null); |
---|
76 | } |
---|
77 | |
---|
78 | @Override |
---|
79 | public <T> T get(ValueParam param, Class<T> type) { |
---|
80 | return get(param.getId(), type); |
---|
81 | } |
---|
82 | |
---|
83 | public String getUidOf(Object value) { |
---|
84 | Object tmp = elementAccess.getSelected(); |
---|
85 | elementAccess.select(value); |
---|
86 | String uid = elementAccess.get(uidName, String.class); |
---|
87 | elementAccess.select(tmp); |
---|
88 | if (uid == null) { |
---|
89 | return null; |
---|
90 | } |
---|
91 | return uid; |
---|
92 | } |
---|
93 | |
---|
94 | protected int setByUid(Object object, String uid) { |
---|
95 | if (uid == null) { |
---|
96 | uid = getUidOf(object); |
---|
97 | if (uid == null) { |
---|
98 | log.error("failed to set - missing uid"); |
---|
99 | return 0; |
---|
100 | } |
---|
101 | } |
---|
102 | if (object == null) { |
---|
103 | map.remove(uid); |
---|
104 | } else { |
---|
105 | map.put(uid, object); |
---|
106 | } |
---|
107 | return 0; |
---|
108 | } |
---|
109 | |
---|
110 | @Override |
---|
111 | public <T> int set(int i, T value) { |
---|
112 | log.error("accesing unique list through index"); |
---|
113 | //return setByUid(value, null); |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | @Override |
---|
118 | public <T> int set(String id, T value) { |
---|
119 | Integer i = Numbers.parse(id, Integer.class); |
---|
120 | if (i != null) { |
---|
121 | return set(i, value); |
---|
122 | } |
---|
123 | if (value == null) { |
---|
124 | return setByUid(null, id); |
---|
125 | } |
---|
126 | String uid = getUidOf(value); |
---|
127 | if (uid != null && id != null) { |
---|
128 | if (!id.equals(uid)) { |
---|
129 | log.error("uid mismatch with set key"); |
---|
130 | return 0; |
---|
131 | } |
---|
132 | setByUid(value, uid); |
---|
133 | return 0; |
---|
134 | } |
---|
135 | if (uid != null) { |
---|
136 | setByUid(value, uid); |
---|
137 | return 0; |
---|
138 | } |
---|
139 | if (id != null) { |
---|
140 | setByUid(value, id); |
---|
141 | return 0; |
---|
142 | } |
---|
143 | log.error("missing both uid and id - failed to set"); |
---|
144 | return 0; |
---|
145 | } |
---|
146 | |
---|
147 | @Override |
---|
148 | public <T> int set(ValueParam param, T value) { |
---|
149 | return set(param.getId(), value); |
---|
150 | } |
---|
151 | |
---|
152 | @Override |
---|
153 | public void clearValues() { |
---|
154 | map.clear(); |
---|
155 | } |
---|
156 | |
---|
157 | @SuppressWarnings("unchecked") |
---|
158 | @Override |
---|
159 | public UniqueListAccess select(Object object) { |
---|
160 | assert (object instanceof Map); |
---|
161 | map = (Map<String, Object>) object; |
---|
162 | return this; |
---|
163 | } |
---|
164 | |
---|
165 | @Override |
---|
166 | public Object getSelected() { |
---|
167 | return map; |
---|
168 | } |
---|
169 | |
---|
170 | @Override |
---|
171 | public UniqueListAccess cloneAccess() { |
---|
172 | return new UniqueListAccess(elementAccess.cloneAccess(), uidName); |
---|
173 | } |
---|
174 | |
---|
175 | @Override |
---|
176 | public String computeIdentifierFor(Object selected) { |
---|
177 | String uid = getUidOf(selected); |
---|
178 | if (uid == null) { |
---|
179 | log.error("missing uid field"); |
---|
180 | return null; |
---|
181 | } |
---|
182 | return uid; |
---|
183 | } |
---|
184 | |
---|
185 | @Override |
---|
186 | public Collection<Param> getParams() { |
---|
187 | List<Param> result = new LinkedList<Param>(); |
---|
188 | if (map == null) { |
---|
189 | return result; |
---|
190 | } |
---|
191 | for (String k : map.keySet()) { |
---|
192 | result.add(getParam(k)); |
---|
193 | } |
---|
194 | return result; |
---|
195 | } |
---|
196 | } |
---|