Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/Java/core/Server/src/org/litesoft/orsup/DB_ImportSupport.java

Diff revisions: vs.
  @@ -4,6 +4,7 @@
4 4 import org.litesoft.commonfoundation.base.*;
5 5 import org.litesoft.commonfoundation.csv.*;
6 6 import org.litesoft.commonfoundation.exceptions.*;
7 + import org.litesoft.commonfoundation.typeutils.Objects;
7 8 import org.litesoft.commonfoundation.typeutils.*;
8 9 import org.litesoft.core.simpletypes.temporal.*;
9 10 import org.litesoft.core.util.*;
  @@ -16,11 +17,9 @@
16 17
17 18 import java.io.*;
18 19 import java.lang.reflect.*;
19 - import org.litesoft.commonfoundation.typeutils.Objects;
20 20 import java.util.*;
21 21
22 - public class DB_ImportSupport
23 - {
22 + public class DB_ImportSupport {
24 23 public static final String TODAY_RELATIVE = ":T";
25 24 public static final String DELTA_DATE_RELATIVE = ":DD";
26 25 public static final String DELTA_DATE_SEP = "+T-";
  @@ -28,83 +27,67 @@
28 27 public static final String SETTER_ATTRIBUTE = "()";
29 28 public static final String PO_URL_ATTRIBUTE = ":";
30 29
31 - protected static class ImportFromMap
32 - {
30 + protected static class ImportFromMap {
33 31 public void process( Logger pLogger, UnfilteringFinder pFinder, File pReferenceDir, Map<FauxPOref, Attributes> pLineMap )
34 - throws IOException
35 - {
36 - if ( pLineMap.isEmpty() )
37 - {
32 + throws IOException {
33 + if ( pLineMap.isEmpty() ) {
38 34 throw new IllegalArgumentException( "No Data Found" );
39 35 }
40 36 new Inner( pLogger, pFinder.createUnaugmentedTransaction(), pReferenceDir, pLineMap ).process().commit();
41 37 }
42 38
43 - private class Inner
44 - {
39 + private class Inner {
45 40 private Logger mLogger;
46 41 private Transaction mTransaction;
47 42 private File mReferenceDir;
48 43 private Map<FauxPOref, Attributes> mLineMap;
49 44 private Map<FauxPOref, PersistentObject<?>> mPOsByRef = new HashMap<FauxPOref, PersistentObject<?>>();
50 45
51 - private Inner( Logger pLogger, Transaction pTransaction, File pReferenceDir, Map<FauxPOref, Attributes> pLineMap )
52 - {
46 + private Inner( Logger pLogger, Transaction pTransaction, File pReferenceDir, Map<FauxPOref, Attributes> pLineMap ) {
53 47 mLogger = pLogger;
54 48 mTransaction = pTransaction;
55 49 mReferenceDir = pReferenceDir;
56 50 mLineMap = pLineMap;
57 51 }
58 52
59 - public Transaction process()
60 - {
61 - for ( FauxPOref ref : mLineMap.keySet() )
62 - {
53 + public Transaction process() {
54 + for ( FauxPOref ref : mLineMap.keySet() ) {
63 55 putLineInDB( ref );
64 56 }
65 57 return mTransaction;
66 58 }
67 59
68 - private PersistentObject<?> putLineInDB( FauxPOref pPOref )
69 - {
60 + private PersistentObject<?> putLineInDB( FauxPOref pPOref ) {
70 61 PersistentObject<?> zPO = mPOsByRef.get( pPOref );
71 62 if ( zPO == null ) // NOT Already created
72 63 {
73 64 mPOsByRef.put( pPOref, zPO = mTransaction.create( pPOref.getPersistedObjectRegistrationName() ) );
74 65
75 66 Attributes attributes = mLineMap.get( pPOref );
76 - if ( attributes == null )
77 - {
67 + if ( attributes == null ) {
78 68 throw new IllegalArgumentException( "No Attributes (Line) found for URL: " + pPOref );
79 69 }
80 70 mLogger.trace.log( "writing line to db for URL: ", pPOref );
81 71
82 - for ( String attrName : attributes.getNames() )
83 - {
72 + for ( String attrName : attributes.getNames() ) {
84 73 String value = attributes.getValue( attrName );
85 74
86 75 // If the attribute is calling a setter method ..
87 - if ( attrName.endsWith( SETTER_ATTRIBUTE ) )
88 - {
89 - try
90 - {
76 + if ( attrName.endsWith( SETTER_ATTRIBUTE ) ) {
77 + try {
91 78 String methodName = attrName.substring( 0, attrName.length() - SETTER_ATTRIBUTE.length() );
92 79 Method method = zPO.getClass().getMethod( methodName, String.class );
93 - if ( method != null )
94 - {
80 + if ( method != null ) {
95 81 method.invoke( zPO, value );
96 82 }
97 83 }
98 - catch ( InvocationTargetException e )
99 - {
84 + catch ( InvocationTargetException e ) {
100 85 throw new RuntimeException( e );
101 86 }
102 - catch ( NoSuchMethodException e )
103 - {
87 + catch ( NoSuchMethodException e ) {
104 88 throw new RuntimeException( e );
105 89 }
106 - catch ( IllegalAccessException e )
107 - {
90 + catch ( IllegalAccessException e ) {
108 91 throw new RuntimeException( e );
109 92 }
110 93 // Setter methods aren't actual attributes, so skip the rest of the iteration.
  @@ -112,57 +95,43 @@
112 95 }
113 96
114 97 boolean isURLAttribute = false;
115 - if ( attrName.endsWith( PO_URL_ATTRIBUTE ) )
116 - {
98 + if ( attrName.endsWith( PO_URL_ATTRIBUTE ) ) {
117 99 attrName = attrName.substring( 0, attrName.length() - PO_URL_ATTRIBUTE.length() );
118 100 isURLAttribute = true;
119 101 }
120 - if ( attrName.endsWith( FILE_ATTRIBUTE ) )
121 - {
102 + if ( attrName.endsWith( FILE_ATTRIBUTE ) ) {
122 103 attrName = attrName.substring( 0, attrName.length() - FILE_ATTRIBUTE.length() );
123 104 value = readFileAttributeValue( value );
124 105 }
125 106
126 107 AttributeAccessorSCD scd = zPO.getMetaDataForPO().getAccessorSCDoptional( attrName );
127 - if ( scd == null )
128 - {
108 + if ( scd == null ) {
129 109 throw new IllegalArgumentException( zPO.getObjectName() + " does NOT appear to have an attribute named: '" + attrName + "'" );
130 110 }
131 - if ( !(scd instanceof NonImportableFeature) )
132 - {
111 + if ( !(scd instanceof NonImportableFeature) ) {
133 112 Object setValue = value;
134 - if ( AttributeAccessorSCD.Form.ToOne == scd.getForm() )
135 - {
136 - if ( scd instanceof AttributeAccessorSCDtoOneVariable )
137 - {
113 + if ( AttributeAccessorSCD.Form.ToOne == scd.getForm() ) {
114 + if ( scd instanceof AttributeAccessorSCDtoOneVariable ) {
138 115 PO_VarURLstringHelper zVarURL = new PO_VarURLstringHelper( value );
139 116 FauxPOref childRef = new FauxPOref( mTransaction, zVarURL.getPersistentObjectURL() );
140 117 PersistentObject<?> po = putLineInDB( childRef );
141 118 setValue = new PO_VarURLstringHelper( zVarURL.getPersistedObjectAttributeName(), po.getPersistentObjectURL().toString() );
142 - }
143 - else
144 - {
119 + } else {
145 120 FauxPOref childRef = new FauxPOref( mTransaction, value );
146 121 setValue = putLineInDB( childRef );
147 122 }
148 - }
149 - else if ( looksLikeAReference( scd, value ) )
150 - {
123 + } else if ( looksLikeAReference( scd, value ) ) {
151 124 // support quasi-foreign keys.
152 125 FauxPOref childRef = new FauxPOref( mTransaction, value );
153 126 setValue = putLineInDB( childRef );
154 127 PersistentObject po = (PersistentObject) setValue;
155 - try
156 - {
128 + try {
157 129 setValue = po.getPersistentObjectUniqueKey();
158 130 }
159 - catch ( Exception e )
160 - {
131 + catch ( Exception e ) {
161 132 e.printStackTrace();
162 133 }
163 - }
164 - else if ( isURLAttribute )
165 - {
134 + } else if ( isURLAttribute ) {
166 135 FauxPOref childRef = new FauxPOref( mTransaction, value );
167 136 setValue = putLineInDB( childRef ).getPersistentObjectURL().toString();
168 137 }
  @@ -173,37 +142,28 @@
173 142 return zPO;
174 143 }
175 144
176 - private boolean looksLikeAReference( AttributeAccessorSCD pScd, String pValue )
177 - {
178 - if ( !(pScd.getColumnType().equals( Long.class )) )
179 - {
145 + private boolean looksLikeAReference( AttributeAccessorSCD pScd, String pValue ) {
146 + if ( !(pScd.getColumnType().equals( Long.class )) ) {
180 147 return false;
181 148 }
182 - try
183 - {
149 + try {
184 150 new FauxPOref( mTransaction, pValue );
185 151 return true;
186 152 }
187 - catch ( Exception e )
188 - {
153 + catch ( Exception e ) {
189 154 return false;
190 155 }
191 156 }
192 157
193 - private String readFileAttributeValue( String fileReference )
194 - {
195 - if ( fileReference.startsWith( "file:" ) )
196 - {
158 + private String readFileAttributeValue( String fileReference ) {
159 + if ( fileReference.startsWith( "file:" ) ) {
197 160 fileReference = fileReference.substring( "file:".length() );
198 161 }
199 162
200 163 File f;
201 - if ( null != mReferenceDir )
202 - {
164 + if ( null != mReferenceDir ) {
203 165 f = new File( mReferenceDir, fileReference );
204 - }
205 - else
206 - {
166 + } else {
207 167 f = new File( fileReference );
208 168 }
209 169
  @@ -212,31 +172,23 @@
212 172 }
213 173 }
214 174
215 - protected static class ImportToMap
216 - {
175 + protected static class ImportToMap {
217 176 private Map<FauxPOref, Attributes> mLineMap = new HashMap<FauxPOref, Attributes>();
218 177
219 178 public ImportToMap process( Logger pLogger, Finder pFinder, Reader[] pReaders )
220 - throws IOException
221 - {
222 - if ( Objects.isNullOrEmpty( pReaders ) )
223 - {
179 + throws IOException {
180 + if ( Objects.isNullOrEmpty( pReaders ) ) {
224 181 throw new IllegalArgumentException( "No Readers provided" );
225 182 }
226 - try
227 - {
183 + try {
228 184 new Inner( pLogger, pFinder ).process( pReaders );
229 185 }
230 - finally
231 - {
232 - for ( Reader reader : pReaders )
233 - {
234 - try
235 - {
186 + finally {
187 + for ( Reader reader : pReaders ) {
188 + try {
236 189 reader.close();
237 190 }
238 - catch ( IOException e )
239 - {
191 + catch ( IOException e ) {
240 192 // Whatever!
241 193 }
242 194 }
  @@ -245,96 +197,75 @@
245 197 return this;
246 198 }
247 199
248 - public Map<FauxPOref, Attributes> getLineMap()
249 - {
200 + public Map<FauxPOref, Attributes> getLineMap() {
250 201 return mLineMap;
251 202 }
252 203
253 - private class Inner
254 - {
204 + private class Inner {
255 205 private Logger mLogger;
256 206 private Finder mFinder;
257 207 private StringBuilder mBuilder;
258 208
259 - private Inner( Logger pLogger, Finder pFinder )
260 - {
209 + private Inner( Logger pLogger, Finder pFinder ) {
261 210 mLogger = pLogger;
262 211 mFinder = pFinder;
263 212 }
264 213
265 214 public void process( Reader[] pReaders )
266 - throws IOException
267 - {
268 - if ( pReaders[0] instanceof ToStringInformative )
269 - {
215 + throws IOException {
216 + if ( pReaders[0] instanceof ToStringInformative ) {
270 217 mLogger.info.log( "Importing:" );
271 218 }
272 - for ( Reader reader : pReaders )
273 - {
274 - if ( reader instanceof ToStringInformative )
275 - {
219 + for ( Reader reader : pReaders ) {
220 + if ( reader instanceof ToStringInformative ) {
276 221 mLogger.info.log( " ", reader );
277 222 }
278 223 BufferedReader bufRead = Utils.getBufferedReader( reader );
279 224 mBuilder = new StringBuilder();
280 - for ( String line; null != (line = bufRead.readLine()); )
281 - {
225 + for ( String line; null != (line = bufRead.readLine()); ) {
282 226 putLineInMap( line );
283 227 }
284 - if ( mBuilder.length() != 0 )
285 - {
228 + if ( mBuilder.length() != 0 ) {
286 229 throw new UnclosedQuoteException( " did not find end of line " + mBuilder );
287 230 }
288 231 }
289 232 }
290 233
291 - private void putLineInMap( String pLine )
292 - {
293 - if ( mBuilder.length() == 0 )
294 - {
234 + private void putLineInMap( String pLine ) {
235 + if ( mBuilder.length() == 0 ) {
295 236 pLine = pLine.trim();
296 - if ( (pLine.length() == 0) || (pLine.charAt( 0 ) == ';') || (pLine.charAt( 0 ) == '#') || pLine.startsWith( "//" ) )
297 - {
237 + if ( (pLine.length() == 0) || (pLine.charAt( 0 ) == ';') || (pLine.charAt( 0 ) == '#') || pLine.startsWith( "//" ) ) {
298 238 return;
299 239 }
300 240 }
301 241 mBuilder.append( pLine );
302 - try
303 - {
242 + try {
304 243 putBuilderInMap();
305 244 mBuilder.setLength( 0 );
306 245 }
307 - catch ( UnclosedQuoteException e )
308 - {
246 + catch ( UnclosedQuoteException e ) {
309 247 mBuilder.append( "\n" );
310 248 }
311 249 }
312 250
313 - private void putBuilderInMap()
314 - {
251 + private void putBuilderInMap() {
315 252 String[] decodedData = new CsvSupport().decode( mBuilder.toString() );
316 253 FauxPOref ref = new FauxPOref( mFinder, decodedData[0] );
317 - if ( mFinder.getMetaDataRequired( ref.getPersistedObjectRegistrationName() ).isImportable() )
318 - {
254 + if ( mFinder.getMetaDataRequired( ref.getPersistedObjectRegistrationName() ).isImportable() ) {
319 255 Attributes attributes = parseData( decodedData );
320 - if ( !attributes.isEmpty() )
321 - {
256 + if ( !attributes.isEmpty() ) {
322 257 mLineMap.put( ref, attributes );
323 258 }
324 259 }
325 260 }
326 261
327 - private Attributes parseData( String[] pDecodedData )
328 - {
262 + private Attributes parseData( String[] pDecodedData ) {
329 263 Attributes zAttributes = new Attributes();
330 - for ( int i = 1; i < pDecodedData.length; ++i )
331 - {
264 + for ( int i = 1; i < pDecodedData.length; ++i ) {
332 265 String pair = Strings.noEmpty( pDecodedData[i] );
333 - if ( pair != null )
334 - {
266 + if ( pair != null ) {
335 267 int equidx = pair.indexOf( '=' );
336 - if ( equidx == -1 )
337 - {
268 + if ( equidx == -1 ) {
338 269 throw new IllegalStateException( "No '=' in: " + pair );
339 270 }
340 271 String attr = pair.substring( 0, equidx ).trim();
  @@ -347,49 +278,41 @@
347 278 }
348 279 }
349 280
350 - protected static class FauxPOref
351 - {
281 + protected static class FauxPOref {
352 282 public static int sWildcardSequence = 0;
353 283
354 284 private MetaDataForPO mPOmd;
355 285 private String mIdentifier;
356 286
357 - public FauxPOref( Finder pFinder, String pToString )
358 - {
287 + public FauxPOref( Finder pFinder, String pToString ) {
359 288 PO_URLstringHelper helper = new PO_URLstringHelper( pToString );
360 289 String zIdentifierOrRegistrationName = helper.getPersistedObjectRegistrationName();
361 290 MetaDataForPO zMD = pFinder.getMetaDataOptionallyByIdentifier( zIdentifierOrRegistrationName );
362 291 mPOmd = (zMD != null) ? zMD : pFinder.getMetaDataRequired( zIdentifierOrRegistrationName );
363 292 mIdentifier = helper.getPersistentObjectUniqueKey();
364 - if ( "?".equals( mIdentifier ) )
365 - {
293 + if ( "?".equals( mIdentifier ) ) {
366 294 mIdentifier = "gened-" + ++sWildcardSequence;
367 295 }
368 296 }
369 297
370 - public MetaDataForPO getPOmd()
371 - {
298 + public MetaDataForPO getPOmd() {
372 299 return mPOmd;
373 300 }
374 301
375 - public String getPersistedObjectRegistrationName()
376 - {
302 + public String getPersistedObjectRegistrationName() {
377 303 return mPOmd.getPOregistrationName();
378 304 }
379 305
380 - public String getIdentifier()
381 - {
306 + public String getIdentifier() {
382 307 return mIdentifier;
383 308 }
384 309
385 310 @Override
386 - public boolean equals( Object o )
387 - {
311 + public boolean equals( Object o ) {
388 312 return (this == o) || ((o instanceof FauxPOref) && equals( (FauxPOref) o ));
389 313 }
390 314
391 - public boolean equals( FauxPOref them )
392 - {
315 + public boolean equals( FauxPOref them ) {
393 316 return (this == them) || //
394 317 ((them != null) //
395 318 && (this.mPOmd == them.mPOmd) //
  @@ -398,64 +321,50 @@
398 321 }
399 322
400 323 @Override
401 - public int hashCode()
402 - {
324 + public int hashCode() {
403 325 return 31 * getPersistedObjectRegistrationName().hashCode() + mIdentifier.hashCode();
404 326 }
405 327
406 328 @Override
407 - public String toString()
408 - {
329 + public String toString() {
409 330 return new PO_URLstringHelper( getPersistedObjectRegistrationName(), mIdentifier ).toString();
410 331 }
411 332 }
412 333
413 - protected static class Attributes
414 - {
334 + protected static class Attributes {
415 335 private Map<String, String> mNameValues = new HashMap<String, String>();
416 336
417 - public boolean isEmpty()
418 - {
337 + public boolean isEmpty() {
419 338 return mNameValues.isEmpty();
420 339 }
421 340
422 - public String[] getNames()
423 - {
341 + public String[] getNames() {
424 342 return mNameValues.keySet().toArray( new String[mNameValues.size()] );
425 343 }
426 344
427 - public String getValue( String pName )
428 - {
345 + public String getValue( String pName ) {
429 346 return mNameValues.get( pName );
430 347 }
431 348
432 - public void setNameValue( String pName, String pValue )
433 - {
434 - if ( pName.endsWith( TODAY_RELATIVE ) )
435 - {
349 + public void setNameValue( String pName, String pValue ) {
350 + if ( pName.endsWith( TODAY_RELATIVE ) ) {
436 351 pName = pName.substring( 0, pName.length() - TODAY_RELATIVE.length() ).trim();
437 352 pValue = makeTodayRelative( pValue );
438 - }
439 - else if ( pName.endsWith( DELTA_DATE_RELATIVE ) )
440 - {
353 + } else if ( pName.endsWith( DELTA_DATE_RELATIVE ) ) {
441 354 pName = pName.substring( 0, pName.length() - DELTA_DATE_RELATIVE.length() ).trim();
442 355 pValue = makeDeltaDateRelative( pValue );
443 356 }
444 - if ( pName.length() != 0 )
445 - {
357 + if ( pName.length() != 0 ) {
446 358 String curValue = (pValue.length() == 0) ? mNameValues.get( pName ) : mNameValues.put( pName, pValue );
447 - if ( curValue != null )
448 - {
359 + if ( curValue != null ) {
449 360 throw new IllegalArgumentException( "Duplicate Attribute for: " + pName );
450 361 }
451 362 }
452 363 }
453 364
454 - private String makeTodayRelative( String pValue )
455 - {
365 + private String makeTodayRelative( String pValue ) {
456 366 boolean zAdd;
457 - switch ( (pValue = pValue + " ").charAt( 0 ) )
458 - {
367 + switch ( (pValue = pValue + " ").charAt( 0 ) ) {
459 368 case '+':
460 369 zAdd = true;
461 370 break;
  @@ -467,8 +376,7 @@
467 376 }
468 377 String zYears, zMonths, zDays;
469 378 String[] zParts = Strings.parseChar( pValue.substring( 1 ).trim(), '/' );
470 - switch ( zParts.length )
471 - {
379 + switch ( zParts.length ) {
472 380 case 3:
473 381 zYears = zParts[0];
474 382 zMonths = zParts[1];
  @@ -492,14 +400,11 @@
492 400
493 401 CalendarYMD zDesiredDate = CalendarYMD.today();
494 402
495 - if ( zAdd )
496 - {
403 + if ( zAdd ) {
497 404 zDesiredDate = zDesiredDate.addYears( iYears );
498 405 zDesiredDate = zDesiredDate.addMonths( iMonths );
499 406 zDesiredDate = zDesiredDate.addDays( iDays );
500 - }
501 - else
502 - {
407 + } else {
503 408 zDesiredDate = zDesiredDate.minusYears( iYears );
504 409 zDesiredDate = zDesiredDate.minusMonths( iMonths );
505 410 zDesiredDate = zDesiredDate.minusDays( iDays );
  @@ -508,11 +413,9 @@
508 413 return zDesiredDate.toSQLvalue();
509 414 }
510 415
511 - private String makeDeltaDateRelative( String pValue )
512 - {
416 + private String makeDeltaDateRelative( String pValue ) {
513 417 int zAt = pValue.indexOf( DELTA_DATE_SEP );
514 - if ( zAt == -1 )
515 - {
418 + if ( zAt == -1 ) {
516 419 throw new IllegalArgumentException( "No Delta Date Sep '" + DELTA_DATE_SEP + "'" );
517 420 }
518 421 CalendarYMD zDesiredDate = to_CalendarYMD( "Desired Date", pValue.substring( 0, zAt ) );
  @@ -522,30 +425,23 @@
522 425 return zDesiredDate.toString();
523 426 }
524 427
525 - private CalendarYMD to_CalendarYMD( String pWhat, String pString )
526 - {
527 - try
528 - {
428 + private CalendarYMD to_CalendarYMD( String pWhat, String pString ) {
429 + try {
529 430 return TypeConverter.to_CalendarYMD( pString.trim() );
530 431 }
531 - catch ( RuntimeException e )
532 - {
432 + catch ( RuntimeException e ) {
533 433 throw new IllegalArgumentException( pWhat + ":" + e.getMessage(), e );
534 434 }
535 435 }
536 436
537 - private int parseInt( String pWhat, String pString )
538 - {
539 - if ( (pString = pString.trim()).length() == 0 )
540 - {
437 + private int parseInt( String pWhat, String pString ) {
438 + if ( (pString = pString.trim()).length() == 0 ) {
541 439 return 0;
542 440 }
543 - try
544 - {
441 + try {
545 442 return Integer.parseInt( pString );
546 443 }
547 - catch ( NumberFormatException e )
548 - {
444 + catch ( NumberFormatException e ) {
549 445 throw new IllegalArgumentException( pWhat + ":" + e.getMessage(), e );
550 446 }
551 447 }