1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import com.framsticks.util.Casting; |
---|
4 | import com.framsticks.util.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 LOGGER = 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 | LOGGER.error("accesing unique list through index"); |
---|
33 | assert false; |
---|
34 | return null; |
---|
35 | //LOGGER.error("accesing unique list through index"); |
---|
36 | //return null; |
---|
37 | //return new ParamBuilder().setId(Integer.toString(i)).setName(elementAccess.getId()).setType("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 | return new ParamBuilder().setId(id).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build(); |
---|
47 | } |
---|
48 | |
---|
49 | @Override |
---|
50 | public String getId() { |
---|
51 | return "l " + elementAccess.getId() + " " + uidName; |
---|
52 | } |
---|
53 | |
---|
54 | @Override |
---|
55 | public int getParamCount() { |
---|
56 | return map.size(); |
---|
57 | } |
---|
58 | |
---|
59 | @Override |
---|
60 | public <T> T get(int i, Class<T> type) { |
---|
61 | LOGGER.error("accesing unique list through index"); |
---|
62 | assert false; |
---|
63 | return null; |
---|
64 | //Object[] values = map.values().toArray(); |
---|
65 | //return Casting.tryCast(i < values.length ? values[i] : null, type); |
---|
66 | } |
---|
67 | |
---|
68 | @Override |
---|
69 | public <T> T get(String id, Class<T> type) { |
---|
70 | Integer i = Numbers.parse(id, Integer.class); |
---|
71 | if (i != null) { |
---|
72 | return get(i, type); |
---|
73 | } |
---|
74 | return Casting.tryCast(type, map.containsKey(id) ? map.get(id) : null); |
---|
75 | } |
---|
76 | |
---|
77 | @Override |
---|
78 | public <T> T get(Param param, Class<T> type) { |
---|
79 | return get(param.getId(), type); |
---|
80 | } |
---|
81 | |
---|
82 | public String getUidOf(Object value) { |
---|
83 | Object tmp = elementAccess.getSelected(); |
---|
84 | elementAccess.select(value); |
---|
85 | String uid = elementAccess.get(uidName, String.class); |
---|
86 | elementAccess.select(tmp); |
---|
87 | if (uid == null) { |
---|
88 | return null; |
---|
89 | } |
---|
90 | return uid; |
---|
91 | } |
---|
92 | |
---|
93 | protected int setByUid(Object object, String uid) { |
---|
94 | if (uid == null) { |
---|
95 | uid = getUidOf(object); |
---|
96 | if (uid == null) { |
---|
97 | LOGGER.error("failed to set - missing uid"); |
---|
98 | return 0; |
---|
99 | } |
---|
100 | } |
---|
101 | if (object == null) { |
---|
102 | map.remove(uid); |
---|
103 | } else { |
---|
104 | map.put(uid, object); |
---|
105 | } |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | @Override |
---|
110 | public <T> int set(int i, T value) { |
---|
111 | LOGGER.error("accesing unique list through index"); |
---|
112 | //return setByUid(value, null); |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | @Override |
---|
117 | public <T> int set(String id, T value) { |
---|
118 | Integer i = Numbers.parse(id, Integer.class); |
---|
119 | if (i != null) { |
---|
120 | return set(i, value); |
---|
121 | } |
---|
122 | if (value == null) { |
---|
123 | return setByUid(null, id); |
---|
124 | } |
---|
125 | String uid = getUidOf(value); |
---|
126 | if (uid != null && id != null) { |
---|
127 | if (!id.equals(uid)) { |
---|
128 | LOGGER.error("uid mismatch with set key"); |
---|
129 | return 0; |
---|
130 | } |
---|
131 | setByUid(value, uid); |
---|
132 | return 0; |
---|
133 | } |
---|
134 | if (uid != null) { |
---|
135 | setByUid(value, uid); |
---|
136 | return 0; |
---|
137 | } |
---|
138 | if (id != null) { |
---|
139 | setByUid(value, id); |
---|
140 | return 0; |
---|
141 | } |
---|
142 | LOGGER.error("missing both uid and id - failed to set"); |
---|
143 | return 0; |
---|
144 | } |
---|
145 | |
---|
146 | @Override |
---|
147 | public <T> int set(Param param, T value) { |
---|
148 | return set(param.getId(), value); |
---|
149 | } |
---|
150 | |
---|
151 | @Override |
---|
152 | public void clearValues() { |
---|
153 | map.clear(); |
---|
154 | } |
---|
155 | |
---|
156 | @Override |
---|
157 | public void select(Object object) { |
---|
158 | map = (Map<String, Object>)object; |
---|
159 | } |
---|
160 | |
---|
161 | @Override |
---|
162 | public Object getSelected() { |
---|
163 | return map; |
---|
164 | } |
---|
165 | |
---|
166 | @Override |
---|
167 | public UniqueListAccess cloneAccess() { |
---|
168 | return new UniqueListAccess(elementAccess.cloneAccess(), uidName); |
---|
169 | } |
---|
170 | |
---|
171 | @Override |
---|
172 | public String computeIdentifierFor(Object selected) { |
---|
173 | String uid = getUidOf(selected); |
---|
174 | if (uid == null) { |
---|
175 | LOGGER.error("missing uid field"); |
---|
176 | return null; |
---|
177 | } |
---|
178 | return uid; |
---|
179 | } |
---|
180 | |
---|
181 | @Override |
---|
182 | public Collection<Param> getParams() { |
---|
183 | List<Param> result = new LinkedList<Param>(); |
---|
184 | if (map == null) { |
---|
185 | return result; |
---|
186 | } |
---|
187 | for (String k : map.keySet()) { |
---|
188 | result.add(getParam(k)); |
---|
189 | } |
---|
190 | return result; |
---|
191 | } |
---|
192 | } |
---|