4 package eu
.etaxonomy
.cdm
.io
.tcs
;
6 import static eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
.OBLIGATORY
;
7 import static eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
.OVERWRITE
;
9 import java
.util
.HashSet
;
10 import java
.util
.List
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.jdom
.Attribute
;
16 import org
.jdom
.Element
;
17 import org
.jdom
.Namespace
;
19 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
20 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
22 import eu
.etaxonomy
.cdm
.common
.XmlHelp
;
23 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
24 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
26 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.Article
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.Book
;
33 import eu
.etaxonomy
.cdm
.model
.reference
.BookSection
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
35 import eu
.etaxonomy
.cdm
.model
.reference
.Journal
;
36 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
37 import eu
.etaxonomy
.cdm
.model
.reference
.StrictReferenceBase
;
38 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
44 public class TcsReferenceIO
extends TcsIoBase
implements ICdmIO
{
45 private static final Logger logger
= Logger
.getLogger(TcsReferenceIO
.class);
47 private static int modCount
= 1000;
49 public TcsReferenceIO(){
54 public boolean doCheck(IImportConfigurator config
){
55 boolean result
= true;
56 result
&= checkArticlesWithoutJournal(config
);
57 //result &= checkPartOfJournal(config);
62 private static boolean checkArticlesWithoutJournal(IImportConfigurator bmiConfig
){
64 boolean result
= true;
66 // result = firstRow = false;
70 } catch (Exception e
) {
77 protected static CdmIoXmlMapperBase
[] standardMappers
= new CdmIoXmlMapperBase
[]{
78 //new CdmTextElementMapper("edition", "edition"),
79 new CdmTextElementMapper("volume", "volume"),
80 new CdmTextElementMapper("placePublished", "placePublished"),
81 new CdmTextElementMapper("publisher", "publisher"),
82 //new CdmTextElementMapper("isbn", "isbn"),
83 new CdmTextElementMapper("pages", "pages"),
84 //new CdmTextElementMapper("series", "series"),
85 //new CdmTextElementMapper("issn", "issn"),
86 //new CdmTextElementMapper("url", "uri")
89 protected static CdmIoXmlMapperBase
[] operationalMappers
= new CdmIoXmlMapperBase
[]{
90 new CdmUnclearMapper("year")
91 , new CdmUnclearMapper("title")
92 , new CdmUnclearMapper("shortTitle")
93 , new CdmUnclearMapper("publicationType")
94 , new CdmUnclearMapper("parentPublication")
95 , new CdmUnclearMapper("authorship")
99 // protected static String[] createdAndNotesAttributes = new String[]{
100 // "created_When", "updated_When", "created_Who", "updated_Who", "notes"
103 protected static CdmIoXmlMapperBase
[] unclearMappers
= new CdmIoXmlMapperBase
[]{
109 private boolean makeStandardMapper(Element parentElement
, StrictReferenceBase ref
, Set
<String
> omitAttributes
){
110 if (omitAttributes
== null){
111 omitAttributes
= new HashSet
<String
>();
113 boolean result
= true;
114 for (CdmIoXmlMapperBase mapper
: standardMappers
){
115 Object value
= getValue(mapper
, parentElement
);
116 //write to destination
118 String destinationAttribute
= mapper
.getDestinationAttribute();
119 if (! omitAttributes
.contains(destinationAttribute
)){
120 result
&= ImportHelper
.addValue(value
, ref
, destinationAttribute
, mapper
.getTypeClass(), OVERWRITE
, OBLIGATORY
);
127 private Object
getValue(CdmIoXmlMapperBase mapper
, Element parentElement
){
128 String sourceAttribute
= mapper
.getSourceAttribute().toLowerCase();
129 Namespace sourceNamespace
= mapper
.getSourceNamespace(parentElement
);
130 Element child
= parentElement
.getChild(sourceAttribute
, sourceNamespace
);
134 if (child
.getContentSize() > 1){
135 logger
.warn("Element is not String");
137 Object value
= child
.getTextTrim();
142 public boolean doInvoke(IImportConfigurator config
, CdmApplicationController cdmApp
,
143 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
){
145 MapWrapper
<ReferenceBase
> referenceMap
= (MapWrapper
<ReferenceBase
>)stores
.get(ICdmIO
.REFERENCE_STORE
);
146 MapWrapper
<ReferenceBase
> nomRefMap
= (MapWrapper
<ReferenceBase
>)stores
.get(ICdmIO
.NOMREF_STORE
);
147 MapWrapper
<TeamOrPersonBase
> authorMap
= (MapWrapper
<TeamOrPersonBase
>)stores
.get(ICdmIO
.AUTHOR_STORE
);
149 // MapWrapper<ReferenceBase> referenceStore= new MapWrapper<ReferenceBase>(null);
150 // MapWrapper<ReferenceBase> nomRefStore= new MapWrapper<ReferenceBase>(null);
152 TcsImportConfigurator tcsConfig
= (TcsImportConfigurator
)config
;
153 Element root
= tcsConfig
.getSourceRoot();
154 logger
.info("start makeReferences ...");
156 String tcsElementName
;
157 Namespace tcsNamespace
;
160 boolean success
= true;
161 IReferenceService referenceService
= cdmApp
.getReferenceService();
164 //Map<Integer, ReferenceBase> referenceCollectionMap = new HashMap<Integer, ReferenceBase>();
166 Namespace rdfNamespace
= root
.getNamespace();
167 String prefix
= "tcom";
168 Namespace commonNamespace
= root
.getNamespace(prefix
);
170 Namespace publicationNamespace
= root
.getNamespace(prefix
);
174 tcsElementName
= "PublicationCitation";
175 tcsNamespace
= publicationNamespace
;
176 List
<Element
> elPublicationCitations
= root
.getChildren(tcsElementName
, tcsNamespace
);
179 int biblioRefsCount
= 0;
183 for (Element elPublicationCitation
: elPublicationCitations
){
185 if ((++i
% modCount
) == 0){ logger
.info("references handled: " + (i
-1));}
187 //create TaxonName element
188 String strAbout
= elPublicationCitation
.getAttributeValue("about", rdfNamespace
);
190 tcsElementName
= "publicationType";
191 tcsNamespace
= publicationNamespace
;
192 String strPubType
= XmlHelp
.getChildAttributeValue(elPublicationCitation
, tcsElementName
, tcsNamespace
, "resource", rdfNamespace
);
195 StrictReferenceBase ref
= TcsTransformer
.pubTypeStr2PubType(strPubType
);
197 ref
= Generic
.NewInstance();
200 Set
<String
> omitAttributes
= null;
201 makeStandardMapper(elPublicationCitation
, ref
, omitAttributes
);
204 tcsElementName
= "authorship";
205 tcsNamespace
= publicationNamespace
;
206 String strAuthorship
= elPublicationCitation
.getChildText(tcsElementName
, tcsNamespace
);
208 TeamOrPersonBase authorTeam
= Team
.NewInstance();
209 authorTeam
.setTitleCache(strAuthorship
);
210 ref
.setAuthorTeam(authorTeam
);
212 tcsElementName
= "year";
213 tcsNamespace
= publicationNamespace
;
214 String strYear
= elPublicationCitation
.getChildText(tcsElementName
, tcsNamespace
);
215 TimePeriod datePublished
= ImportHelper
.getDatePublished(strYear
);
216 ref
.setDatePublished(datePublished
);
220 tcsElementName
= "parentPublication";
221 tcsNamespace
= publicationNamespace
;
222 String strParent
= XmlHelp
.getChildAttributeValue(elPublicationCitation
, tcsElementName
, tcsNamespace
, "resource", rdfNamespace
);
223 ReferenceBase parent
= referenceMap
.get(strParent
);
225 if ((ref
instanceof Article
) && (parent
instanceof Journal
)){
226 ((Article
)ref
).setInJournal((Journal
)parent
);
227 }else if ((ref
instanceof BookSection
) && (parent
instanceof Book
)){
228 ((BookSection
)ref
).setInBook((Book
)parent
);
230 logger
.warn("parent type (parent: " + parent
.getClass().getSimpleName() +", child("+strAbout
+"): " + ref
.getClass().getSimpleName() + ")not yet implemented");
231 //ref.setParent(parent);
237 //nomRef and reference
238 tcsElementName
= "shortTitle";
239 tcsNamespace
= publicationNamespace
;
240 boolean nomRefExists
= false;
241 String strShortTitle
= elPublicationCitation
.getChildText(tcsElementName
, tcsNamespace
);
242 if (! CdmUtils
.Nz(strShortTitle
).trim().equals("")){
243 ref
.setTitle(strShortTitle
);
244 nomRefMap
.put(strAbout
, ref
);
249 tcsElementName
= "title";
250 tcsNamespace
= publicationNamespace
;
251 String strTitle
= elPublicationCitation
.getChildText(tcsElementName
, tcsNamespace
);
252 tcsNamespace
= publicationNamespace
;
253 if (! CdmUtils
.Nz(strTitle
).trim().equals("") || nomRefExists
== false){
255 StrictReferenceBase biblioRef
= (StrictReferenceBase
)ref
.clone();
256 biblioRef
.setTitle(strTitle
);
257 referenceMap
.put(strAbout
, biblioRef
);
261 checkAdditionalContents(elPublicationCitation
, standardMappers
, operationalMappers
, unclearMappers
);
264 //ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
266 } catch (UnknownCdmTypeException e
) {
268 logger
.warn("Name with id " + strAbout
+ " has unknown type " + strPubType
+ " and could not be saved.");
273 //change conceptRef uuid
274 ReferenceBase sec
= referenceMap
.get(config
.getSourceSecId());
276 sec
= nomRefMap
.get(config
.getSourceSecId());
279 sec
.setUuid(config
.getSecUuid());
280 logger
.info("concept reference uuid changed to: " + config
.getSecUuid());
284 //save and store in map
285 logger
.info("Save nomenclatural references (" + nomRefCount
+ ")");
286 referenceService
.saveReferenceAll(nomRefMap
.objects());
287 logger
.info("Save bibliographical references (" + biblioRefsCount
+")");
288 referenceService
.saveReferenceAll(referenceMap
.objects());
290 //referenceService.saveReferenceAll(referenceMap.objects());
291 logger
.info("end makeReferences ...");
296 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
298 protected boolean isIgnore(IImportConfigurator config
){
299 return (config
.getDoReferences() == IImportConfigurator
.DO_REFERENCES
.NONE
);