| 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.IDaoKinds; |
| 26 | |
import org.jtheque.primary.od.able.Kind; |
| 27 | |
import org.jtheque.primary.od.impl.KindImpl; |
| 28 | |
import org.jtheque.utils.StringUtils; |
| 29 | |
import org.springframework.jdbc.core.simple.ParameterizedRowMapper; |
| 30 | |
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; |
| 31 | |
|
| 32 | |
import javax.annotation.Resource; |
| 33 | |
import java.sql.ResultSet; |
| 34 | |
import java.sql.SQLException; |
| 35 | |
import java.util.ArrayList; |
| 36 | |
import java.util.Collection; |
| 37 | |
import java.util.List; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 0 | public final class DaoKinds extends GenericDao<Kind> implements IDaoKinds { |
| 45 | 0 | private final ParameterizedRowMapper<Kind> rowMapper = new KindRowMapper(); |
| 46 | 0 | private final QueryMapper queryMapper = new KindQueryMapper(); |
| 47 | |
|
| 48 | |
@Resource |
| 49 | |
private IDaoPersistenceContext persistenceContext; |
| 50 | |
|
| 51 | |
@Resource |
| 52 | |
private SimpleJdbcTemplate jdbcTemplate; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public DaoKinds() { |
| 58 | 0 | super(TABLE); |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
@Override |
| 62 | |
public void create(Kind entity) { |
| 63 | 0 | entity.setPrimaryImpl(PrimaryUtils.getPrimaryImpl()); |
| 64 | |
|
| 65 | 0 | super.create(entity); |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
@Override |
| 69 | |
public Kind createKind() { |
| 70 | 0 | return new KindImpl(); |
| 71 | |
} |
| 72 | |
|
| 73 | |
@Override |
| 74 | |
public boolean exists(String name) { |
| 75 | 0 | return getKind(name) != null; |
| 76 | |
} |
| 77 | |
|
| 78 | |
@Override |
| 79 | |
public Collection<Kind> getKinds() { |
| 80 | 0 | return getKinds(PrimaryUtils.getPrimaryImpl()); |
| 81 | |
} |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
private Collection<Kind> getKinds(CharSequence impl) { |
| 90 | 0 | if (StringUtils.isEmpty(impl)) { |
| 91 | 0 | return getAll(); |
| 92 | |
} |
| 93 | |
|
| 94 | 0 | load(); |
| 95 | |
|
| 96 | 0 | Collection<Kind> kinds = new ArrayList<Kind>(getCache().size() / 2); |
| 97 | |
|
| 98 | 0 | for (Kind kind : getCache().values()) { |
| 99 | 0 | if (impl.equals(kind.getPrimaryImpl())) { |
| 100 | 0 | kinds.add(kind); |
| 101 | |
} |
| 102 | |
} |
| 103 | |
|
| 104 | 0 | return kinds; |
| 105 | |
} |
| 106 | |
|
| 107 | |
@Override |
| 108 | |
public Kind getKind(int id) { |
| 109 | 0 | return get(id); |
| 110 | |
} |
| 111 | |
|
| 112 | |
@Override |
| 113 | |
public Kind getKind(String name) { |
| 114 | 0 | List<Kind> kinds = jdbcTemplate.query("SELECT * FROM " + TABLE + " WHERE NAME = ? AND IMPL = ?", |
| 115 | |
rowMapper, name, PrimaryUtils.getPrimaryImpl()); |
| 116 | |
|
| 117 | 0 | if (kinds.isEmpty()) { |
| 118 | 0 | return null; |
| 119 | |
} |
| 120 | |
|
| 121 | 0 | Kind kind = kinds.get(0); |
| 122 | |
|
| 123 | 0 | if (isNotInCache(kind.getId())) { |
| 124 | 0 | getCache().put(kind.getId(), kind); |
| 125 | |
} |
| 126 | |
|
| 127 | 0 | return getCache().get(kind.getId()); |
| 128 | |
} |
| 129 | |
|
| 130 | |
@Override |
| 131 | |
protected ParameterizedRowMapper<Kind> getRowMapper() { |
| 132 | 0 | return rowMapper; |
| 133 | |
} |
| 134 | |
|
| 135 | |
@Override |
| 136 | |
protected QueryMapper getQueryMapper() { |
| 137 | 0 | return queryMapper; |
| 138 | |
} |
| 139 | |
|
| 140 | |
@Override |
| 141 | |
protected void loadCache() { |
| 142 | 0 | Collection<Kind> kinds = persistenceContext.getSortedList(TABLE, rowMapper); |
| 143 | |
|
| 144 | 0 | for (Kind kind : kinds) { |
| 145 | 0 | getCache().put(kind.getId(), kind); |
| 146 | |
} |
| 147 | |
|
| 148 | 0 | setCacheEntirelyLoaded(); |
| 149 | 0 | } |
| 150 | |
|
| 151 | |
@Override |
| 152 | |
protected void load(int i) { |
| 153 | 0 | Kind kind = persistenceContext.getDataByID(TABLE, i, rowMapper); |
| 154 | |
|
| 155 | 0 | getCache().put(i, kind); |
| 156 | 0 | } |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | 0 | private final class KindRowMapper implements ParameterizedRowMapper<Kind> { |
| 164 | |
@Override |
| 165 | |
public Kind mapRow(ResultSet rs, int i) throws SQLException { |
| 166 | 0 | Kind kind = createKind(); |
| 167 | |
|
| 168 | 0 | kind.setId(rs.getInt("ID")); |
| 169 | 0 | kind.setName(rs.getString("NAME")); |
| 170 | 0 | kind.setPrimaryImpl(rs.getString("IMPL")); |
| 171 | |
|
| 172 | 0 | return kind; |
| 173 | |
} |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | 0 | private static final class KindQueryMapper implements QueryMapper { |
| 182 | |
@Override |
| 183 | |
public Query constructInsertQuery(Entity entity) { |
| 184 | 0 | Kind kind = (Kind) entity; |
| 185 | |
|
| 186 | 0 | String query = "INSERT INTO " + TABLE + " (NAME, IMPL) VALUES(?, ?)"; |
| 187 | |
|
| 188 | 0 | Object[] parameters = { |
| 189 | |
kind.getName(), |
| 190 | |
kind.getPrimaryImpl() |
| 191 | |
}; |
| 192 | |
|
| 193 | 0 | return new Query(query, parameters); |
| 194 | |
} |
| 195 | |
|
| 196 | |
@Override |
| 197 | |
public Query constructUpdateQuery(Entity entity) { |
| 198 | 0 | Kind kind = (Kind) entity; |
| 199 | |
|
| 200 | 0 | String query = "UPDATE " + TABLE + " SET NAME = ?, IMPL = ? WHERE ID = ?"; |
| 201 | |
|
| 202 | 0 | Object[] parameters = { |
| 203 | |
kind.getName(), |
| 204 | |
kind.getPrimaryImpl(), |
| 205 | |
kind.getId()}; |
| 206 | |
|
| 207 | 0 | return new Query(query, parameters); |
| 208 | |
} |
| 209 | |
} |
| 210 | |
} |