comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java @ 8024:963ede7b32bb

Renamed SedimentLoad to SedimentLoadLSData to make place for SedimentLoad in backend.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 09 Jul 2014 17:33:57 +0200
parents b468dc65154a
children 5e3f4b4fcb28
comparison
equal deleted inserted replaced
8023:f7f86f4e4c8d 8024:963ede7b32bb
214 214
215 /** 215 /**
216 * @param river name of river. 216 * @param river name of river.
217 * @param type 217 * @param type
218 */ 218 */
219 public static SedimentLoad[] getLoads( 219 public static SedimentLoadLSData[] getLoads(
220 String river, 220 String river,
221 String type, 221 String type,
222 double startKm, 222 double startKm,
223 double endKm 223 double endKm
224 ) { 224 ) {
235 235
236 Element element = cache.get(key); 236 Element element = cache.get(key);
237 237
238 if (element != null) { 238 if (element != null) {
239 log.debug("SedimentLoad found in cache"); 239 log.debug("SedimentLoad found in cache");
240 return (SedimentLoad[])element.getValue(); 240 return (SedimentLoadLSData[])element.getValue();
241 } 241 }
242 242
243 SedimentLoad[] values = 243 SedimentLoadLSData[] values =
244 getSedimentLoadsUncached(river, type, startKm, endKm); 244 getSedimentLoadsUncached(river, type, startKm, endKm);
245 245
246 if (values != null && key != null) { 246 if (values != null && key != null) {
247 log.debug("Store static sediment load values in cache."); 247 log.debug("Store static sediment load values in cache.");
248 element = new Element(key, values); 248 element = new Element(key, values);
254 /** 254 /**
255 * Get a sedimentLoad filled with data from db (or cache). 255 * Get a sedimentLoad filled with data from db (or cache).
256 * @param type "epoch", "off_epoch" or "single" 256 * @param type "epoch", "off_epoch" or "single"
257 * @return A Sedimentload filled with values from db or cache. 257 * @return A Sedimentload filled with values from db or cache.
258 */ 258 */
259 public static SedimentLoad getLoadWithData( 259 public static SedimentLoadLSData getLoadWithData(
260 String river, 260 String river,
261 String type, 261 String type,
262 double startKm, 262 double startKm,
263 double endKm, 263 double endKm,
264 int syear, 264 int syear,
283 283
284 Element element = cache.get(key); 284 Element element = cache.get(key);
285 285
286 if (element != null) { 286 if (element != null) {
287 log.debug("SedimentLoad found in cache"); 287 log.debug("SedimentLoad found in cache");
288 return (SedimentLoad)element.getValue(); 288 return (SedimentLoadLSData)element.getValue();
289 } 289 }
290 290
291 SedimentLoad values = getSedimentLoadWithDataUncached( 291 SedimentLoadLSData values = getSedimentLoadWithDataUncached(
292 river, 292 river,
293 type, 293 type,
294 startKm, 294 startKm,
295 endKm, 295 endKm,
296 syear, 296 syear,
384 * Get sediment loads from db. 384 * Get sediment loads from db.
385 * @param river the river 385 * @param river the river
386 * @param type the sediment load type (year or epoch) 386 * @param type the sediment load type (year or epoch)
387 * @return according sediment loads. 387 * @return according sediment loads.
388 */ 388 */
389 public static SedimentLoad[] getSedimentLoadsUncached( 389 public static SedimentLoadLSData[] getSedimentLoadsUncached(
390 String river, 390 String river,
391 String type, 391 String type,
392 double startKm, 392 double startKm,
393 double endKm 393 double endKm
394 ) { 394 ) {
403 .addScalar("year", StandardBasicTypes.DATE); 403 .addScalar("year", StandardBasicTypes.DATE);
404 sqlQuery.setString("name", river); 404 sqlQuery.setString("name", river);
405 sqlQuery.setDouble("startKm", startKm); 405 sqlQuery.setDouble("startKm", startKm);
406 sqlQuery.setDouble("endKm", endKm); 406 sqlQuery.setDouble("endKm", endKm);
407 List<Object []> results = sqlQuery.list(); 407 List<Object []> results = sqlQuery.list();
408 SedimentLoad[] loads = new SedimentLoad[results.size()]; 408 SedimentLoadLSData[] loads = new SedimentLoadLSData[results.size()];
409 for (int i = 0, R = results.size(); i < R; i++) { 409 for (int i = 0, R = results.size(); i < R; i++) {
410 Object[] row = results.get(i); 410 Object[] row = results.get(i);
411 loads[i] = new SedimentLoad( 411 loads[i] = new SedimentLoadLSData(
412 (String) row[0], 412 (String) row[0],
413 (Date) row[1], 413 (Date) row[1],
414 null, 414 null,
415 false, 415 false,
416 ""); 416 "");
425 sqlQuery.setString("name", river); 425 sqlQuery.setString("name", river);
426 sqlQuery.setDouble("startKm", startKm); 426 sqlQuery.setDouble("startKm", startKm);
427 sqlQuery.setDouble("endKm", endKm); 427 sqlQuery.setDouble("endKm", endKm);
428 List<Object []> results = sqlQuery.list(); 428 List<Object []> results = sqlQuery.list();
429 429
430 SedimentLoad[] loads = new SedimentLoad[results.size()]; 430 SedimentLoadLSData[] loads = new SedimentLoadLSData[results.size()];
431 for (int i = 0, R = results.size(); i < R; i++) { 431 for (int i = 0, R = results.size(); i < R; i++) {
432 Object[] row = results.get(i); 432 Object[] row = results.get(i);
433 loads[i] = new SedimentLoad( 433 loads[i] = new SedimentLoadLSData(
434 (String) row[0], 434 (String) row[0],
435 (Date) row[1], 435 (Date) row[1],
436 (Date) row[2], 436 (Date) row[2],
437 true, 437 true,
438 ""); 438 "");
446 sqlQuery.setString("name", river); 446 sqlQuery.setString("name", river);
447 sqlQuery.setDouble("startKm", startKm); 447 sqlQuery.setDouble("startKm", startKm);
448 sqlQuery.setDouble("endKm", endKm); 448 sqlQuery.setDouble("endKm", endKm);
449 List<Object []> results = sqlQuery.list(); 449 List<Object []> results = sqlQuery.list();
450 450
451 SedimentLoad[] loads = new SedimentLoad[results.size()]; 451 SedimentLoadLSData[] loads = new SedimentLoadLSData[results.size()];
452 for (int i = 0, R = results.size(); i < R; i++) { 452 for (int i = 0, R = results.size(); i < R; i++) {
453 Object[] row = results.get(i); 453 Object[] row = results.get(i);
454 loads[i] = new SedimentLoad( 454 loads[i] = new SedimentLoadLSData(
455 ((Date) row[0]).toString() + (Date) row[1], 455 ((Date) row[0]).toString() + (Date) row[1],
456 (Date) row[0], 456 (Date) row[0],
457 (Date) row[1], 457 (Date) row[1],
458 true, 458 true,
459 ""); 459 "");
461 return loads; 461 return loads;
462 } 462 }
463 else { 463 else {
464 log.warn("getSedimentLoadsUncached does not understand type " + type); 464 log.warn("getSedimentLoadsUncached does not understand type " + type);
465 } 465 }
466 return new SedimentLoad[0]; 466 return new SedimentLoadLSData[0];
467 } 467 }
468 468
469 /** 469 /**
470 * Get a specific sediment load from db. 470 * Get a specific sediment load from db.
471 * 471 *
472 * @param id the sediment yields id. 472 * @param id the sediment yields id.
473 * 473 *
474 * @return according sediment load. 474 * @return according sediment load.
475 */ 475 */
476 public static SedimentLoad getSedimentLoadWithDataUncached( 476 public static SedimentLoadLSData getSedimentLoadWithDataUncached(
477 String id, 477 String id,
478 String river 478 String river
479 ) { 479 ) {
480 log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached / id " + id); 480 log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached / id " + id);
481 Session session = SessionHolder.HOLDER.get(); 481 Session session = SessionHolder.HOLDER.get();
516 .addScalar("fraction", StandardBasicTypes.STRING) 516 .addScalar("fraction", StandardBasicTypes.STRING)
517 .addScalar("unit", StandardBasicTypes.STRING); 517 .addScalar("unit", StandardBasicTypes.STRING);
518 sqlQuery.setInteger("id", Integer.valueOf(id)); 518 sqlQuery.setInteger("id", Integer.valueOf(id));
519 519
520 List<Object []> results = sqlQuery.list(); 520 List<Object []> results = sqlQuery.list();
521 SedimentLoad load = new SedimentLoad(); 521 SedimentLoadLSData load = new SedimentLoadLSData();
522 if (results.isEmpty()) { 522 if (results.isEmpty()) {
523 log.warn("Empty result for year calculation."); 523 log.warn("Empty result for year calculation.");
524 } 524 }
525 else { 525 else {
526 Object[] row = results.get(0); 526 Object[] row = results.get(0);
527 load = new SedimentLoad( 527 load = new SedimentLoadLSData(
528 (String) row[0], //description 528 (String) row[0], //description
529 null,//(Date) row[1], //start 529 null,//(Date) row[1], //start
530 null, //end 530 null, //end
531 false, //isEpoch 531 false, //isEpoch
532 (String) row[4]); //unit 532 (String) row[4]); //unit
567 * @param river the river 567 * @param river the river
568 * @param type the sediment load type (year, epoch or off_epoch) 568 * @param type the sediment load type (year, epoch or off_epoch)
569 * 569 *
570 * @return according sediment loads. 570 * @return according sediment loads.
571 */ 571 */
572 public static SedimentLoad getSedimentLoadWithDataUncached( 572 public static SedimentLoadLSData getSedimentLoadWithDataUncached(
573 String river, 573 String river,
574 String type, 574 String type,
575 double startKm, 575 double startKm,
576 double endKm, 576 double endKm,
577 int syear, 577 int syear,
627 sqlQuery.setDouble("endKm", endKm); 627 sqlQuery.setDouble("endKm", endKm);
628 sqlQuery.setDate("begin", start.getTime()); 628 sqlQuery.setDate("begin", start.getTime());
629 sqlQuery.setDate("end", end.getTime()); 629 sqlQuery.setDate("end", end.getTime());
630 sqlQuery.setString("grain", "total"); 630 sqlQuery.setString("grain", "total");
631 List<Object []> results = sqlQuery.list(); 631 List<Object []> results = sqlQuery.list();
632 SedimentLoad load = new SedimentLoad(); 632 SedimentLoadLSData load = new SedimentLoadLSData();
633 if (results.isEmpty()) { 633 if (results.isEmpty()) {
634 log.warn("Empty result for year calculation."); 634 log.warn("Empty result for year calculation.");
635 } 635 }
636 else { 636 else {
637 Object[] row = results.get(0); 637 Object[] row = results.get(0);
638 load = new SedimentLoad( 638 load = new SedimentLoadLSData(
639 (String) row[0], //description 639 (String) row[0], //description
640 (Date) row[1], //start 640 (Date) row[1], //start
641 null, //end 641 null, //end
642 false, //isEpoch 642 false, //isEpoch
643 (String) row[4]); //unit 643 (String) row[4]); //unit
676 sqlQuery.setString("grain", "total"); 676 sqlQuery.setString("grain", "total");
677 677
678 List<Object[]> results = null; 678 List<Object[]> results = null;
679 results = sqlQuery.list(); 679 results = sqlQuery.list();
680 680
681 SedimentLoad load = null; 681 SedimentLoadLSData load = null;
682 if (results.isEmpty()) { 682 if (results.isEmpty()) {
683 log.warn("No total results for off-epoch"); 683 log.warn("No total results for off-epoch");
684 } 684 }
685 else { 685 else {
686 Object[] row = results.get(0); 686 Object[] row = results.get(0);
687 load = new SedimentLoad( 687 load = new SedimentLoadLSData(
688 (String) row[0], 688 (String) row[0],
689 (Date) row[1], 689 (Date) row[1],
690 (Date) row[4], 690 (Date) row[4],
691 true, 691 true,
692 (String)row[5]); 692 (String)row[5]);
703 load = getValues("suspended_sediment", sqlQuery, load, suspStations); 703 load = getValues("suspended_sediment", sqlQuery, load, suspStations);
704 load = getValues("susp_sand_bed", sqlQuery, load, floatStations); 704 load = getValues("susp_sand_bed", sqlQuery, load, floatStations);
705 load = getValues("susp_sand", sqlQuery, load, floatStations); 705 load = getValues("susp_sand", sqlQuery, load, floatStations);
706 return load; 706 return load;
707 } 707 }
708 return new SedimentLoad(); 708 return new SedimentLoadLSData();
709 } 709 }
710 710
711 protected static Range findMeasurementStationRange( 711 protected static Range findMeasurementStationRange(
712 TreeMap<Double, MeasurementStation> stations, 712 TreeMap<Double, MeasurementStation> stations,
713 double km 713 double km
741 * @param query query in which to set 'grain' parameter and run. 741 * @param query query in which to set 'grain' parameter and run.
742 * @param load[out] SedimentLoad which to populate with values. if null 742 * @param load[out] SedimentLoad which to populate with values. if null
743 * and values are found, return a new load. 743 * and values are found, return a new load.
744 * @return param load or new load if null. 744 * @return param load or new load if null.
745 */ 745 */
746 protected static SedimentLoad getValues ( 746 protected static SedimentLoadLSData getValues (
747 String fraction, 747 String fraction,
748 SQLQuery query, 748 SQLQuery query,
749 SedimentLoad load, 749 SedimentLoadLSData load,
750 TreeMap<Double, MeasurementStation> stations 750 TreeMap<Double, MeasurementStation> stations
751 ) { 751 ) {
752 query.setString("grain", fraction); 752 query.setString("grain", fraction);
753 List<Object[]> results = query.list(); 753 List<Object[]> results = query.list();
754 754
755 // We have the first results for this query, create new load. 755 // We have the first results for this query, create new load.
756 if (!results.isEmpty() && load == null) { 756 if (!results.isEmpty() && load == null) {
757 Object[] row = results.get(0); 757 Object[] row = results.get(0);
758 load = new SedimentLoad( 758 load = new SedimentLoadLSData(
759 (String) row[0], 759 (String) row[0],
760 (Date) row[1], 760 (Date) row[1],
761 (Date) row[4], 761 (Date) row[4],
762 true, 762 true,
763 (String)row[5]); 763 (String)row[5]);
788 } 788 }
789 789
790 790
791 /** Set a fraction value of load to given km, value and range. */ 791 /** Set a fraction value of load to given km, value and range. */
792 private static void setLoadFraction( 792 private static void setLoadFraction(
793 SedimentLoad load, double km, double v, Range range, String fraction) { 793 SedimentLoadLSData load, double km, double v, Range range, String fraction) {
794 if (fraction.equals("coarse")) { 794 if (fraction.equals("coarse")) {
795 load.setCoarse(km, v, range); 795 load.setCoarse(km, v, range);
796 } 796 }
797 else if (fraction.equals("sand")) { 797 else if (fraction.equals("sand")) {
798 load.setSand(km, v, range); 798 load.setSand(km, v, range);
815 else { 815 else {
816 log.error("Unknown fraction type " + fraction); 816 log.error("Unknown fraction type " + fraction);
817 } 817 }
818 } 818 }
819 819
820 public static SedimentLoad getLoadUnknown( 820 public static SedimentLoadLSData getLoadUnknown(
821 String river, 821 String river,
822 String description, 822 String description,
823 String type 823 String type
824 ) { 824 ) {
825 log.debug("SedimentLoadFactory.getLoadWithData"); 825 log.debug("SedimentLoadFactory.getLoadWithData");
835 835
836 Element element = cache.get(key); 836 Element element = cache.get(key);
837 837
838 if (element != null) { 838 if (element != null) {
839 log.debug("SedimentLoad found in cache"); 839 log.debug("SedimentLoad found in cache");
840 return (SedimentLoad)element.getValue(); 840 return (SedimentLoadLSData)element.getValue();
841 } 841 }
842 842
843 SedimentLoad values = 843 SedimentLoadLSData values =
844 getSedimentLoadUnknownUncached(river, description, type); 844 getSedimentLoadUnknownUncached(river, description, type);
845 845
846 if (values != null && key != null) { 846 if (values != null && key != null) {
847 log.debug("Store static bed height values in cache."); 847 log.debug("Store static bed height values in cache.");
848 element = new Element(key, values); 848 element = new Element(key, values);
855 * Get sediment loads with fraction 'unknown' from db. 855 * Get sediment loads with fraction 'unknown' from db.
856 * @param river the river 856 * @param river the river
857 * @param type the sediment load type (year or epoch) 857 * @param type the sediment load type (year or epoch)
858 * @return according sediment loads. 858 * @return according sediment loads.
859 */ 859 */
860 public static SedimentLoad getSedimentLoadUnknownUncached( 860 public static SedimentLoadLSData getSedimentLoadUnknownUncached(
861 String river, 861 String river,
862 String description, 862 String description,
863 String type 863 String type
864 ) { 864 ) {
865 log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached"); 865 log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached");
880 } 880 }
881 else { 881 else {
882 sqlQuery.setInteger("type", 0); 882 sqlQuery.setInteger("type", 0);
883 } 883 }
884 List<Object []> results = sqlQuery.list(); 884 List<Object []> results = sqlQuery.list();
885 SedimentLoad load = new SedimentLoad(); 885 SedimentLoadLSData load = new SedimentLoadLSData();
886 if (results.isEmpty()) { 886 if (results.isEmpty()) {
887 return new SedimentLoad(); 887 return new SedimentLoadLSData();
888 } 888 }
889 Object[] row = results.get(0); 889 Object[] row = results.get(0);
890 load = new SedimentLoad( 890 load = new SedimentLoadLSData(
891 (String) row[0], 891 (String) row[0],
892 (Date) row[1], 892 (Date) row[1],
893 (Date) row[2], 893 (Date) row[2],
894 false, 894 false,
895 (String)row[5]); 895 (String)row[5]);
907 * Return sediment loads with 'unknown' fraction type. 907 * Return sediment loads with 'unknown' fraction type.
908 * @param river Name of the river 908 * @param river Name of the river
909 * @param unit Restrict result set to those of given unit. 909 * @param unit Restrict result set to those of given unit.
910 * @param type Type like year, epoch, off_epoch 910 * @param type Type like year, epoch, off_epoch
911 */ 911 */
912 public static SedimentLoad[] getSedimentLoadUnknown( 912 public static SedimentLoadLSData[] getSedimentLoadUnknown(
913 String river, 913 String river,
914 String unit, 914 String unit,
915 String type) { 915 String type) {
916 Session session = SessionHolder.HOLDER.get(); 916 Session session = SessionHolder.HOLDER.get();
917 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_UNKNOWN) 917 SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_UNKNOWN)
925 } 925 }
926 else { 926 else {
927 sqlQuery.setInteger("type", 0); 927 sqlQuery.setInteger("type", 0);
928 } 928 }
929 List<Object[]> results = sqlQuery.list(); 929 List<Object[]> results = sqlQuery.list();
930 SedimentLoad[] loads = new SedimentLoad[results.size()]; 930 SedimentLoadLSData[] loads = new SedimentLoadLSData[results.size()];
931 int counter = 0; 931 int counter = 0;
932 for (Object[] row: results) { 932 for (Object[] row: results) {
933 loads[counter] = new SedimentLoad( 933 loads[counter] = new SedimentLoadLSData(
934 (String)row[0], (Date)row[1], (Date)row[2], false, ""); 934 (String)row[0], (Date)row[1], (Date)row[2], false, "");
935 counter++; 935 counter++;
936 } 936 }
937 return loads; 937 return loads;
938 } 938 }

http://dive4elements.wald.intevation.org