major changes in imports and exports
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / ExportConfiguratorBase.java
1 /**
2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 */
6
7 package eu.etaxonomy.cdm.io.common;
8
9 import org.apache.log4j.Logger;
10
11 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
12 import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
13 import eu.etaxonomy.cdm.database.ICdmDataSource;
14 import eu.etaxonomy.cdm.io.common.IExportConfigurator.CHECK;
15 import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
16 import eu.etaxonomy.cdm.model.reference.Database;
17 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
18
19 /**
20 * @author a.babadshanjan
21 * @created 16.11.2008
22 */
23 public abstract class ExportConfiguratorBase<DESTINATION extends Object> extends IoConfiguratorBase {
24
25 private static final Logger logger = Logger.getLogger(ExportConfiguratorBase.class);
26
27 private CHECK check = CHECK.EXPORT_WITHOUT_CHECK;
28
29 private ICdmDataSource source;
30 private DESTINATION destination;
31 //private DbSchemaValidation dbSchemaValidation = DbSchemaValidation.VALIDATE;
32 private CdmApplicationController cdmApp = null;
33 protected ReferenceBase sourceReference;
34 protected Class<ICdmIO>[] ioClassList;
35
36
37 public ExportConfiguratorBase(){
38 super();
39 //setDbSchemaValidation(DbSchemaValidation.UPDATE);
40 makeIoClassList();
41 }
42
43 abstract protected void makeIoClassList();
44
45 public ICdmDataSource getSource() {
46 return source;
47 }
48
49 public void setSource(ICdmDataSource source) {
50 this.source = source;
51 }
52
53 /**
54 * @param source the source to get
55 */
56 public DESTINATION getDestination() {
57 return destination;
58 }
59
60 /**
61 * @param source the source to set
62 */
63 public void setDestination(DESTINATION destination) {
64 this.destination = destination;
65 }
66
67 /* (non-Javadoc)
68 * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#getSourceReference()
69 */
70 // @Override
71 public ReferenceBase getSourceReference() {
72 //TODO //needed
73 if (this.sourceReference == null){
74 sourceReference = Database.NewInstance();
75 if (getSource() != null){
76 sourceReference.setTitleCache(getSource().getDatabase());
77 }
78 }
79 return sourceReference;
80 }
81
82 public Class<ICdmIO>[] getIoClassList(){
83 return ioClassList;
84 }
85
86 /* (non-Javadoc)
87 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getCheck()
88 */
89 public CHECK getCheck() {
90 return this.check;
91 }
92
93 /* (non-Javadoc)
94 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setCheck(eu.etaxonomy.cdm.io.tcsrdf.TcsRdfImportConfigurator.CHECK)
95 */
96 public void setCheck(CHECK check) {
97 this.check = check;
98 }
99
100 /* (non-Javadoc)
101 * @see eu.etaxonomy.cdm.io.tcsrdf.IExportConfigurator#getDbSchemaValidation()
102 */
103 // public DbSchemaValidation getDbSchemaValidation() {
104 // return dbSchemaValidation;
105 // }
106
107 /**
108 * Returns a <code>CdmApplicationController</code> created by the values of this configuration.
109 * If a controller was already created before the last created controller is returned.
110 * @return
111 */
112 public CdmApplicationController getCdmAppController(){
113 return getCdmAppController(false);
114 }
115
116 /**
117 * Returns a new instance of <code>CdmApplicationController</code> created by the values of this configuration.
118 * @return
119 */
120 public CdmApplicationController getNewCdmAppController(){
121 return getCdmAppController(true, false);
122 }
123
124 /**
125 * Returns a <code>CdmApplicationController</code> created by the values of this configuration.
126 * If create new is true always a new controller is returned, else the last created controller is returned. If no controller has
127 * been created before a new controller is returned.
128 * @return
129 */
130 public CdmApplicationController getCdmAppController(boolean createNew){
131 return getCdmAppController(createNew, false);
132 }
133
134
135 /**
136 * Returns a <code>CdmApplicationController</code> created by the values of this configuration.
137 * If create new is true always a new controller is returned, else the last created controller is returned. If no controller has
138 * been created before a new controller is returned.
139 * @return
140 */
141 public CdmApplicationController getCdmAppController(boolean createNew, boolean omitTermLoading){
142 if (cdmApp == null || createNew == true){
143 try {
144 cdmApp = CdmApplicationController.NewInstance(this.getSource(), this.getDbSchemaValidation(), omitTermLoading);
145 } catch (DataSourceNotFoundException e) {
146 logger.error("could not connect to destination database");
147 return null;
148 }catch (TermNotFoundException e) {
149 logger.error("could not find needed term in destination datasource");
150 return null;
151 }
152 }
153 return cdmApp;
154 }
155
156
157 /* (non-Javadoc)
158 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#isValid()
159 */
160 public boolean isValid(){
161 boolean result = true;
162 if (source == null){
163 logger.warn("Connection to CDM could not be established");
164 result = false;
165 }
166 if (destination == null){
167 logger.warn("Invalid export destination");
168 result = false;
169 }
170
171 return result;
172 }
173
174 public String getSourceNameString() {
175 if (this.getSource() == null) {
176 return null;
177 } else {
178 return (String)this.getSource().getName();
179 }
180 }
181
182 }