1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scaffold.sql;
18
19
20 import java.sql.SQLException;
21 import java.sql.Timestamp;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.apache.commons.beanutils.BeanUtils;
29 import org.apache.commons.scaffold.lang.ParameterException;
30 import org.apache.commons.scaffold.lang.PopulateException;
31 import org.apache.commons.scaffold.lang.PropertiesException;
32 import org.apache.commons.scaffold.lang.ResourceException;
33 import org.apache.commons.scaffold.lang.Tokens;
34 import org.apache.commons.scaffold.lucene.Engine;
35 import org.apache.commons.scaffold.lucene.SearchUtils;
36 import org.apache.commons.scaffold.text.ConvertUtils;
37 import org.apache.commons.scaffold.util.ProcessBeanBase;
38 import org.apache.commons.scaffold.util.ProcessResult;
39 import org.apache.commons.scaffold.util.ProcessResultBase;
40 import org.apache.commons.scaffold.util.StorageBean;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public class StorageBeanBase extends ProcessBeanBase implements StorageBean {
117
118
119
120
121
122 protected boolean blank(String s) {
123 return ConvertUtils.blank(s);
124 }
125
126
127
128
129
130 protected boolean blankValue(String s) {
131 return ConvertUtils.blankValue(s);
132 }
133
134
135
136
137 public static boolean blank(Number key) {
138 return ConvertUtils.blank(key);
139 }
140
141
142
143
144
145
146
147
148
149
150 public final static String like(String parameter) {
151 return StatementUtils.like(parameter);
152 }
153
154
155
156
157
158 public final static String DOT = ".";
159
160
161
162
163
164
165 protected static Properties commands;
166
167
168
169
170
171 protected String lookup(String key)
172 throws PropertiesException {
173
174 if (null==commands) throw new PropertiesException(
175 PropertiesException.MESSAGE + key);
176
177 String command = null;
178 String prefix = getPrefix();
179 if (null!=prefix) {
180 StringBuffer sb = new StringBuffer(prefix);
181 sb.append(DOT);
182 sb.append(key);
183 command = sb.toString();
184 }
185 else command = key;
186
187 return commands.getProperty(command);
188 }
189
190
191
192
193
194 protected String lookupRoot(String key)
195 throws PropertiesException {
196
197 if (null==commands) throw new PropertiesException(
198 PropertiesException.MESSAGE + key);
199
200 return commands.getProperty(key);
201
202 }
203
204
205
206
207
208
209
210
211
212 public static final void init(Properties _commands) {
213
214 if (null==commands) {
215 commands = _commands;
216 }
217 else {
218 commands.putAll(_commands);
219 }
220 }
221
222
223
224
225
226
227
228 public final static String INSERT = "insert";
229
230
231
232
233
234 public final static String UPDATE = "update";
235
236
237
238
239
240 public final static String RETRIEVE = "retrieve";
241
242
243
244
245
246 public final static String DELETE = "delete";
247
248
249
250
251
252 public final static String RESTORE = "restore";
253
254
255
256
257
258 public final static String RECYCLE = "recycle";
259
260
261
262
263
264 public final static String TABLE = "table";
265
266
267
268
269
270 public final static String PARAMS = DOT + "params";
271
272
273
274
275
276
277 private String prefix = null;
278
279
280
281 public String getPrefix() {
282 return prefix;
283 }
284
285
286
287 public void setPrefix(String prefix) {
288 this.prefix = prefix;
289 }
290
291
292
293
294
295
296 private Object[] parameters = null;
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313 public Object[] getParameters(String command) throws ResourceException {
314
315 if (null==command) {
316 if ((null==parameters) && (null!=paramList)) {
317 return paramList.toArray();
318 }
319 return parameters;
320 }
321
322
323 String paramToken = command + PARAMS;
324 String params = lookup(paramToken);
325
326 if (null==params) {
327 throw new PropertiesException(
328 PropertiesException.MESSAGE + paramToken);
329 }
330 else {
331 String[] tokens = ConvertUtils.tokensToArray(params,PARAM_SEP);
332 Object[] values = new Object[tokens.length];
333 try {
334 for (int i=0; i<values.length; i++) {
335 values[i] = BeanUtils.getProperty(this,tokens[i]);
336 }
337 }
338 catch (Exception e) {
339 throw new PropertiesException(
340 PropertiesException.MESSAGE + params);
341 }
342
343 return values;
344 }
345 }
346
347
348
349 public void setParameters(Object[] parameters) {
350 this.parameters = parameters;
351 }
352
353
354
355
356
357
358 private List paramList = null;
359
360
361
362
363
364
365 private List paramListDefault() throws ResourceException {
366
367 List list = paramList;
368 if (null==list) {
369
370 list = new java.util.ArrayList();
371
372 Object[] array = getParameters(null);
373 if (null!=array) {
374 for (int i =0; i<array.length; i++) {
375 list.add(array[i]);
376 }
377 }
378 }
379 return list;
380
381 }
382
383
384
385
386
387 public static final String PARAM_SEP = ",";
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404 public List getParamList(String command) throws ResourceException {
405
406
407 if (null==command) return paramListDefault();
408
409 List values = new java.util.ArrayList();
410
411 String paramToken = command + PARAMS;
412 String params = lookup(paramToken);
413 if (null==params) {
414 throw new PropertiesException(
415 PropertiesException.MESSAGE + paramToken);
416 }
417 else {
418
419 Iterator tokens = ConvertUtils.tokensToList(params,PARAM_SEP).iterator();
420
421 try {
422 while (tokens.hasNext()) {
423 values.add(BeanUtils.getProperty(this,(String)tokens.next()));
424 }
425 }
426 catch (Exception e) {
427 throw new PropertiesException(
428 PropertiesException.MESSAGE + params);
429 }
430 }
431 return values;
432 }
433
434
435
436 public void setParamList(List paramList) {
437 this.paramList = paramList;
438 }
439
440
441
442
443
444
445
446
447
448
449 private Short marked = ConvertUtils.SHORT_ONE;
450
451
452
453
454
455
456
457 public Short getMarked() {
458 return (this.marked);
459 }
460
461
462
463
464
465
466 public void setMarked(Short marked) {
467 this.marked = marked;
468 }
469
470
471
472
473
474
475 public void populate(Map parameters) throws Exception {
476
477 if (parameters!=null) {
478 BeanUtils.copyProperties(this,parameters);
479 }
480
481 }
482
483
484
485
486
487
488 public void executeUpdate(String command)
489 throws ResourceException {
490
491 try {
492
493 int result = StatementUtils.executeUpdate(
494 null,lookup(command),getParameters(command));
495
496 }
497 catch (SQLException e) {
498 throw new ResourceException(e);
499 }
500
501 }
502
503
504 public void executeUpdateRoot(String command)
505 throws ResourceException {
506
507 try {
508
509 int result = StatementUtils.executeUpdate(
510 null,lookupRoot(command));
511
512 }
513 catch (SQLException e) {
514 throw new ResourceException(e);
515 }
516
517 }
518
519
520
521
522
523 public int count(
524 String command)
525 throws ResourceException {
526
527 Integer result = null;
528 try {
529 result = (Integer) StatementUtils.getColumn(
530 null,
531 1,
532 lookup(command)
533 );
534 }
535 catch (SQLException e) {
536 throw new ResourceException(e);
537 }
538 return result.intValue();
539 }
540
541
542
543 public int count(
544 String command,
545 Object parameter)
546 throws ResourceException {
547
548 Integer result = null;
549 try {
550 result = (Integer) StatementUtils.getColumn(
551 null,
552 1,
553 lookup(command),
554 parameter
555 );
556 }
557 catch (SQLException e) {
558 throw new ResourceException(e);
559 }
560 return result.intValue();
561 }
562
563
564
565
566 public boolean findElement(
567 Object target,
568 String command,
569 Object key) throws ResourceException {
570
571 boolean found = false;
572
573 try {
574
575 found = StatementUtils.getElement(null,target,
576 lookup(command),key);
577
578 }
579 catch (SQLException e) {
580 throw new ResourceException(e);
581 }
582
583 return found;
584
585 }
586
587
588
589
590
591
592
593 public Collection findCollection(Object target,
594 String command) throws ResourceException {
595
596 try {
597
598 return StatementUtils.getCollection(null,
599 target,lookup(command));
600
601 }
602 catch (SQLException e) {
603 throw new ResourceException(e);
604 }
605
606 }
607
608
609
610
611
612
613
614 protected final Collection findCollection(Object target,
615 String command, int parameter) throws ResourceException {
616
617
618 try {
619
620 return StatementUtils.getCollection(null,
621 target,lookup(command),parameter);
622
623 }
624 catch (SQLException e) {
625 throw new ResourceException(e);
626 }
627 }
628
629
630
631
632
633
634
635
636 protected final Collection findCollection(Object target,
637 String command, Object parameter) throws ResourceException {
638
639 try {
640
641 return StatementUtils.getCollection(null,
642 target,lookup(command),parameter);
643
644 }
645 catch (SQLException e) {
646 throw new ResourceException(e);
647 }
648
649 }
650
651
652
653 public Collection findCollection(Object target,
654 String command, Object[] parameters) throws ResourceException {
655
656 try {
657
658 return StatementUtils.getCollection(null,
659 target,lookup(command),parameters);
660
661 }
662 catch (SQLException e) {
663 throw new ResourceException(e);
664 }
665
666 }
667
668
669
670 public Collection findByProperty(
671 Object target,
672 String property,
673 String value) throws ParameterException, PopulateException,
674 ResourceException {
675
676 return SearchUtils.getCollection(target,
677 Engine.getHits(
678 Engine.getQuery(value,property)));
679
680 }
681
682
683
684
685
686
687
688
689
690
691
692
693 private Object storageKey = null;
694
695
696
697 public Object getStorageKey() {
698 return this.storageKey;
699 }
700
701
702
703 public void setStorageKey(Object storageKey) {
704 this.storageKey = storageKey;
705 }
706
707
708
709
710
711
712
713
714
715 private Timestamp modified = ConvertUtils.NULL_TIMESTAMP;
716
717
718
719
720
721
722 public Timestamp getModified() {
723 return (this.modified);
724 }
725
726
727
728
729
730
731 public void setModified(Timestamp modified) {
732 this.modified = modified;
733 }
734
735
736
737
738
739
740
741
742
743 public boolean isCurrent(Timestamp modified) {
744 return (modified!=getModified());
745 }
746
747
748
749
750
751 private int resultCode = 0;
752
753
754
755 public int getResultCode() {
756 return resultCode;
757 }
758
759
760
761 public void setResultCode(int resultCode) {
762 this.resultCode = resultCode;
763 }
764
765
766
767
768
769 private Object result = this;
770
771
772
773 public Object getResult() {
774 return result;
775 }
776
777
778
779 public void setResult(Object result) {
780 this.result = result;
781 }
782
783
784
785
786
787
788
789
790
791
792 public void fixModified() {
793 Timestamp modified = getModified();
794 if ((null==modified) || (ConvertUtils.NULL_TIMESTAMP==modified)) {
795 setModified(new Timestamp(System.currentTimeMillis()));
796 }
797 }
798
799
800 public int updateResultCode(String command) throws ResourceException {
801
802 int result = 0;
803 try {
804
805 result = StatementUtils.executeUpdate(
806 null,lookup(command),getParameters(command));
807
808 }
809 catch (SQLException e) {
810 throw new ResourceException(e);
811 }
812
813 return result;
814 }
815
816
817 public Object update(Object parameters) throws Exception {
818
819 populate((Map) parameters);
820
821 String command = getParameter();
822
823 fixModified();
824 setResultCode(updateResultCode(command));
825
826 String message = null;
827 if (isNew()) message = Tokens.DATA_RECORD_INSERTED;
828 else message = Tokens.DATA_RECORD_UPDATED;
829
830 ProcessResult result = new ProcessResultBase(getResult());
831 result.addMessage(message);
832 result.addMessage(getStorageKey());
833 result.setSingleForm(true);
834 return result;
835 }
836
837
838 public ProcessResult elementResult(String command) throws Exception {
839
840 boolean found = false;
841 try {
842
843 found = StatementUtils.getElement(null,this,
844 lookup(command),getParameters(command));
845
846 }
847 catch (SQLException e) {
848 throw new ResourceException(e);
849 }
850
851 if (found) {
852
853 setResult(this);
854 setResultCode(1);
855
856 }
857 else {
858 setResultCode(0);
859 }
860
861 ProcessResult result = new ProcessResultBase(this);
862 result.setSingleForm(true);
863 return result;
864 }
865
866
867
868
869
870
871
872
873
874
875 public Object element(Object command) throws Exception {
876
877 populate((Map) command);
878
879 return elementResult(getParameter());
880
881 }
882
883
884 public Object collectionResult(String command) throws Exception {
885
886 Collection result = null;
887
888 try {
889
890 result = StatementUtils.getCollection(null,
891 this,lookup(command),getParameters(command));
892
893 }
894 catch (SQLException e) {
895 throw new ResourceException(e);
896 }
897
898 setResult(result);
899
900 return new ProcessResultBase(result);
901
902 }
903
904
905
906
907
908
909
910
911
912
913 public Object collection(Object parameters) throws Exception {
914
915 populate((Map) parameters);
916
917 return collectionResult(getParameter());
918
919 }
920
921
922
923
924
925
926 public static String KEYS_NEXT = "keys.next";
927
928
929
930
931
932 public static String KEYS_INC = "keys.inc";
933
934
935
936 public boolean isNew() {
937 Object key = getStorageKey();
938 return ((null==key) || (blankValue(key.toString())));
939 }
940
941
942
943
944 public Object createKey(String keyName)
945 throws ResourceException {
946
947 Object result = null;
948
949 try {
950
951 result = StatementUtils.createKey(
952 null,
953 1,
954 lookupRoot(KEYS_NEXT),
955 lookup(keyName),
956 lookupRoot(KEYS_INC)
957 );
958 }
959
960 catch (SQLException e) {
961 throw new ResourceException(e);
962 }
963
964 return result;
965
966 }
967
968
969
970 public void allocateKey() throws Exception {
971
972 setStorageKey(createKey(TABLE));
973
974 }
975
976
977
978
979
980 public void store() throws Exception {
981
982 boolean isInsert = isNew();
983 String token = null;
984 if (isInsert) {
985 token = INSERT;
986 allocateKey();
987 }
988 else {
989 token = UPDATE;
990 }
991
992 String command = lookup(token);
993
994 Timestamp modified = getModified();
995 if ((null==modified) || (ConvertUtils.NULL_TIMESTAMP==modified)) {
996 setModified(new Timestamp(System.currentTimeMillis()));
997 }
998
999
1000
1001 int result = 0;
1002 try {
1003
1004 result = StatementUtils.executeUpdate(
1005 null,command,getParameters(token));
1006
1007 }
1008 catch (SQLException e) {
1009 throw new ResourceException(e);
1010 }
1011 setResultCode(result);
1012
1013 }
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 public Object store(Object parameters) throws Exception {
1026
1027 populate((Map) parameters);
1028
1029 String message = null;
1030 if (isNew()) message = Tokens.DATA_RECORD_INSERTED;
1031 else message = Tokens.DATA_RECORD_UPDATED;
1032
1033 store();
1034
1035 ProcessResult result = new ProcessResultBase(getResult());
1036 result.addMessage(message);
1037 result.addMessage(getStorageKey());
1038 result.setSingleForm(true);
1039 return result;
1040 }
1041
1042
1043
1044
1045
1046 public void retrieve() throws Exception {
1047
1048 boolean found = findElement(this,RETRIEVE,getStorageKey());
1049
1050 if (found) {
1051
1052 setResult(this);
1053 setResultCode(1);
1054
1055 }
1056 else {
1057 setResultCode(0);
1058 }
1059
1060 }
1061
1062
1063 public Object retrieve(Object parameters) throws Exception {
1064
1065 populate((Map) parameters);
1066 Object key = getStorageKey();
1067 if (null==key) {
1068 throw new ParameterException();
1069 }
1070
1071 retrieve();
1072 int resultCode = getResultCode();
1073
1074 ProcessResult result = new ProcessResultBase(this);
1075 if (0==resultCode) {
1076 result.addMessage(Tokens.DATA_ACCESS_EMPTY);
1077 }
1078 else {
1079 result.addMessage(Tokens.DATA_RECORD_RETRIEVED);
1080 }
1081 result.addMessage(key);
1082 result.setSingleForm(true);
1083
1084 return result;
1085
1086 }
1087
1088
1089
1090
1091
1092
1093 public void delete() throws Exception {
1094
1095 retrieve();
1096 int result = getResultCode();
1097
1098 if (0!=result) {
1099
1100 try {
1101
1102
1103 result = StatementUtils.executeUpdate(null,
1104 lookup(DELETE),getStorageKey());
1105 }
1106
1107 catch (SQLException e) {
1108 throw new ResourceException(e);
1109 }
1110
1111 setResultCode(result);
1112 }
1113
1114 }
1115
1116
1117
1118
1119
1120 public Object delete(Object parameters) throws Exception {
1121
1122 populate((Map) parameters);
1123 Object key = getStorageKey();
1124 if (null==key) {
1125 throw new ParameterException();
1126 }
1127
1128 delete();
1129 int resultCode = getResultCode();
1130
1131 ProcessResult result = new ProcessResultBase(this);
1132 if (0==resultCode) {
1133 result.addMessage(Tokens.DATA_ACCESS_EMPTY);
1134 }
1135 else {
1136 result.addMessage(Tokens.DATA_RECORD_DELETED);
1137 }
1138 result.addMessage(key);
1139 result.setSingleForm(true);
1140
1141 return result;
1142
1143 }
1144
1145
1146
1147
1148
1149
1150 public void recycle() throws Exception {
1151
1152 retrieve();
1153 int result = getResultCode();
1154
1155 if (0!=result) {
1156
1157 try {
1158
1159
1160 result = StatementUtils.executeUpdate(null,
1161 lookup(RECYCLE),getStorageKey());
1162 }
1163
1164 catch (SQLException e) {
1165 throw new ResourceException(e);
1166 }
1167
1168 setResultCode(result);
1169 }
1170
1171 }
1172
1173
1174
1175 public Object recycle(Object parameters) throws Exception {
1176
1177 populate((Map) parameters);
1178 Object key = getStorageKey();
1179 if (null==key) {
1180 throw new ParameterException();
1181 }
1182
1183 recycle();
1184 int resultCode = getResultCode();
1185
1186 ProcessResult result = new ProcessResultBase(this);
1187 if (resultCode==0) {
1188 result.addMessage(Tokens.DATA_ACCESS_EMPTY);
1189 }
1190 else {
1191 result.addMessage(Tokens.DATA_RECORD_RECYCLED);
1192 }
1193 result.addMessage(key);
1194 result.setSingleForm(true);
1195
1196 return result;
1197
1198 }
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 public void restore() throws Exception {
1213
1214 int result = 0;
1215
1216 try {
1217
1218 result = StatementUtils.executeUpdate(null,
1219 lookup(RESTORE),getStorageKey());
1220
1221 }
1222 catch (SQLException e) {
1223 throw new ResourceException(e);
1224 }
1225
1226 setResultCode(result);
1227
1228 }
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240 public Object restore(Object parameters) throws Exception {
1241
1242 populate((Map) parameters);
1243 Object key = getStorageKey();
1244 if (null==key) {
1245 throw new ParameterException();
1246 }
1247
1248 restore();
1249 int resultCode = getResultCode();
1250
1251 ProcessResult result = new ProcessResultBase(this);
1252 if (0==resultCode) {
1253 result.addMessage(Tokens.DATA_ACCESS_EMPTY);
1254 }
1255 else {
1256 result.addMessage(Tokens.DATA_RECORD_RESTORED);
1257 }
1258 result.addMessage(key);
1259 result.setSingleForm(true);
1260
1261 return result;
1262
1263 }
1264
1265 }
1266