View Javadoc
1   /******************************************************************************
2    * MosaicVectorsDaoJdbc.java - Implement the Dao interface for the MosaicVectors
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2007 - Dick Balaska
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  /** Implement the Dao Interface for the {@link com.buckosoft.PicMan.domain.mosaic.MosaicVector}s.
28   * @author Dick Balaska
29   * @since 2007/12/14
30   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/MosaicVectorsDaoJdbc.java">MosaicVectorsDaoJdbc.java</a>
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  	/** Set the reference to the JDBC datasource.
42  	 * @param ds The datasource as configured by Spring.
43  	 * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/WebContent/WEB-INF/picManDatabase/picManDatabase.xml">picManDatabase.xml</a>
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  	/* (non-Javadoc)
81  	 * @see com.buckosoft.PicMan.db.MosaicVectorsDao#getMosaicVector(java.lang.String)
82  	 */
83  	public MosaicVector getMosaicVector(String pic) {
84  		// TODO Auto-generated method stub
85  		return null;
86  	}
87  
88  	/* (non-Javadoc)
89  	 * @see com.buckosoft.PicMan.db.MosaicVectorsDao#updateMosaicVector(com.buckosoft.PicMan.domain.MosaicVector)
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  	/* (non-Javadoc)
100 	 * @see com.buckosoft.PicMan.db.MosaicVectorsDao#getMosaicVectors(java.util.List)
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 	 * <code>MosaicVector</code> Insert Object.
147 	 */
148 	protected class MosaicVectorInsert extends SqlUpdate {
149 		
150 		/**
151 		 * Create a new instance of MetaSetInsert.
152 		 * @param ds the DataSource to use for the insert
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 			// retrieveIdentity(owner);
191 		}
192 	}
193 
194 }