Project

General

Profile

Revision 48e5bbde

ID48e5bbde635e7d2fd347cf5c2eef0d103c3699db
Parent 1000779d
Child c31444d4

Added by Patrick Plitzner over 2 years ago

fix #8257 Remove factory methods for term node creation

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/StructureTreeOwlImport.java
171 171
            getVocabularyService().saveOrUpdate(vocabulary);
172 172
        }
173 173

  
174
        FeatureNode<?> childNode = FeatureNode.NewInstance(term);
175
        parent.addChild(childNode);
174
        FeatureNode<?> childNode = parent.addChild(term);
176 175

  
177 176
        nodeResource.listProperties(propHasSubStructure).forEachRemaining(prop->createNode(childNode, prop, treeLabel, model, state));
178 177
    }
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/in/SDDImport.java
1798 1798
					if (elDescriptiveConcept != null){
1799 1799
						String refDC = elDescriptiveConcept.getAttributeValue("ref");
1800 1800
						dc = features.get(refDC);
1801
						fn = FeatureNode.NewInstance(dc);
1802
					}
1803
					if (fn==null){
1804
						fn = FeatureNode.NewInstance();
1805 1801
					}
1806 1802
					Element elParent = elNode.getChild("Parent", sddNamespace);
1807 1803
					// in SDD links between Nodes are referenced by the <Parent> tag
......
1810 1806
						if (refP!=null) {
1811 1807
							FeatureNode<Feature> parent = featureNodes.get(refP);
1812 1808
							if (parent==null){
1813
								root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree
1809
							    // if no parent found or the reference is broken, add the node to the root of the tree
1810
							    fn = (dc==null)?root.addChild():root.addChild(dc);
1814 1811
							}
1815 1812
							else {
1816
								parent.addChild(fn);
1813
                                fn = (dc==null)?parent.addChild():parent.addChild(dc);
1817 1814
							}
1818 1815
						}
1819 1816
					}
1820 1817
					else {
1821
						root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree
1818
					    // if no parent found or the reference is broken, add the node to the root of the tree
1819
                        fn = (dc==null)?root.addChild():root.addChild(dc);
1822 1820
					}
1823 1821
				}
1824 1822
				featureNodes.put(idN, fn);
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/taxonx2013/TaxonXTreatmentExtractor.java
335 335
        }
336 336

  
337 337
        for (Feature feature:featuresMap.values()) {
338
            root2.addChild(FeatureNode.NewInstance(feature));
338
            root2.addChild(feature);
339 339
        }
340 340
        importer.getFeatureTreeService().saveOrUpdate(proibiospheretree);
341 341

  
cdmlib-io/src/test/java/eu/etaxonomy/cdm/io/owl/out/OwlExportTest.java
69 69
    public FeatureTree createFeatureTree() {
70 70
        FeatureTree tree = FeatureTree.NewInstance();
71 71

  
72
        FeatureNode nodeA = FeatureNode.NewInstance(Feature.NewInstance("A", "A", "A"));
73
        FeatureNode nodeA1 = FeatureNode.NewInstance(Feature.NewInstance("A1", "A1", "A1"));
74
        FeatureNode nodeA2 = FeatureNode.NewInstance(Feature.NewInstance("A2", "A2", "A2"));
75
        FeatureNode nodeB = FeatureNode.NewInstance(Feature.NewInstance("B", "B", "B"));
76
        FeatureNode nodeB1 = FeatureNode.NewInstance(Feature.NewInstance("B", "B1", "B1"));
77
        FeatureNode nodeC = FeatureNode.NewInstance(Feature.NewInstance("C", "C", "C"));
78

  
79
        tree.getRoot().addChild(nodeA);
80
        nodeA.addChild(nodeA1);
81
        nodeA.addChild(nodeA2);
82
        tree.getRoot().addChild(nodeB);
83
        nodeB.addChild(nodeB1);
84
        tree.getRoot().addChild(nodeC);
72
        FeatureNode nodeA = tree.getRoot().addChild(Feature.NewInstance("A", "A", "A"));
73
        FeatureNode nodeA1 = nodeA.addChild(Feature.NewInstance("A1", "A1", "A1"));
74
        FeatureNode nodeA2 = nodeA.addChild(Feature.NewInstance("A2", "A2", "A2"));
75
        FeatureNode nodeB = tree.getRoot().addChild(Feature.NewInstance("B", "B", "B"));
76
        FeatureNode nodeB1 = nodeB.addChild(Feature.NewInstance("B", "B1", "B1"));
77
        FeatureNode nodeC = tree.getRoot().addChild(Feature.NewInstance("C", "C", "C"));
85 78
        return tree;
86 79
    }
87 80

  
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/term/FeatureNode.java
163 163
// ***************************** FACTORY *********************************/
164 164

  
165 165
	/**
166
     * Creates a new empty term node instance.
167
     *
168
     * @see #NewInstance(Feature)
169
     */
170
    public static <T extends DefinedTermBase<T>> FeatureNode<T> NewInstance(TermType termType){
171
        return new FeatureNode<>(termType);
172
    }
173

  
174
	/**
175 166
	 * Creates a new empty feature node instance.
176 167
	 *
177 168
	 * @see #NewInstance(Feature)
169
	 * @deprecated will be removed in future
178 170
	 */
171
	@Deprecated
179 172
	public static FeatureNode<Feature> NewInstance(){
180 173
		return new FeatureNode<>(TermType.Feature);
181 174
	}
182 175

  
183
	/**
184
	 * Creates a new feature node instance only with the given {@link DefinedTermBase term}
185
	 * (without parent and children). The termType of the feature node is the termType of
186
	 * the given term
187
	 *
188
	 * @param	term	the term assigned to the new feature node
189
	 * @see 			#NewInstance()
190
	 */
191
	public static <T extends DefinedTermBase<T>> FeatureNode<T> NewInstance(T term){
192
	    FeatureNode<T> result = new FeatureNode<>(term.getTermType());
193
	    result.setTerm(term);
194
	    return result;
195
	}
196

  
197 176
// ******************** CONSTRUCTOR ***************************************/
198 177

  
199 178
	//TODO needed?
......
300 279
	public void addChild(FeatureNode<T> child){
301 280
		addChild(child, children.size());
302 281
	}
282

  
283
	/**
284
     * Creates a new node without a term and adds it to the end of
285
     * the list of children of
286
     * <i>this</i> node. Due to bidirectionality this method must also
287
     * assign <i>this</i> feature node as the parent of the new child.
288
     *
289
     * @return the newly created child node
290
     * @see             #getChildNodes()
291
     * @see             #setChildren(List)
292
     * @see             #removeChild(FeatureNode)
293
     * @see             #removeChild(int)
294
     */
295
    public FeatureNode<T> addChild(){
296
        return addChild((T)null, children.size());
297
    }
298

  
299
	/**
300
	 * Creates a new node for the given term and adds it to the end of
301
	 * the list of children of
302
	 * <i>this</i> node. Due to bidirectionality this method must also
303
	 * assign <i>this</i> feature node as the parent of the new child.
304
	 *
305
	 * @param	term	the term to be added
306
	 * @return the newly created child node
307
	 * @see				#getChildNodes()
308
	 * @see				#setChildren(List)
309
	 * @see				#removeChild(FeatureNode)
310
	 * @see				#removeChild(int)
311
	 */
312
	public FeatureNode<T> addChild(T term){
313
	    return addChild(term, children.size());
314
	}
315

  
316
    /**
317
     * Creates a new node for the given term and adds it at the
318
     * given (index + 1) position of the list of children of
319
     * <i>this</i> node. Due to bidirectionality this method must also
320
     * assign <i>this</i> feature node as the parent of the new child.
321
     *
322
     * @param   term    the term to be added
323
     * @return the newly created child node
324
     * @see             #getChildNodes()
325
     * @see             #setChildren(List)
326
     * @see             #removeChild(FeatureNode)
327
     * @see             #removeChild(int)
328
     */
329
	public FeatureNode<T> addChild(T term, int index){
330
	    FeatureNode<T> child = new FeatureNode<>(termType);
331
	    if(term!=null){
332
	        child.setTerm(term);
333
	    }
334
	    checkTermType(child);
335

  
336
	    List<FeatureNode<T>> children = this.getChildNodes();
337
	    if (index < 0 || index > children.size() + 1){
338
	        throw new IndexOutOfBoundsException("Wrong index: " + index);
339
	    }
340
	    child.setParent(this);
341
	    child.setFeatureTree(this.getFeatureTree());
342
	    children.add(index, child);
343
	    //TODO workaround (see sortIndex doc)
344
	    for(int i = 0; i < children.size(); i++){
345
	        children.get(i).setSortIndex(i);
346
	    }
347
	    child.setSortIndex(index);
348
	    return child;
349
	}
350

  
303 351
	/**
304 352
	 * Inserts the given feature node in the list of children of <i>this</i> feature node
305 353
	 * at the given (index + 1) position. If the given index is out of bounds
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/term/FeatureTree.java
170 170
		FeatureNode<Feature> root = result.getRoot();
171 171

  
172 172
		for (Feature feature : featureList){
173
			FeatureNode<Feature> child = FeatureNode.NewInstance(TermType.Feature);
174
			child.setTerm(feature);
175
			root.addChild(child);
173
			root.addChild(feature);
176 174
		}
177 175

  
178 176
		return result;
......
192 190
	protected FeatureTree(TermType termType) {
193 191
        this.termType = termType;
194 192
        checkTermType(this);  //check not null
195
		root = FeatureNode.NewInstance(termType);
193
		root = new FeatureNode<>(termType);
196 194
		root.setFeatureTree(this);
197 195
	}
198 196

  
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/term/FeatureTreeTest.java
13 13
import org.junit.Test;
14 14

  
15 15
import eu.etaxonomy.cdm.model.description.Feature;
16
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
17
import eu.etaxonomy.cdm.model.term.FeatureNode;
18
import eu.etaxonomy.cdm.model.term.FeatureTree;
19 16

  
20 17
public class FeatureTreeTest {
21 18
	@SuppressWarnings("unused")
......
40 37
	public void setUp() throws Exception {
41 38
		testTree = FeatureTree.NewInstance();
42 39

  
43
		node1 = FeatureNode.NewInstance(Feature.ANATOMY());
44
		node2 = FeatureNode.NewInstance(Feature.BIOLOGY_ECOLOGY());
45
		node3 = FeatureNode.NewInstance(Feature.DESCRIPTION());
46
		node4 = FeatureNode.NewInstance(Feature.DISCUSSION());
47

  
48
		testTree.getRoot().addChild(node1);
49
		node1.addChild(node2);
50
		node2.addChild(node3);
51
		node3.addChild(node4);
52

  
53

  
40
		node1 = testTree.getRoot().addChild(Feature.ANATOMY());
41
		node2 = node1.addChild(Feature.BIOLOGY_ECOLOGY());
42
		node3 = node2.addChild(Feature.DESCRIPTION());
43
		node4 = node3.addChild(Feature.DISCUSSION());
54 44

  
55 45
	}
56 46
//	@Test
......
61 51
//	}
62 52
	@Test
63 53
	public void testAddChild(){
64
		FeatureNode node21 = FeatureNode.NewInstance();
65
		node21.setTerm(Feature.ANATOMY());
66
		node1.addChild(node21, 1);
54
		FeatureNode node21 = node1.addChild(Feature.ANATOMY(), 1);
67 55

  
68 56
		assertEquals(node1.getChildNodes().size(), 2);
69 57
		assertEquals(node1.getChildNodes().get(1), node21);
......
75 63
	}
76 64
	@Test
77 65
	public void testClone(){
78
		FeatureNode node21 = FeatureNode.NewInstance();
79
		node21.setTerm(Feature.ADDITIONAL_PUBLICATION());
80
		node1.addChild(node21, 1);
66
		FeatureNode node21 = node1.addChild(Feature.ADDITIONAL_PUBLICATION(), 1);
81 67
		FeatureTree clone = (FeatureTree) testTree.clone();
82 68
		assertEquals (clone.getRoot().getTerm(), testTree.getRoot().getTerm());
83 69
		assertNotSame(clone.getRoot(), testTree.getRoot());
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/data/FullCoverageDataGenerator.java
414 414
		//Feature Tree
415 415
		FeatureTree featureTree = FeatureTree.NewInstance();
416 416
//		featureTree
417
		FeatureNode descriptionFeatureNode = FeatureNode.NewInstance(Feature.DESCRIPTION());
418
		FeatureNode leaveLengthNode = FeatureNode.NewInstance(leaveLength);
419
		featureTree.getRootChildren().add(descriptionFeatureNode);
420
		descriptionFeatureNode.addChild(leaveLengthNode);
417
		FeatureNode descriptionFeatureNode = featureTree.getRoot().addChild(Feature.DESCRIPTION());
418
		FeatureNode leaveLengthNode = descriptionFeatureNode.addChild(leaveLength);
421 419
		handleIdentifiableEntity(featureTree);
422 420

  
423 421
		State inapplicableState = State.NewInstance("inapplicableState", "inapplicableState", null);
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/FeatureNodeServiceImpl.java
121 121
	     FeatureNode node = load(nodeUUID);
122 122
	     DefinedTermBase child = HibernateProxyHelper.deproxy(termService.load(termChildUuid), DefinedTermBase.class);
123 123

  
124
	     FeatureNode childNode = FeatureNode.NewInstance(node.getTermType());
125
         childNode.setTerm(child);
124
	     FeatureNode childNode;
126 125
         UpdateResult result = new UpdateResult();
127 126
         if(position<0) {
128
             node.addChild(childNode);
127
             childNode = node.addChild(child);
129 128
         }
130 129
         else{
131
             node.addChild(childNode, position);
130
             childNode = node.addChild(child, position);
132 131
         }
133 132
         save(childNode);
134 133
         result.addUpdatedObject(node);
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/FeatureNodeServiceImplTest.java
76 76
		featureTreeService.save(featureTree);
77 77

  
78 78
		Feature feature = (Feature)termService.find(914);
79
		FeatureNode newNode = FeatureNode.NewInstance(feature);
80
		featureTree.getRoot().addChild(newNode);
79
		FeatureNode<Feature> newNode = featureTree.getRoot().addChild(feature);
81 80

  
82 81
		featureTreeService.save(featureTree);
83 82

  
......
97 96
		node2 = featureNodeService.load(node2Uuid);
98 97
		String oldTreeIndex = node2.treeIndex();
99 98

  
100
		FeatureNode newNode = FeatureNode.NewInstance(feature);
101
		node2.addChild(newNode);
99
		FeatureNode newNode = node2.addChild(feature);
102 100
		featureNodeService.saveOrUpdate(node2);
103 101

  
104 102
		commitAndStartNewTransaction(new String[]{"FeatureNode"});
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/NaturalLanguageGeneratorTest.java
78 78
		qd.setUnit(munit);
79 79
		qd.setFeature(qFeature);
80 80
		description.addElement(qd);
81
		FeatureNode qNode = FeatureNode.NewInstance(qFeature);
82
		root.addChild(qNode);
81
		root.addChild(qFeature);
83 82
	}
84 83

  
85 84
	@Test
......
102 101
				for (j=0; j<children[level][i].length ; j++) {
103 102
					Feature feature = Feature.NewInstance(null, children[level][i][j], null);
104 103
					featureSet.add(feature);
105
					FeatureNode child = FeatureNode.NewInstance(feature);
106
					parent.addChild(child);
104
					FeatureNode<Feature> child = parent.addChild(feature);
107 105
					if (level<depth-1) {
108 106
						buildBranches(child, children,level+1,depth, j);
109 107
					}
cdmlib-services/src/test/java/eu/etaxonomy/cdm/test/function/Datasource.java
37 37
import eu.etaxonomy.cdm.database.ICdmDataSource;
38 38
import eu.etaxonomy.cdm.model.agent.Person;
39 39
import eu.etaxonomy.cdm.model.description.Distribution;
40
import eu.etaxonomy.cdm.model.description.Feature;
41 40
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
42 41
import eu.etaxonomy.cdm.model.description.TaxonDescription;
43 42
import eu.etaxonomy.cdm.model.location.NamedArea;
......
49 48
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
50 49
import eu.etaxonomy.cdm.model.reference.Reference;
51 50
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
52
import eu.etaxonomy.cdm.model.taxon.Classification;
53 51
import eu.etaxonomy.cdm.model.taxon.Taxon;
54 52
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
55 53
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
56
import eu.etaxonomy.cdm.model.term.FeatureNode;
57
import eu.etaxonomy.cdm.model.term.FeatureTree;
58 54
import eu.etaxonomy.cdm.model.term.init.TermNotFoundException;
59 55

  
60 56
public class Datasource {
......
217 213

  
218 214
	}
219 215

  
220
	private void insertSomeData(CdmApplicationController appCtr) {
221
		Classification cl = Classification.NewInstance("myClass");
222
		TaxonNode node1 = cl.addChildTaxon(Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(null), null), null, null);
223
		appCtr.getClassificationService().save(cl);
224

  
225
		Taxon t2 = Taxon.NewInstance(null, null);
226
		t2.setTitleCache("Taxon2", true);
227
		TaxonNode node2 = node1.addChildTaxon(t2, null, null);
228

  
229
		Taxon t3 = Taxon.NewInstance(null, null);
230
		t3.setTitleCache("Taxon3", true);
231
		TaxonNode node3 = node1.addChildTaxon(t3, 0, null, null);
232

  
233
		appCtr.getTaxonNodeService().saveOrUpdate(node1);
234

  
235
		cl.addChildNode(node3, 0, null, null);
236
		appCtr.getTaxonNodeService().saveOrUpdate(node3);
237
		appCtr.getClassificationService().saveOrUpdate(cl);
238

  
239
		FeatureTree ft1 = FeatureTree.NewInstance();
240
		FeatureNode fn1 = FeatureNode.NewInstance((Feature)null);
241
		ft1.getRoot().addChild(fn1);
242
		appCtr.getFeatureNodeService().save(fn1);
243

  
244
		FeatureNode fn2 = FeatureNode.NewInstance((Feature)null);
245
		fn1.addChild(fn2);
246

  
247
		FeatureNode fn3 = FeatureNode.NewInstance((Feature)null);
248
		fn1.addChild(fn2, 0);
249

  
250
		appCtr.getFeatureNodeService().saveOrUpdate(fn1);
251

  
252
		ft1.getRoot().addChild(fn3, 0);
253
		appCtr.getFeatureNodeService().saveOrUpdate(fn3);
254
		appCtr.getFeatureTreeService().saveOrUpdate(ft1);
255
	}
216
//	private void insertSomeData(CdmApplicationController appCtr) {
217
//		Classification cl = Classification.NewInstance("myClass");
218
//		TaxonNode node1 = cl.addChildTaxon(Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(null), null), null, null);
219
//		appCtr.getClassificationService().save(cl);
220
//
221
//		Taxon t2 = Taxon.NewInstance(null, null);
222
//		t2.setTitleCache("Taxon2", true);
223
//		TaxonNode node2 = node1.addChildTaxon(t2, null, null);
224
//
225
//		Taxon t3 = Taxon.NewInstance(null, null);
226
//		t3.setTitleCache("Taxon3", true);
227
//		TaxonNode node3 = node1.addChildTaxon(t3, 0, null, null);
228
//
229
//		appCtr.getTaxonNodeService().saveOrUpdate(node1);
230
//
231
//		cl.addChildNode(node3, 0, null, null);
232
//		appCtr.getTaxonNodeService().saveOrUpdate(node3);
233
//		appCtr.getClassificationService().saveOrUpdate(cl);
234
//
235
//		FeatureTree ft1 = FeatureTree.NewInstance();
236
//		FeatureNode fn1 = FeatureNode.NewInstance((Feature)null);
237
//		ft1.getRoot().addChild(fn1);
238
//		appCtr.getFeatureNodeService().save(fn1);
239
//
240
//		FeatureNode fn2 = FeatureNode.NewInstance((Feature)null);
241
//		fn1.addChild(fn2);
242
//
243
//		FeatureNode fn3 = FeatureNode.NewInstance((Feature)null);
244
//		fn1.addChild(fn2, 0);
245
//
246
//		appCtr.getFeatureNodeService().saveOrUpdate(fn1);
247
//
248
//		ft1.getRoot().addChild(fn3, 0);
249
//		appCtr.getFeatureNodeService().saveOrUpdate(fn3);
250
//		appCtr.getFeatureTreeService().saveOrUpdate(ft1);
251
//	}
256 252

  
257 253
	private void testDatabaseChange() throws DataSourceNotFoundException{
258 254
		CdmApplicationController appCtr;

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)