3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.scripts
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
20 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
21 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
22 import eu
.etaxonomy
.cdm
.common
.AccountStore
;
23 import eu
.etaxonomy
.cdm
.database
.CdmDataSource
;
24 import eu
.etaxonomy
.cdm
.database
.DataSourceNotFoundException
;
25 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
26 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
27 import eu
.etaxonomy
.cdm
.model
.common
.init
.TermNotFoundException
;
28 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
29 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
31 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
33 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
36 * We decided by convention to have only ONE TextData element in an image gallery and that
37 * all media objects should be attached to that element.
39 * Run this script to clean-up a database for which this convention was not satisfied.
42 * @created Jun 9, 2010
45 public class FixMultipleTextDataInImageGalleries
{
46 public static final Logger logger
= Logger
47 .getLogger(FixMultipleTextDataInImageGalleries
.class);
49 public static ICdmDataSource
dataSource(){
50 DatabaseTypeEnum dbType
= DatabaseTypeEnum
.MySQL
;
51 String cdmServer
= "<IP adress>";
52 String cdmDB
= "<database name>";
53 String cdmUserName
= "<user>";
54 return makeDestination(dbType
, cdmServer
, cdmDB
, -1, cdmUserName
, null);
57 private static ICdmDataSource
makeDestination(DatabaseTypeEnum dbType
, String cdmServer
, String cdmDB
, int port
, String cdmUserName
, String pwd
){
58 //establish connection
59 pwd
= AccountStore
.readOrStorePassword(cdmServer
, cdmDB
, cdmUserName
, pwd
);
60 ICdmDataSource destination
;
61 if(dbType
.equals(DatabaseTypeEnum
.MySQL
)){
62 destination
= CdmDataSource
.NewMySqlInstance(cdmServer
, cdmDB
, port
, cdmUserName
, pwd
, null);
63 } else if(dbType
.equals(DatabaseTypeEnum
.PostgreSQL
)){
64 destination
= CdmDataSource
.NewPostgreSQLInstance(cdmServer
, cdmDB
, port
, cdmUserName
, pwd
, null);
67 throw new RuntimeException("Unsupported DatabaseType");
74 * @throws TermNotFoundException
75 * @throws DataSourceNotFoundException
77 public static void main(String
[] args
) throws DataSourceNotFoundException
, TermNotFoundException
{
79 CdmApplicationController applicationController
= CdmApplicationController
.NewInstance(dataSource());
81 ConversationHolder conversation
= applicationController
.NewConversation();
82 conversation
.startTransaction();
84 IDescriptionService service
= applicationController
.getDescriptionService();
86 // get all taxon descriptions
87 List
<DescriptionBase
> result
= service
.list(TaxonDescription
.class, null, null, null, null);
89 int countTaxonDescriptions
= 0;
91 for (DescriptionBase description
: result
){
92 // filter image galleries with more than one element
93 if(description
.isImageGallery() && description
.getElements().size() > 1){
94 countTaxonDescriptions
++;
96 logger
.warn("Found image gallery with mulitple TextData: " + description
.getElements().size());
97 TaxonDescription taxonDescription
= (TaxonDescription
) description
;
99 TextData newDescriptionElement
= TextData
.NewInstance(Feature
.IMAGE());
101 Set
<DescriptionElementBase
> elementsToRemove
= new HashSet
<DescriptionElementBase
>();
103 // consolidate media from all elements into a new element
104 for(DescriptionElementBase element
: taxonDescription
.getElements()){
105 List
<Media
> medias
= element
.getMedia();
107 for(Media media
: medias
){
108 newDescriptionElement
.addMedia(media
);
109 logger
.warn("Consolidating media");
111 elementsToRemove
.add(element
);
114 // remove old elements
115 for(DescriptionElementBase element
: elementsToRemove
){
116 taxonDescription
.removeElement(element
);
119 // add the new element
120 taxonDescription
.addElement(newDescriptionElement
);
124 conversation
.commit(false);
126 logger
.warn("Descriptions Processed: " + countTaxonDescriptions
);