1
2
3
4
5
6
7
8 package com.buckosoft.PicMan.db;
9
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.sql.Types;
13 import java.util.LinkedList;
14 import java.util.List;
15
16 import javax.sql.DataSource;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.springframework.jdbc.core.SqlParameter;
21 import org.springframework.jdbc.object.MappingSqlQuery;
22 import org.springframework.jdbc.object.SqlUpdate;
23
24 import com.buckosoft.PicMan.domain.Pic;
25 import com.buckosoft.PicMan.domain.mosaic.MosaicVector;
26
27
28
29
30
31
32 public class MosaicVectorsDaoJdbc implements MosaicVectorsDao {
33 private static final boolean DEBUG = true;
34 protected final Log logger = LogFactory.getLog(getClass());
35
36
37 private DataSource ds;
38
39 private String replaceString;
40
41
42
43
44
45 public void setDataSource(DataSource ds) {
46 this.ds = ds;
47 }
48
49 public MosaicVectorsDaoJdbc() {
50 StringBuilder sb = new StringBuilder();
51 sb.append("REPLACE INTO mosaicVectors (pid");
52 int d;
53 int x;
54 int y;
55 for (d=1; d<=MosaicVector.MAXDEPTH; d++) {
56 for (x=0; x<d; x++) {
57 for (y=0; y<d; y++) {
58 sb.append(",c");
59 sb.append(d);
60 sb.append("_");
61 sb.append(x);
62 sb.append("_");
63 sb.append(y);
64 }
65 }
66 }
67 sb.append(") VALUES (?");
68 for (d=1; d<=MosaicVector.MAXDEPTH; d++) {
69 for (y=0; y<d; y++) {
70 for (x=0; x<d; x++) {
71 sb.append(",?");
72 }
73 }
74 }
75 sb.append(")");
76 replaceString = sb.toString();
77 if (DEBUG)
78 logger.info(replaceString);
79 }
80
81
82
83 public MosaicVector getMosaicVector(String pic) {
84
85 return null;
86 }
87
88
89
90
91 public void updateMosaicVector(MosaicVector mv) {
92 if (sql_storeMosaicVectorINSERT == null)
93 sql_storeMosaicVectorINSERT = new MosaicVectorInsert(ds);
94 sql_storeMosaicVectorINSERT.insert(mv);
95 }
96 private MosaicVectorInsert sql_storeMosaicVectorINSERT = null;
97
98
99
100
101
102 public List<MosaicVector> getMosaicVectors(List<Pic> picList) {
103 LinkedList<MosaicVector> list = new LinkedList<MosaicVector>();
104
105 return(list);
106 }
107
108
109 class MosaicVectorQuery extends MappingSqlQuery {
110
111 MosaicVectorQuery(DataSource ds) {
112 super(ds, "SELECT * from mosaicVectors");
113 compile();
114 }
115
116 MosaicVectorQuery(DataSource ds, int pid) {
117 super(ds, "SELECT * from mosaicVectors WHERE pid = ?");
118 declareParameter(new SqlParameter(Types.INTEGER));
119 compile();
120 }
121
122
123 protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
124 MosaicVector mv = new MosaicVector();
125 mv.pid = rs.getInt("pid");
126 mv.rgb = rs.getInt("c1_0_0");
127 int x,y,z;
128 z = 2;
129 for (x=0; x<z; x++) {
130 for (y=0; y<z; y++) {
131 mv.rgb2[x][y] = rs.getInt("c2_" + x + "_" + "y");
132 }
133 }
134 z = 3;
135 for (x=0; x<z; x++) {
136 for (y=0; y<z; y++) {
137 mv.rgb3[x][y] = rs.getInt("c3_" + x + "_" + "y");
138 }
139 }
140 return(mv);
141 }
142 }
143
144
145
146
147
148 protected class MosaicVectorInsert extends SqlUpdate {
149
150
151
152
153
154 protected MosaicVectorInsert(DataSource ds) {
155 super(ds, replaceString);
156 declareParameter(new SqlParameter(Types.VARCHAR));
157 int d;
158 int x;
159 int y;
160 for (d=1; d<=MosaicVector.MAXDEPTH; d++) {
161 for (y=0; y<d; y++) {
162 for (x=0; x<d; x++) {
163 declareParameter(new SqlParameter(Types.INTEGER));
164 }
165 }
166 }
167 compile();
168 }
169
170 protected void insert(MosaicVector mv) {
171 LinkedList<Object> ll = new LinkedList<Object>();
172 ll.add(new Integer(mv.pid));
173 ll.add(new Integer(mv.rgb));
174 int x;
175 int y;
176 for (y=0; y<2; y++) {
177 for (x=0; x<2; x++) {
178 ll.add(new Integer(mv.rgb2[x][y]));
179 }
180 }
181 for (y=0; y<3; y++) {
182 for (x=0; x<3; x++) {
183 ll.add(new Integer(mv.rgb3[x][y]));
184 }
185 }
186
187
188 Object[] objs = ll.toArray();
189 super.update(objs);
190
191 }
192 }
193
194 }