1 | |
package org.jtheque.primary.dao.impl; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.persistence.GenericDao; |
20 | |
import org.jtheque.core.managers.persistence.Query; |
21 | |
import org.jtheque.core.managers.persistence.QueryMapper; |
22 | |
import org.jtheque.core.managers.persistence.able.Entity; |
23 | |
import org.jtheque.core.managers.persistence.context.IDaoPersistenceContext; |
24 | |
import org.jtheque.primary.PrimaryUtils; |
25 | |
import org.jtheque.primary.dao.able.IDaoLendings; |
26 | |
import org.jtheque.primary.dao.able.IDaoPersons; |
27 | |
import org.jtheque.primary.od.able.Lending; |
28 | |
import org.jtheque.primary.od.impl.LendingImpl; |
29 | |
import org.jtheque.utils.StringUtils; |
30 | |
import org.jtheque.utils.bean.IntDate; |
31 | |
import org.springframework.jdbc.core.simple.ParameterizedRowMapper; |
32 | |
|
33 | |
import javax.annotation.Resource; |
34 | |
import java.sql.ResultSet; |
35 | |
import java.sql.SQLException; |
36 | |
import java.util.ArrayList; |
37 | |
import java.util.Collection; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public final class DaoLendings extends GenericDao<Lending> implements IDaoLendings { |
45 | 0 | private final ParameterizedRowMapper<Lending> rowMapper = new LendingRowMapper(); |
46 | 0 | private final QueryMapper queryMapper = new LendingQueryMapper(); |
47 | |
|
48 | |
@Resource |
49 | |
private IDaoPersistenceContext daoPersistenceContext; |
50 | |
|
51 | |
@Resource |
52 | |
private IDaoPersons daoPersons; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public DaoLendings(){ |
58 | 0 | super(TABLE); |
59 | 0 | } |
60 | |
|
61 | |
@Override |
62 | |
public void create(Lending entity){ |
63 | 0 | entity.setPrimaryImpl(PrimaryUtils.getPrimaryImpl()); |
64 | |
|
65 | 0 | super.create(entity); |
66 | 0 | } |
67 | |
|
68 | |
@Override |
69 | |
public Lending getLending(int id){ |
70 | 0 | return get(id); |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
protected ParameterizedRowMapper<Lending> getRowMapper(){ |
75 | 0 | return rowMapper; |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
protected QueryMapper getQueryMapper(){ |
80 | 0 | return queryMapper; |
81 | |
} |
82 | |
|
83 | |
@Override |
84 | |
protected void loadCache(){ |
85 | 0 | Collection<Lending> lendings = daoPersistenceContext.getSortedList(TABLE, rowMapper); |
86 | |
|
87 | 0 | for (Lending lending : lendings){ |
88 | 0 | getCache().put(lending.getId(), lending); |
89 | |
} |
90 | |
|
91 | 0 | setCacheEntirelyLoaded(); |
92 | 0 | } |
93 | |
|
94 | |
@Override |
95 | |
public Collection<Lending> getLendings(){ |
96 | 0 | return getLendings(PrimaryUtils.getPrimaryImpl()); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
private Collection<Lending> getLendings(CharSequence impl){ |
107 | 0 | if (StringUtils.isEmpty(impl)){ |
108 | 0 | return getAll(); |
109 | |
} |
110 | |
|
111 | 0 | load(); |
112 | |
|
113 | 0 | Collection<Lending> lendings = new ArrayList<Lending>(getCache().size() / 2); |
114 | |
|
115 | 0 | for (Lending lending : getCache().values()){ |
116 | 0 | if (impl.equals(lending.getPrimaryImpl())){ |
117 | 0 | lendings.add(lending); |
118 | |
} |
119 | |
} |
120 | |
|
121 | 0 | return lendings; |
122 | |
} |
123 | |
|
124 | |
@Override |
125 | |
protected void load(int i){ |
126 | 0 | Lending lending = daoPersistenceContext.getDataByID(TABLE, i, rowMapper); |
127 | |
|
128 | 0 | getCache().put(i, lending); |
129 | 0 | } |
130 | |
|
131 | |
@Override |
132 | |
public Lending createLending(){ |
133 | 0 | return new LendingImpl(); |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | 0 | private final class LendingRowMapper implements ParameterizedRowMapper<Lending> { |
142 | |
@Override |
143 | |
public Lending mapRow(ResultSet rs, int i) throws SQLException{ |
144 | 0 | Lending lending = createLending(); |
145 | |
|
146 | 0 | lending.setId(rs.getInt("ID")); |
147 | 0 | lending.setTheOther(rs.getInt("THE_OTHER_FK")); |
148 | 0 | lending.setThePerson(daoPersons.getPerson(rs.getInt("THE_PERSON_FK"))); |
149 | 0 | lending.setDate(new IntDate(rs.getInt("DATE"))); |
150 | 0 | lending.setPrimaryImpl(rs.getString("IMPL")); |
151 | |
|
152 | 0 | return lending; |
153 | |
} |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | 0 | private static final class LendingQueryMapper implements QueryMapper { |
162 | |
@Override |
163 | |
public Query constructInsertQuery(Entity entity){ |
164 | 0 | String query = "INSERT INTO " + TABLE + " (THE_OTHER_FK, THE_PERSON_FK, DATE, IMPL) VALUES(?,?,?, ?)"; |
165 | |
|
166 | 0 | return new Query(query, fillArray((Lending) entity, false)); |
167 | |
} |
168 | |
|
169 | |
@Override |
170 | |
public Query constructUpdateQuery(Entity entity){ |
171 | 0 | String query = "UPDATE " + TABLE + " SET THE_OTHER_FK = ?, THE_BORROWER_FK = ?, DATE = ?, IMPL = ? WHERE ID = ?"; |
172 | |
|
173 | 0 | return new Query(query, fillArray((Lending) entity, true)); |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
private static Object[] fillArray(Lending lending, boolean id){ |
185 | 0 | Object[] values = new Object[4 + (id ? 1 : 0)]; |
186 | |
|
187 | 0 | values[0] = lending.getTheOther(); |
188 | 0 | values[1] = lending.getThePerson().getId(); |
189 | 0 | values[2] = lending.getDate().intValue(); |
190 | 0 | values[3] = lending.getPrimaryImpl(); |
191 | |
|
192 | 0 | if (id){ |
193 | 0 | values[4] = lending.getId(); |
194 | |
} |
195 | |
|
196 | 0 | return values; |
197 | |
} |
198 | |
} |
199 | |
} |