/exchange/src/main/java/org/color4j/imports/mdb/SimpleColorDBExtractor.java

https://github.com/color4j/color4j · Java · 760 lines · 615 code · 126 blank · 19 comment · 89 complexity · 5cee24bf7f7683d81d19fd8c68a39334 MD5 · raw file

  1. /*
  2. * Copyright (c) 2000-2011 Niclas Hedhman.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13. * implied.
  14. *
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.color4j.imports.mdb;
  19. import java.sql.Connection;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.sql.SQLException;
  23. import java.sql.Statement;
  24. import java.text.DecimalFormat;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.TreeMap;
  31. import org.color4j.imports.ImportException;
  32. /**
  33. */
  34. public class SimpleColorDBExtractor
  35. implements ColorDBExtractor
  36. {
  37. private final static int LOW = 360;
  38. private final static int INTERVAL = 10;
  39. private final static int SPECTRAL_COUNT = 40;
  40. private final static DecimalFormat SPECTRAL_COLUMN =
  41. new DecimalFormat( "SPECTRAL00" );
  42. protected String[] m_standardIDs;
  43. protected Map<String, String> m_standardNames;
  44. protected Map<String, Date> m_standardDates;
  45. protected Map m_standardDescriptions;
  46. protected Map m_standardAttributes;
  47. protected Map<String, TreeMap<Integer, Object>> m_standardSpectrums;
  48. protected Map<String, String> m_standardApertures;
  49. protected Map<String, String> m_standardLights;
  50. protected Map<String, String> m_standardSpeculars;
  51. protected Map<String, String> m_standardMeasureStatus;
  52. protected String[] m_batchIDs;
  53. protected Map<String, String> m_batchNames;
  54. protected Map<String, Date> m_batchDates;
  55. protected Map m_batchDescriptions;
  56. protected Map m_batchAttributes;
  57. protected Map<String, TreeMap<Integer, Double>> m_batchSpectrums;
  58. protected Map<String, String> m_batchApertures;
  59. protected Map m_batchLights;
  60. protected Map<String, String> m_batchSpeculars;
  61. protected Map<String, String> m_batchMeasureStatus;
  62. Connection m_conn;
  63. public SimpleColorDBExtractor( Connection conn )
  64. {
  65. m_conn = conn;
  66. }
  67. public String[] getBatchIDs()
  68. throws ImportException
  69. {
  70. if( m_batchIDs != null )
  71. {
  72. return m_batchIDs;
  73. }
  74. Statement st = null;
  75. try
  76. {
  77. st = m_conn.createStatement();
  78. String query =
  79. " SELECT TrialID FROM \"Trial Configuration Parameters\"";
  80. ResultSet rs = st.executeQuery( query );
  81. List<String> names = new ArrayList<String>();
  82. while( rs.next() )
  83. {
  84. names.add( rs.getString( 1 ) );
  85. }
  86. m_batchIDs = names.toArray( new String[ names.size() ] );
  87. }
  88. catch( SQLException e )
  89. {
  90. throw new ImportException( "", e );
  91. }
  92. finally
  93. {
  94. closeStatement( st );
  95. }
  96. return m_batchIDs;
  97. }
  98. public String getBatchName( String id )
  99. throws ImportException
  100. {
  101. if( m_batchNames == null )
  102. {
  103. m_batchNames = new HashMap<String, String>();
  104. }
  105. String batchName;
  106. if( ( batchName = m_batchNames.get( id ) ) != null )
  107. {
  108. return batchName;
  109. }
  110. PreparedStatement ps = null;
  111. try
  112. {
  113. String query =
  114. " SELECT NAME FROM \"Trial Configuration Parameters\""
  115. + " WHERE SpectralOrColorimetricDataId = ?";
  116. ps = m_conn.prepareStatement( query );
  117. ps.setString( 1, id );
  118. ResultSet rs = ps.executeQuery();
  119. if( rs.next() )
  120. {
  121. batchName = rs.getString( 1 );
  122. }
  123. }
  124. catch( SQLException e )
  125. {
  126. throw new ImportException( "", e );
  127. }
  128. finally
  129. {
  130. closeStatement( ps );
  131. }
  132. m_batchNames.put( id, batchName );
  133. return batchName;
  134. }
  135. public String[] getStandardIDs()
  136. throws ImportException
  137. {
  138. if( m_standardIDs != null )
  139. {
  140. return m_standardIDs;
  141. }
  142. Statement st = null;
  143. try
  144. {
  145. st = m_conn.createStatement();
  146. String query =
  147. " SELECT StandardID FROM \"Standard Configuration Parameters\"";
  148. ResultSet rs = st.executeQuery( query );
  149. List<String> names = new ArrayList<String>();
  150. while( rs.next() )
  151. {
  152. names.add( rs.getString( 1 ) );
  153. }
  154. m_standardIDs = names.toArray( new String[ names.size() ] );
  155. }
  156. catch( SQLException e )
  157. {
  158. throw new ImportException( "", e );
  159. }
  160. finally
  161. {
  162. closeStatement( st );
  163. }
  164. return m_standardIDs;
  165. }
  166. public String getStandardName( String id )
  167. throws ImportException
  168. {
  169. if( m_standardNames == null )
  170. {
  171. m_standardNames = new HashMap<String, String>();
  172. }
  173. String stdName;
  174. if( ( stdName = m_standardNames.get( id ) ) != null )
  175. {
  176. return stdName;
  177. }
  178. PreparedStatement ps = null;
  179. try
  180. {
  181. String query =
  182. " SELECT NAME FROM \"Standard Configuration Parameters\""
  183. + " WHERE SpectralOrColorimetricDataId = ?";
  184. ps = m_conn.prepareStatement( query );
  185. ps.setString( 1, id );
  186. ResultSet rs = ps.executeQuery();
  187. if( rs.next() )
  188. {
  189. stdName = rs.getString( 1 );
  190. }
  191. }
  192. catch( SQLException e )
  193. {
  194. throw new ImportException( "", e );
  195. }
  196. finally
  197. {
  198. closeStatement( ps );
  199. }
  200. m_standardNames.put( id, stdName );
  201. return stdName;
  202. }
  203. public TreeMap getBatchSpectrum( String id )
  204. throws ImportException
  205. {
  206. if( m_batchSpectrums == null )
  207. {
  208. m_batchSpectrums = new HashMap<String, TreeMap<Integer, Double>>();
  209. }
  210. TreeMap<Integer, Double> batSpec;
  211. if( ( batSpec = m_batchSpectrums.get( id ) ) != null )
  212. {
  213. return batSpec;
  214. }
  215. PreparedStatement ps = null;
  216. try
  217. {
  218. String query =
  219. " SELECT A.* FROM \"Spectral Data\" A, \"Trial Configuration Parameters\" B"
  220. + " WHERE B.TrialID = ?"
  221. + " AND A.\"Spectral Data ID\" = B.SpectralOrColorimetricDataId";
  222. ps = m_conn.prepareStatement( query );
  223. ps.setString( 1, id );
  224. ResultSet rs = ps.executeQuery();
  225. if( rs.next() )
  226. {
  227. batSpec = new TreeMap<Integer, Double>();
  228. for( int i = 0; i < SPECTRAL_COUNT; i++ )
  229. {
  230. int wavelength = LOW + ( i * INTERVAL );
  231. double reading = rs.getDouble( SPECTRAL_COLUMN.format( i + 1 ) );
  232. batSpec.put( wavelength, reading );
  233. }
  234. }
  235. }
  236. catch( SQLException e )
  237. {
  238. throw new ImportException( "", e );
  239. }
  240. finally
  241. {
  242. closeStatement( ps );
  243. }
  244. m_batchSpectrums.put( id, batSpec );
  245. return batSpec;
  246. }
  247. public TreeMap getStandardSpectrum( String id )
  248. throws ImportException
  249. {
  250. if( m_standardSpectrums == null )
  251. {
  252. m_standardSpectrums = new HashMap<String, TreeMap<Integer, Object>>();
  253. }
  254. TreeMap<Integer, Object> stdSpec = null;
  255. if( ( stdSpec = m_standardSpectrums.get( id ) ) != null )
  256. {
  257. return stdSpec;
  258. }
  259. PreparedStatement ps = null;
  260. try
  261. {
  262. String query =
  263. " SELECT A.* FROM \"Spectral Data\" A, \"Standard Configuration Parameters\" B"
  264. + " WHERE B.StandardID = ?"
  265. + " AND A.\"Spectral Data ID\" = B.SpectralOrColorimetricDataId";
  266. ps = m_conn.prepareStatement( query );
  267. ps.setString( 1, id );
  268. ResultSet rs = ps.executeQuery();
  269. if( rs.next() )
  270. {
  271. stdSpec = new TreeMap<Integer, Object>();
  272. for( int i = 0; i < SPECTRAL_COUNT; i++ )
  273. {
  274. int wavelength = LOW + ( i * INTERVAL );
  275. double reading = rs.getDouble( SPECTRAL_COLUMN.format( i + 1 ) );
  276. stdSpec.put(wavelength,reading );
  277. }
  278. }
  279. }
  280. catch( SQLException e )
  281. {
  282. throw new ImportException( "", e );
  283. }
  284. finally
  285. {
  286. closeStatement( ps );
  287. }
  288. m_standardSpectrums.put( id, stdSpec );
  289. return stdSpec;
  290. }
  291. protected void closeStatement( Statement st )
  292. throws ImportException
  293. {
  294. try
  295. {
  296. if( st != null )
  297. {
  298. st.close();
  299. }
  300. }
  301. catch( SQLException e )
  302. {
  303. }
  304. }
  305. public String getBatchDescription( String name )
  306. throws ImportException
  307. {
  308. return null;
  309. }
  310. public String getStandardDescription( String name )
  311. throws ImportException
  312. {
  313. return null;
  314. }
  315. public Date getBatchCreationDate( String id )
  316. throws ImportException
  317. {
  318. if( m_batchDates == null )
  319. {
  320. m_batchDates = new HashMap<String, Date>();
  321. }
  322. Date batdate;
  323. if( ( batdate = m_batchDates.get( id ) ) != null )
  324. {
  325. return batdate;
  326. }
  327. PreparedStatement ps = null;
  328. try
  329. {
  330. String query =
  331. " SELECT A.Spec_DateMeasTaken FROM \"Spectral Data\" A, \"Trial Configuration Parameters\" B"
  332. + " WHERE B.TrialID = ?"
  333. + " AND A.\"Spectral Data ID\"=B.SpectralOrColorimetricDataId";
  334. ps = m_conn.prepareStatement( query );
  335. ps.setString( 1, id );
  336. ResultSet rs = ps.executeQuery();
  337. if( rs.next() )
  338. {
  339. batdate = rs.getTimestamp( 1 );
  340. }
  341. }
  342. catch( SQLException e )
  343. {
  344. throw new ImportException( "", e );
  345. }
  346. finally
  347. {
  348. closeStatement( ps );
  349. }
  350. m_batchDates.put( id, batdate );
  351. return batdate;
  352. }
  353. public Date getStandardCreationDate( String id )
  354. throws ImportException
  355. {
  356. if( m_standardDates == null )
  357. {
  358. m_standardDates = new HashMap<String, Date>();
  359. }
  360. Date stddate;
  361. if( ( stddate = m_standardDates.get( id ) ) != null )
  362. {
  363. return stddate;
  364. }
  365. PreparedStatement ps = null;
  366. try
  367. {
  368. String query =
  369. " SELECT A.Spec_DateMeasTaken FROM \"Spectral Data\" A, \"Standard Configuration Parameters\" B"
  370. + " WHERE B.StandardID = ?"
  371. + " AND A.\"Spectral Data ID\"=B.SpectralOrColorimetricDataId";
  372. ps = m_conn.prepareStatement( query );
  373. ps.setString( 1, id );
  374. ResultSet rs = ps.executeQuery();
  375. if( rs.next() )
  376. {
  377. stddate = rs.getTimestamp( 1 );
  378. }
  379. }
  380. catch( SQLException e )
  381. {
  382. throw new ImportException( "", e );
  383. }
  384. finally
  385. {
  386. closeStatement( ps );
  387. }
  388. m_standardDates.put( id, stddate );
  389. return stddate;
  390. }
  391. public String getBatchAperture( String id )
  392. throws ImportException
  393. {
  394. if( m_batchApertures == null )
  395. {
  396. m_batchApertures = new HashMap<String, String>();
  397. }
  398. String bataper;
  399. if( ( bataper = m_batchApertures.get( id ) ) != null )
  400. {
  401. return bataper;
  402. }
  403. bataper = getBatchMeasuredStatus( id );
  404. m_batchApertures.put( id, bataper );
  405. return extractAperture( bataper );
  406. }
  407. public Map getBatchAttributes( String name )
  408. throws ImportException
  409. {
  410. return new HashMap();
  411. }
  412. public String getBatchLightFilter( String name )
  413. throws ImportException
  414. {
  415. return "Inc";
  416. }
  417. public String getBatchSpecular( String id )
  418. throws ImportException
  419. {
  420. if( m_batchSpeculars == null )
  421. {
  422. m_batchSpeculars = new HashMap<String, String>();
  423. }
  424. String batspec;
  425. if( ( batspec = m_batchSpeculars.get( id ) ) != null )
  426. {
  427. return batspec;
  428. }
  429. batspec = getBatchMeasuredStatus( id );
  430. m_batchSpeculars.put( id, batspec );
  431. return extractSpecular( batspec );
  432. }
  433. public String getStandardAperture( String id )
  434. throws ImportException
  435. {
  436. if( m_standardApertures == null )
  437. {
  438. m_standardApertures = new HashMap<String, String>();
  439. }
  440. String stdaper;
  441. if( ( stdaper = m_standardApertures.get( id ) ) != null )
  442. {
  443. return stdaper;
  444. }
  445. stdaper = getBatchMeasuredStatus( id );
  446. m_standardApertures.put( id, stdaper );
  447. return extractAperture( stdaper );
  448. }
  449. public Map getStandardAttributes( String name )
  450. throws ImportException
  451. {
  452. return new HashMap();
  453. }
  454. public String getStandardLightFilter( String id )
  455. throws ImportException
  456. {
  457. if( m_standardLights == null )
  458. {
  459. m_standardLights = new HashMap<String, String>();
  460. }
  461. String stdlite;
  462. if( ( stdlite = m_standardApertures.get( id ) ) != null )
  463. {
  464. return stdlite;
  465. }
  466. stdlite = getBatchMeasuredStatus( id );
  467. m_standardLights.put( id, stdlite );
  468. return extractLightFilter( stdlite );
  469. }
  470. public String getStandardSpecular( String name )
  471. throws ImportException
  472. {
  473. if( m_standardSpeculars == null )
  474. {
  475. m_standardSpeculars = new HashMap<String, String>();
  476. }
  477. String stdspec;
  478. if( ( stdspec = m_standardSpeculars.get( name ) ) != null )
  479. {
  480. return stdspec;
  481. }
  482. stdspec = getStandardMeasuredStatus( name );
  483. m_standardSpeculars.put( name, stdspec );
  484. return extractSpecular( stdspec );
  485. }
  486. protected String getStandardMeasuredStatus( String id )
  487. throws ImportException
  488. {
  489. if( m_standardMeasureStatus == null )
  490. {
  491. m_standardMeasureStatus = new HashMap<String, String>();
  492. }
  493. String stdmeasurestatus;
  494. if( ( stdmeasurestatus = m_standardMeasureStatus.get( id ) ) != null )
  495. {
  496. return stdmeasurestatus;
  497. }
  498. PreparedStatement ps = null;
  499. try
  500. {
  501. String query =
  502. " SELECT A.Spec_MeasuredStatus FROM \"Spectral Data\" A, \"Standard Configuration Parameters\" B"
  503. + " WHERE B.StandardID = ?"
  504. + " AND A.\"Spectral Data ID\"=B.SpectralOrColorimetricDataId";
  505. ps = m_conn.prepareStatement( query );
  506. ps.setString( 1, id );
  507. ResultSet rs = ps.executeQuery();
  508. if( rs.next() )
  509. {
  510. stdmeasurestatus = rs.getString( 1 );
  511. }
  512. }
  513. catch( SQLException e )
  514. {
  515. throw new ImportException( "", e );
  516. }
  517. finally
  518. {
  519. closeStatement( ps );
  520. }
  521. m_standardMeasureStatus.put( id, stdmeasurestatus );
  522. return stdmeasurestatus;
  523. }
  524. protected String getBatchMeasuredStatus( String id )
  525. throws ImportException
  526. {
  527. if( m_batchMeasureStatus == null )
  528. {
  529. m_batchMeasureStatus = new HashMap<String, String>();
  530. }
  531. String batmeasurestatus;
  532. if( ( batmeasurestatus = m_batchMeasureStatus.get( id ) ) != null )
  533. {
  534. return batmeasurestatus;
  535. }
  536. PreparedStatement ps = null;
  537. try
  538. {
  539. String query =
  540. " SELECT A.Spec_MeasuredStatus FROM \"Spectral Data\" A, \"Trial Configuration Parameters\" B"
  541. + " WHERE B.TrialID = ?"
  542. + " AND A.\"Spectral Data ID\"=B.SpectralOrColorimetricDataId";
  543. ps = m_conn.prepareStatement( query );
  544. ps.setString( 1, id );
  545. ResultSet rs = ps.executeQuery();
  546. if( rs.next() )
  547. {
  548. batmeasurestatus = rs.getString( 1 );
  549. }
  550. }
  551. catch( SQLException e )
  552. {
  553. throw new ImportException( "", e );
  554. }
  555. finally
  556. {
  557. closeStatement( ps );
  558. }
  559. m_batchMeasureStatus.put( id, batmeasurestatus );
  560. return batmeasurestatus;
  561. }
  562. protected String extractLightFilter( String str )
  563. {
  564. if( str == null )
  565. {
  566. return null;
  567. }
  568. char c = str.charAt( 3 );
  569. if( c == 'O' )
  570. {
  571. return "UVE";
  572. }
  573. else if( c == 'P' )
  574. {
  575. return "UVI";
  576. }
  577. return null;
  578. }
  579. protected String extractSpecular( String str )
  580. {
  581. if( str == null )
  582. {
  583. return null;
  584. }
  585. char c = str.charAt( 2 );
  586. if( c == 'I' )
  587. {
  588. return "SCI";
  589. }
  590. else if( c == 'E' )
  591. {
  592. return "SCE";
  593. }
  594. return null;
  595. }
  596. protected String extractAperture( String str )
  597. {
  598. if( str == null )
  599. {
  600. return null;
  601. }
  602. char c = str.charAt( 5 );
  603. if( c == 'S' )
  604. {
  605. return "SAV";
  606. }
  607. else if( c == 'L' )
  608. {
  609. return "LAV";
  610. }
  611. return null;
  612. }
  613. public void refresh()
  614. {
  615. m_standardNames = null;
  616. m_standardDates = null;
  617. m_standardDescriptions = null;
  618. m_standardAttributes = null;
  619. m_standardSpectrums = null;
  620. m_standardApertures = null;
  621. m_standardLights = null;
  622. m_standardSpeculars = null;
  623. m_standardMeasureStatus = null;
  624. m_batchNames = null;
  625. m_batchDates = null;
  626. m_batchDescriptions = null;
  627. m_batchAttributes = null;
  628. m_batchSpectrums = null;
  629. m_batchApertures = null;
  630. m_batchLights = null;
  631. m_batchSpeculars = null;
  632. m_batchMeasureStatus = null;
  633. }
  634. }