1
2
3
4
5
6
7
8
9
10 package eu.etaxonomy.cdm.io.tcsxml.in;
11
12 import java.util.List;
13
14 import org.apache.log4j.Logger;
15 import org.jdom.Element;
16 import org.jdom.Namespace;
17 import org.springframework.stereotype.Component;
18
19 import eu.etaxonomy.cdm.common.DoubleResult;
20 import eu.etaxonomy.cdm.common.XmlHelp;
21 import eu.etaxonomy.cdm.io.common.ICdmIO;
22 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
23 import eu.etaxonomy.cdm.io.common.MapWrapper;
24 import eu.etaxonomy.cdm.model.agent.Institution;
25 import eu.etaxonomy.cdm.model.occurrence.Collection;
26 import eu.etaxonomy.cdm.model.occurrence.Specimen;
27
28
29
30
31
32 @Component
33 public class TcsXmlSpecimensImport extends TcsXmlImportBase implements ICdmIO<TcsXmlImportState> {
34 private static final Logger logger = Logger.getLogger(TcsXmlSpecimensImport.class);
35
36 private static int modCount = 1000;
37
38 public TcsXmlSpecimensImport(){
39 super();
40 }
41
42 @Override
43 public boolean doCheck(TcsXmlImportState config){
44 boolean result = true;
45
46
47
48 return result;
49 }
50
51 private static boolean checkXXX(IImportConfigurator tcsConfig){
52 try {
53 boolean result = true;
54
55
56
57
58 return result;
59 } catch (Exception e) {
60 e.printStackTrace();
61 return false;
62 }
63 }
64
65
66 @Override
67 public boolean doInvoke(TcsXmlImportState state){
68 logger.info("start make Specimens ...");
69
70 MapWrapper<Specimen> specimenMap = (MapWrapper<Specimen>)state.getStore(ICdmIO.SPECIMEN_STORE);
71
72 boolean success = true;
73 String childName;
74 boolean obligatory;
75
76 TcsXmlImportConfigurator config = state.getConfig();
77 Element elDataSet = getDataSetElement(config);
78 Namespace tcsNamespace = config.getTcsXmlNamespace();
79
80 DoubleResult<Element, Boolean> doubleResult;
81 childName = "Specimens";
82 obligatory = false;
83 doubleResult = XmlHelp.getSingleChildElement(elDataSet, childName, tcsNamespace, obligatory);
84 success &= doubleResult.getSecondResult();
85 Element elSpecimens = doubleResult.getFirstResult();
86
87 String tcsElementName = "Specimen";
88 List<Element> elSpecimenList = elSpecimens.getChildren(tcsElementName, tcsNamespace);
89
90
91 int i = 0;
92
93 for (Element elSpecimen : elSpecimenList){
94
95 if ((++i % modCount) == 0){ logger.info("specimen handled: " + (i-1));}
96
97
98 String strId = elSpecimen.getAttributeValue("id");
99
100 childName = "Simple";
101 obligatory = true;
102 doubleResult = XmlHelp.getSingleChildElement(elSpecimen, childName, tcsNamespace, obligatory);
103 success &= doubleResult.getSecondResult();
104 Element elSimple = doubleResult.getFirstResult();
105
106 String simple = elSimple.getTextNormalize();
107 Specimen specimen = Specimen.NewInstance();
108 specimen.setTitleCache(simple, true);
109
110
111 childName = "Collection";
112 obligatory = false;
113 doubleResult = XmlHelp.getSingleChildElement(elSpecimen, childName, tcsNamespace, obligatory);
114 success &= doubleResult.getSecondResult();
115 Element elCollection = doubleResult.getFirstResult();
116 success &= makeCollection(specimen, elCollection);
117 Collection collection = specimen.getCollection();
118
119 childName = "Institution";
120 obligatory = false;
121 doubleResult = XmlHelp.getSingleChildElement(elSpecimen, childName, tcsNamespace, obligatory);
122 success &= doubleResult.getSecondResult();
123 Element elInstitution = doubleResult.getFirstResult();
124 success &= makeInstitution(specimen, elInstitution);
125
126 childName = "SpecimenItem";
127 obligatory = true;
128 doubleResult = XmlHelp.getSingleChildElement(elSpecimen, childName, tcsNamespace, obligatory);
129 success &= doubleResult.getSecondResult();
130 Element elSpecimenItem = doubleResult.getFirstResult();
131 makeSpecimenItem(specimen, elSpecimenItem);
132
133 specimenMap.put(strId, specimen);
134 }
135
136 logger.info("Save specimen (" + i +")");
137 getOccurrenceService().save((java.util.Collection)specimenMap.objects());
138
139 logger.info("end make Specimens ...");
140 return success;
141
142 }
143
144 private boolean makeInstitution(Specimen specimen, Element elInstitution){
145 boolean success = true;
146 Institution institution = null;
147 if (specimen == null){
148 logger.warn("No specimen defined");
149 return false;
150 }
151 if (elInstitution != null){
152 Namespace ns = elInstitution.getNamespace();
153 institution = Institution.NewInstance();
154
155 String childName = "InstitutionName";
156 boolean obligatory = false;
157 DoubleResult<Element, Boolean> doubleResult = XmlHelp.getSingleChildElement(elInstitution, childName, ns, obligatory);
158 success &= doubleResult.getSecondResult();
159 Element elInstitutionName = doubleResult.getFirstResult();
160 if(elInstitutionName != null){
161 String institutionName = elInstitutionName.getTextNormalize();
162 institution.setName(institutionName);
163
164 childName = "Code";
165 obligatory = true;
166 doubleResult = XmlHelp.getSingleChildElement(elInstitution, childName, ns, obligatory);
167 success &= doubleResult.getSecondResult();
168 Element elCode = doubleResult.getFirstResult();
169 String code = elCode.getTextNormalize();
170 institution.setName(code);
171
172 childName = "Address";
173 obligatory = true;
174 doubleResult = XmlHelp.getSingleChildElement(elInstitution, childName, ns, obligatory);
175 success &= doubleResult.getSecondResult();
176 Element elAddress = doubleResult.getFirstResult();
177 String address = elAddress.getTextNormalize();
178 institution.setName(address);
179
180 childName = "URL";
181 obligatory = true;
182 doubleResult = XmlHelp.getSingleChildElement(elInstitution, childName, ns, obligatory);
183 success &= doubleResult.getSecondResult();
184 Element elUrl = doubleResult.getFirstResult();
185 String url = elUrl.getTextNormalize();
186 institution.setName(url);
187
188 childName = "Phone";
189 obligatory = true;
190 doubleResult = XmlHelp.getSingleChildElement(elInstitution, childName, ns, obligatory);
191 success &= doubleResult.getSecondResult();
192 Element elPhone = doubleResult.getFirstResult();
193 String phone = elPhone.getTextNormalize();
194 institution.setName(phone);
195
196 childName = "Email";
197 obligatory = true;
198 doubleResult = XmlHelp.getSingleChildElement(elInstitution, childName, ns, obligatory);
199 success &= doubleResult.getSecondResult();
200 Element elEmail = doubleResult.getFirstResult();
201 String email = elEmail.getTextNormalize();
202 institution.setName(email);
203 }
204
205
206 }
207
208 Collection collection = specimen.getCollection();
209 if (collection == null){
210 collection = Collection.NewInstance();
211 specimen.setCollection(collection);
212 }
213
214 collection.setInstitute(institution);
215
216 return success;
217 }
218
219 private boolean makeCollection(Specimen specimen, Element elCollection){
220 boolean success = true;
221 Collection collection = null;
222 if (elCollection != null){
223 Namespace ns = elCollection.getNamespace();
224 collection = Collection.NewInstance();
225
226
227 specimen.setCollection(collection);
228 }
229 return success;
230 }
231
232 private boolean makeSpecimenItem(Specimen specimen, Element elSpecimenItem){
233 boolean success = true;
234 Namespace ns = elSpecimenItem.getNamespace();
235 if (specimen == null){
236 logger.warn("No specimen");
237 return false;
238 }else if (elSpecimenItem != null){
239
240
241
242 }
243 return success;
244 }
245
246
247
248
249
250
251
252 protected boolean isIgnore(TcsXmlImportState state){
253 TcsXmlImportConfigurator tcsConfig = state.getConfig();
254 return (! tcsConfig.isDoSpecimen());
255 }
256
257 }