Revision c0248cde
Added by Andreas Müller over 7 years ago
cdmlib-services/src/test/java/eu/etaxonomy/cdm/scripts/FixMultipleTextDataInImageGalleries.java | ||
---|---|---|
1 | 1 |
// $Id$ |
2 | 2 |
/** |
3 | 3 |
* Copyright (C) 2007 EDIT |
4 |
* European Distributed Institute of Taxonomy
|
|
4 |
* European Distributed Institute of Taxonomy |
|
5 | 5 |
* http://www.e-taxonomy.eu |
6 |
*
|
|
6 |
* |
|
7 | 7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
8 | 8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
9 | 9 |
*/ |
... | ... | |
34 | 34 |
/** |
35 | 35 |
* We decided by convention to have only ONE TextData element in an image gallery and that |
36 | 36 |
* all media objects should be attached to that element. |
37 |
*
|
|
37 |
* |
|
38 | 38 |
* Run this script to clean-up a database for which this convention was not satisfied. |
39 |
*
|
|
39 |
* |
|
40 | 40 |
* @author n.hoffmann |
41 | 41 |
* @created Jun 9, 2010 |
42 | 42 |
* @version 1.0 |
... | ... | |
51 | 51 |
String cdmUserName = "<user>"; |
52 | 52 |
return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null); |
53 | 53 |
} |
54 |
|
|
54 |
|
|
55 | 55 |
private static ICdmDataSource makeDestination(DatabaseTypeEnum dbType, String cdmServer, String cdmDB, int port, String cdmUserName, String pwd ){ |
56 | 56 |
//establish connection |
57 | 57 |
pwd = AccountStore.readOrStorePassword(cdmServer, cdmDB, cdmUserName, pwd); |
58 | 58 |
ICdmDataSource destination; |
59 | 59 |
if(dbType.equals(DatabaseTypeEnum.MySQL)){ |
60 |
destination = CdmDataSource.NewMySqlInstance(cdmServer, cdmDB, port, cdmUserName, pwd, null);
|
|
60 |
destination = CdmDataSource.NewMySqlInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
|
|
61 | 61 |
} else if(dbType.equals(DatabaseTypeEnum.PostgreSQL)){ |
62 |
destination = CdmDataSource.NewPostgreSQLInstance(cdmServer, cdmDB, port, cdmUserName, pwd, null);
|
|
62 |
destination = CdmDataSource.NewPostgreSQLInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
|
|
63 | 63 |
} else { |
64 | 64 |
//TODO others |
65 | 65 |
throw new RuntimeException("Unsupported DatabaseType"); |
66 | 66 |
} |
67 | 67 |
return destination; |
68 | 68 |
} |
69 |
|
|
69 |
|
|
70 | 70 |
/** |
71 | 71 |
* @param args |
72 |
* @throws TermNotFoundException
|
|
73 |
* @throws DataSourceNotFoundException
|
|
72 |
* @throws TermNotFoundException |
|
73 |
* @throws DataSourceNotFoundException |
|
74 | 74 |
*/ |
75 | 75 |
public static void main(String[] args) throws DataSourceNotFoundException, TermNotFoundException { |
76 |
|
|
76 |
|
|
77 | 77 |
CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource()); |
78 |
|
|
78 |
|
|
79 | 79 |
ConversationHolder conversation = applicationController.NewConversation(); |
80 | 80 |
conversation.startTransaction(); |
81 |
|
|
81 |
|
|
82 | 82 |
IDescriptionService service = applicationController.getDescriptionService(); |
83 |
|
|
83 |
|
|
84 | 84 |
// get all taxon descriptions |
85 | 85 |
List<TaxonDescription> result = service.list(TaxonDescription.class, null, null, null, null); |
86 |
|
|
86 |
|
|
87 | 87 |
int countTaxonDescriptions = 0; |
88 |
|
|
88 |
|
|
89 | 89 |
for (TaxonDescription description : result){ |
90 | 90 |
// filter image galleries with more than one element |
91 | 91 |
if(description.isImageGallery() && description.getElements().size() > 1){ |
92 | 92 |
countTaxonDescriptions++; |
93 |
|
|
93 |
|
|
94 | 94 |
logger.warn("Found image gallery with mulitple TextData: " + description.getElements().size()); |
95 |
|
|
95 |
|
|
96 | 96 |
TextData newDescriptionElement = TextData.NewInstance(Feature.IMAGE()); |
97 |
|
|
97 |
|
|
98 | 98 |
Set<DescriptionElementBase> elementsToRemove = new HashSet<DescriptionElementBase>(); |
99 |
|
|
99 |
|
|
100 | 100 |
// consolidate media from all elements into a new element |
101 | 101 |
for(DescriptionElementBase element : description.getElements()){ |
102 | 102 |
List<Media> medias = element.getMedia(); |
103 |
|
|
103 |
|
|
104 | 104 |
for(Media media : medias){ |
105 | 105 |
newDescriptionElement.addMedia(media); |
106 | 106 |
logger.warn("Consolidating media"); |
107 | 107 |
} |
108 | 108 |
elementsToRemove.add(element); |
109 | 109 |
} |
110 |
|
|
110 |
|
|
111 | 111 |
// remove old elements |
112 | 112 |
for(DescriptionElementBase element : elementsToRemove){ |
113 | 113 |
description.removeElement(element); |
114 |
}
|
|
115 |
|
|
114 |
} |
|
115 |
|
|
116 | 116 |
// add the new element |
117 | 117 |
description.addElement(newDescriptionElement); |
118 |
|
|
118 |
|
|
119 | 119 |
} |
120 |
}
|
|
120 |
} |
|
121 | 121 |
conversation.commit(false); |
122 |
|
|
122 |
|
|
123 | 123 |
logger.warn("Descriptions Processed: " + countTaxonDescriptions); |
124 | 124 |
} |
125 | 125 |
} |
Also available in: Unified diff
ref #3658 remove nomenclaturalCode from all datasource related classes