88import org .slf4j .LoggerFactory ;
99
1010import ca .bc .gov .farms .service .api .v1 .code .CodeService ;
11+ import ca .bc .gov .farms .service .api .v1 .model .factory .CodeFactory ;
1112import ca .bc .gov .nrs .wfone .common .model .Code ;
1213import ca .bc .gov .nrs .wfone .common .model .CodeHierarchy ;
1314import ca .bc .gov .nrs .wfone .common .model .CodeHierarchyList ;
@@ -45,6 +46,8 @@ public class CodeServiceImpl implements CodeService {
4546
4647 private CodeTableFactory codeTableFactory ;
4748
49+ private CodeFactory codeFactory ;
50+
4851 private CodeHierarchyFactory codeHierarchyFactory ;
4952
5053 private CodeValidator codeValidator ;
@@ -522,6 +525,10 @@ public void setCodeTableFactory(CodeTableFactory codeTableFactory) {
522525 this .codeTableFactory = codeTableFactory ;
523526 }
524527
528+ public void setCodeFactory (CodeFactory codeFactory ) {
529+ this .codeFactory = codeFactory ;
530+ }
531+
525532 public void setCodeHierarchyFactory (CodeHierarchyFactory codeHierarchyFactory ) {
526533 this .codeHierarchyFactory = codeHierarchyFactory ;
527534 }
@@ -542,7 +549,64 @@ public void setCodeHierarchyConfigs(
542549 @ Override
543550 public Code getCode (String codeTableName , String codeName , FactoryContext factoryContext )
544551 throws ServiceException , NotFoundException {
545- // TODO Auto-generated method stub
546- throw new UnsupportedOperationException ("Unimplemented method 'getCode'" );
552+ logger .debug ("<getCode" );
553+
554+ Code result = null ;
555+
556+ if (codeTableConfigs == null || codeTableConfigs .isEmpty ()) {
557+ logger .warn ("No codeTables have been configured." );
558+ }
559+
560+ try {
561+
562+ CodeTableConfig codeTableConfig = null ;
563+ for (CodeTableConfig tmp : this .codeTableConfigs ) {
564+ if (tmp .getCodeTableName ().equalsIgnoreCase (codeTableName )) {
565+ codeTableConfig = tmp ;
566+ break ;
567+ }
568+ }
569+
570+ if (codeTableConfig == null ) {
571+ throw new NotFoundException ("Did not find the CodeTable: " + codeTableName );
572+ }
573+
574+ CodeTableDao dao = codeTableConfig .getCodeTableDao ();
575+
576+ if (dao == null ) {
577+ dao = this .codeTableDao ;
578+ }
579+
580+ if (dao == null ) {
581+ logger .error ("No codeTableDao has been configured for " + codeTableConfig .getCodeTableName () + "." );
582+ }
583+
584+ CodeTableDto codeTableDto = dao .fetch (codeTableConfig , null );
585+
586+ if (codeTableDto == null ) {
587+ throw new NotFoundException ("Did not find the CodeTable: " + codeTableName );
588+ }
589+
590+ CodeDto codeDto = null ;
591+ for (CodeDto tmp : codeTableDto .getCodes ()) {
592+ if (tmp .getCode ().equalsIgnoreCase (codeName )) {
593+ codeDto = tmp ;
594+ break ;
595+ }
596+ }
597+
598+ if (codeDto == null ) {
599+ throw new NotFoundException ("Did not find the Code: " + codeName );
600+ }
601+
602+ result = this .codeFactory .getCode (codeTableDto , codeDto , factoryContext );
603+
604+ } catch (DaoException e ) {
605+
606+ throw new ServiceException ("DAO threw an exception" , e );
607+ }
608+
609+ logger .debug (">getCode " + result .getCode ());
610+ return result ;
547611 }
548612}
0 commit comments