1 package org.kuali.kpme.edo;
2
3 import org.apache.commons.dbcp.BasicDataSource;
4 import org.apache.ojb.broker.PBKey;
5 import org.apache.poi.hssf.usermodel.*;
6 import org.apache.poi.ss.usermodel.Cell;
7 import org.junit.Test;
8 import org.kuali.kpme.edo.service.EdoServiceLocator;
9 import org.kuali.rice.core.impl.config.property.Config;
10 import org.kuali.rice.core.impl.config.property.Param;
11 import org.kuali.rice.coreservice.api.parameter.Parameter;
12 import org.springframework.context.support.StaticApplicationContext;
13 import org.springframework.jdbc.core.JdbcTemplate;
14 import org.springframework.jdbc.support.rowset.SqlRowSet;
15 import org.xml.sax.*;
16 import org.xml.sax.helpers.XMLFilterImpl;
17
18 import javax.sql.RowSet;
19 import javax.xml.bind.JAXBContext;
20 import javax.xml.bind.Unmarshaller;
21 import javax.xml.bind.UnmarshallerHandler;
22 import javax.xml.parsers.SAXParser;
23 import javax.xml.parsers.SAXParserFactory;
24 import java.io.*;
25 import java.sql.Connection;
26 import java.sql.DriverManager;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 public class SpreadsheetEditTest {
33
34
35 enum Location {LEFT, CENTER, RIGHT};
36
37
38 Pattern pattern=Pattern.compile("\\$\\{(.*?)\\}");
39
40
41 interface xlsBean {
42 public void setData(String datum);
43 public String getData();
44 }
45
46 Map<String,xlsBean> beans=new HashMap();
47
48 class CellBean implements xlsBean {
49 int index=1;
50
51 HSSFCell cell;
52 CellBean(HSSFCell cell) { this.cell=cell;}
53
54 public void setData(String datum) {
55 int destinationRowNumber=cell.getRow().getRowNum()+index++;
56 int destinationCellNumber=cell.getColumnIndex();
57
58
59 HSSFSheet sheet=cell.getRow().getSheet();
60 HSSFRow destinationRow=sheet.getRow(destinationRowNumber);
61 if (destinationRow==null) {
62 destinationRow=sheet.createRow(destinationRowNumber);
63 }
64 HSSFCell destinationCell=destinationRow.getCell(destinationCellNumber);
65
66 if (destinationCell==null) {
67 destinationCell=destinationRow.createCell(destinationCellNumber);
68 destinationCell.setCellType(cell.getCellType());
69 }
70
71 if (cell.getCellType()== Cell.CELL_TYPE_NUMERIC) {
72 destinationCell.setCellValue(new Double(datum));
73 } else {
74 destinationCell.setCellValue(datum);
75 }
76 }
77
78 public String getData() {
79 if (cell.getCellType()== Cell.CELL_TYPE_NUMERIC) {
80 return ""+cell.getNumericCellValue();
81 } else {
82 return cell.getStringCellValue();
83 }
84 }
85
86 }
87
88 class HeaderBean implements xlsBean {
89
90 Location loc;
91 HSSFHeader header;
92 HeaderBean(HSSFHeader header,Location loc) { this.header=header; this.loc=loc;}
93 public void setData(String datum) {
94 switch (loc) {
95 case LEFT:
96 header.setLeft(datum);
97 break;
98 case CENTER:
99 header.setCenter(datum);
100 break;
101 case RIGHT:
102 header.setRight(datum);
103 break;
104 }
105 }
106 public String getData() {
107 switch (loc) {
108 case LEFT:
109 return header.getLeft();
110 case CENTER:
111 return header.getCenter();
112 case RIGHT:
113 return header.getRight();
114 }
115 return null;
116 }
117 }
118
119
120 void apply(String var,String val) {
121 System.out.println(String.format("%s=%s",var,val));
122 xlsBean bean=beans.get(var);
123 if (bean instanceof HeaderBean) {
124 if (Character.isDigit(val.charAt(0))) {
125 val=" "+val;
126 }
127 }
128 bean.setData(bean.getData().replace(var,val));
129 }
130
131
132 @Test
133 public void testAddRow() throws IOException {
134
135 final String inFilename=EdoUnitTestBase.UNIT_TEST_SAMPLE_FILES_PATH+"file1.xls";
136 final String outFilename="file1-out.xls";
137
138 HSSFWorkbook wb=new HSSFWorkbook(new FileInputStream(inFilename));
139 HSSFSheet sheet = wb.getSheetAt(0);
140
141 HSSFHeader header=sheet.getHeader();
142
143 Matcher matcher=pattern.matcher(header.getLeft());
144 while (matcher.find()) {
145 String index=matcher.group();
146 beans.put(index, new HeaderBean(header,Location.LEFT));
147 }
148
149 matcher=pattern.matcher(header.getCenter());
150 while (matcher.find()) {
151 String index=matcher.group();
152 beans.put(index, new HeaderBean(header,Location.CENTER));
153 }
154
155 matcher=pattern.matcher(header.getRight());
156 while (matcher.find()) {
157 String index=matcher.group();
158 beans.put(index, new HeaderBean(header,Location.RIGHT));
159 }
160
161 HSSFRow row1=sheet.getRow(1);
162
163 for (int i=row1.getFirstCellNum(); i<row1.getLastCellNum(); i++ ) {
164 HSSFCell nextCell=row1.getCell(i);
165 beans.put(nextCell.getStringCellValue(),new CellBean(nextCell));
166 }
167
168 apply("${CampusName}", "Indiana University - Bloomington");
169 apply("${Title1}","2012-13 Summary Sheet");
170 apply("${Title2}","TENURE ONLY");
171
172 apply("${Last}","Test1");
173 apply("${Last}","Smith2");
174 apply("${Last}","Jones3");
175
176 apply("${First}","Tester1");
177 apply("${First}","Chris2");
178 apply("${First}","Billy3");
179
180 sheet.removeRow(row1);
181 sheet.shiftRows(2,5,-1);
182
183 FileOutputStream stream = new FileOutputStream(outFilename);
184
185 wb.write(stream);
186 stream.close();
187
188
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 public class NamespaceFilter extends XMLFilterImpl {
225
226 private static final String NAMESPACE = "http://rice.kuali.org/core/impl/config";
227
228 @Override
229 public void endElement(String uri, String localName, String qName)
230 throws SAXException {
231 super.endElement(NAMESPACE, localName, qName);
232 }
233
234 @Override
235 public void startElement(String uri, String localName, String qName,Attributes atts) throws SAXException {
236 super.startElement(NAMESPACE, localName, qName, atts);
237 }
238
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 }