| 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.core.utils.db.DaoNotes; |
| 25 | |
import org.jtheque.primary.dao.able.IDaoPersons; |
| 26 | |
import org.jtheque.primary.dao.able.IDaoSimpleDatas; |
| 27 | |
import org.jtheque.primary.od.able.Person; |
| 28 | |
import org.jtheque.primary.od.impl.PersonImpl; |
| 29 | |
import org.jtheque.utils.StringUtils; |
| 30 | |
import org.springframework.jdbc.core.simple.ParameterizedRowMapper; |
| 31 | |
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; |
| 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 DaoPersons extends GenericDao<Person> implements IDaoPersons { |
| 45 | 0 | private final ParameterizedRowMapper<Person> rowMapper = new PersonRowMapper(); |
| 46 | 0 | private final QueryMapper queryMapper = new PersonQueryMapper(); |
| 47 | |
|
| 48 | |
@Resource |
| 49 | |
private IDaoPersistenceContext daoPersistenceContext; |
| 50 | |
|
| 51 | |
@Resource |
| 52 | |
private SimpleJdbcTemplate jdbcTemplate; |
| 53 | |
|
| 54 | |
@Resource |
| 55 | |
private IDaoSimpleDatas daoCountries; |
| 56 | |
|
| 57 | 0 | private final DaoNotes daoNotes = DaoNotes.getInstance(); |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public DaoPersons(){ |
| 63 | 0 | super(TABLE); |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
@Override |
| 67 | |
public Collection<Person> getPersons(String type){ |
| 68 | 0 | return getAll(type); |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private Collection<Person> getAll(CharSequence type){ |
| 79 | 0 | if (StringUtils.isEmpty(type)){ |
| 80 | 0 | return getAll(); |
| 81 | |
} |
| 82 | |
|
| 83 | 0 | load(); |
| 84 | |
|
| 85 | 0 | Collection<Person> persons = new ArrayList<Person>(getCache().size() / 3); |
| 86 | |
|
| 87 | 0 | for (Person person : getCache().values()){ |
| 88 | 0 | if (type.equals(person.getType())){ |
| 89 | 0 | persons.add(person); |
| 90 | |
} |
| 91 | |
} |
| 92 | |
|
| 93 | 0 | return persons; |
| 94 | |
} |
| 95 | |
|
| 96 | |
@Override |
| 97 | |
public Person getPerson(int id){ |
| 98 | 0 | return get(id); |
| 99 | |
} |
| 100 | |
|
| 101 | |
@Override |
| 102 | |
public Person getPerson(String firstName, String name, String type){ |
| 103 | 0 | load(); |
| 104 | |
|
| 105 | 0 | if (getCache().isEmpty()){ |
| 106 | 0 | return null; |
| 107 | |
} |
| 108 | |
|
| 109 | 0 | for (Person person : getCache().values()){ |
| 110 | 0 | if (type.equals(person.getType()) && firstName.equals(person.getFirstName()) && name.equals(person.getName())){ |
| 111 | 0 | return person; |
| 112 | |
} |
| 113 | |
} |
| 114 | |
|
| 115 | 0 | return null; |
| 116 | |
} |
| 117 | |
|
| 118 | |
@Override |
| 119 | |
public boolean exists(String firstName, String name, String type){ |
| 120 | 0 | return getPerson(firstName, name, type) != null; |
| 121 | |
} |
| 122 | |
|
| 123 | |
@Override |
| 124 | |
public boolean exist(Person person){ |
| 125 | 0 | return getPerson(person.getFirstName(), person.getName(), person.getType()) != null; |
| 126 | |
} |
| 127 | |
|
| 128 | |
@Override |
| 129 | |
public Person createPerson(){ |
| 130 | 0 | return new PersonImpl(); |
| 131 | |
} |
| 132 | |
|
| 133 | |
@Override |
| 134 | |
public void clearAll(String type){ |
| 135 | 0 | Collection<Person> persons = getPersons(type); |
| 136 | |
|
| 137 | 0 | jdbcTemplate.update("DELETE FROM " + TABLE + " WHERE TYPE = ?", type); |
| 138 | |
|
| 139 | 0 | for (Person person : persons){ |
| 140 | 0 | getCache().remove(person.getId()); |
| 141 | |
} |
| 142 | 0 | } |
| 143 | |
|
| 144 | |
@Override |
| 145 | |
protected void loadCache(){ |
| 146 | 0 | Collection<Person> persons = daoPersistenceContext.getSortedList(TABLE, rowMapper); |
| 147 | |
|
| 148 | 0 | for (Person borrower : persons){ |
| 149 | 0 | getCache().put(borrower.getId(), borrower); |
| 150 | |
} |
| 151 | |
|
| 152 | 0 | setCacheEntirelyLoaded(); |
| 153 | 0 | } |
| 154 | |
|
| 155 | |
@Override |
| 156 | |
protected void load(int i){ |
| 157 | 0 | Person person = daoPersistenceContext.getDataByID(TABLE, i, rowMapper); |
| 158 | |
|
| 159 | 0 | getCache().put(i, person); |
| 160 | 0 | } |
| 161 | |
|
| 162 | |
@Override |
| 163 | |
protected ParameterizedRowMapper<Person> getRowMapper(){ |
| 164 | 0 | return rowMapper; |
| 165 | |
} |
| 166 | |
|
| 167 | |
@Override |
| 168 | |
protected QueryMapper getQueryMapper(){ |
| 169 | 0 | return queryMapper; |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | 0 | private final class PersonRowMapper implements ParameterizedRowMapper<Person> { |
| 178 | |
@Override |
| 179 | |
public Person mapRow(ResultSet rs, int i) throws SQLException{ |
| 180 | 0 | Person person = createPerson(); |
| 181 | |
|
| 182 | 0 | person.setId(rs.getInt("ID")); |
| 183 | 0 | person.setName(rs.getString("NAME")); |
| 184 | 0 | person.setFirstName(rs.getString("FIRST_NAME")); |
| 185 | 0 | person.setEmail(rs.getString("EMAIL")); |
| 186 | 0 | person.setType(rs.getString("TYPE")); |
| 187 | 0 | person.setTheCountry(daoCountries.getSimpleData(rs.getInt("THE_COUNTRY_FK"))); |
| 188 | |
|
| 189 | 0 | if (StringUtils.isNotEmpty(rs.getString("NOTE"))){ |
| 190 | 0 | person.setNote(daoNotes.getNote(DaoNotes.NoteType.getEnum(rs.getInt("NOTE")))); |
| 191 | |
} |
| 192 | |
|
| 193 | 0 | return person; |
| 194 | |
} |
| 195 | |
} |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | 0 | private static final class PersonQueryMapper implements QueryMapper { |
| 203 | |
@Override |
| 204 | |
public Query constructInsertQuery(Entity entity){ |
| 205 | 0 | String query = "INSERT INTO " + TABLE + " (NAME, FIRST_NAME, EMAIL, NOTE, THE_COUNTRY_FK, TYPE) VALUES(?,?,?,?,?,?)"; |
| 206 | |
|
| 207 | 0 | return new Query(query, fillArray((Person) entity, false)); |
| 208 | |
} |
| 209 | |
|
| 210 | |
@Override |
| 211 | |
public Query constructUpdateQuery(Entity entity){ |
| 212 | 0 | String query = "UPDATE " + TABLE + " SET NAME = ?, FIRST_NAME = ?, EMAIL = ?, NOTE = ?, THE_COUNTRY_FK = ?, TYPE = ? WHERE ID = ?"; |
| 213 | |
|
| 214 | 0 | return new Query(query, fillArray((Person) entity, true)); |
| 215 | |
} |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
private static Object[] fillArray(Person person, boolean id){ |
| 226 | 0 | Object[] values = new Object[6 + (id ? 1 : 0)]; |
| 227 | |
|
| 228 | 0 | values[0] = person.getName(); |
| 229 | 0 | values[1] = person.getFirstName(); |
| 230 | 0 | values[2] = person.getEmail(); |
| 231 | 0 | values[3] = person.getNote() == null ? 0 : person.getNote().getValue().intValue(); |
| 232 | 0 | values[4] = person.getTheCountry() == null ? null : person.getTheCountry().getId(); |
| 233 | 0 | values[5] = person.getType(); |
| 234 | |
|
| 235 | 0 | if (id){ |
| 236 | 0 | values[6] = person.getId(); |
| 237 | |
} |
| 238 | |
|
| 239 | 0 | return values; |
| 240 | |
} |
| 241 | |
} |
| 242 | |
} |