View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: NodeAttributeManager.java
13   * Created: 2008-04-28
14   * Author: apohllo, awos
15   * $Id: NodeAttributeManager.java 2770 2009-04-21 19:17:34Z kpietak $
16   */
17  
18  package pl.edu.agh.cast.model.attributes;
19  
20  import java.util.Collection;
21  import java.util.Map;
22  
23  import pl.edu.agh.cast.model.ModelUtil;
24  import pl.edu.agh.cast.model.base.IDataSet;
25  import pl.edu.agh.cast.model.base.IEntity;
26  import pl.edu.agh.cast.model.base.Model;
27  
28  /**
29   * This class represents {@link AttributeManager}s which are to manage attributes of
30   * {@link pl.edu.agh.cast.model.visual.backward.Node}s.
31   *
32   * @author AGH CAST Team
33   */
34  public class NodeAttributeManager extends AttributeManager {
35  
36  	private static final long serialVersionUID = -3321558530249411428L;
37  
38  	/**
39  	 * Name of the permanent attribute <em>ID</em>.
40  	 */
41  	public static final String PERMANENT_ID = "Id"; //$NON-NLS-1$
42  
43  	/**
44  	 * Name of the permanent attribute <em>IsMain</em>.
45  	 */
46  	public static final String PERMANENT_ISMAIN = "IsMain"; //$NON-NLS-1$
47  
48  	/**
49  	 * Name of the permanent attribute <em>Label</em>.
50  	 */
51  	public static final String PERMANENT_LABEL = "Label"; //$NON-NLS-1$
52  
53  	/**
54  	 * The model utility instance.
55  	 */
56   	private transient ModelUtil modelUtil = new ModelUtil();
57  
58  	/**
59  	 * Dummy {@link NodeAttributeManager}.
60  	 */
61  	protected static NodeAttributeManager dummyManager = new NodeAttributeManager();
62  
63  	public static NodeAttributeManager getDummyManager() {
64  		return dummyManager;
65  	}
66  
67  	/**
68  	 * Default constructor.
69  	 */
70  	public NodeAttributeManager() {
71  		super();
72  
73  		// Add properties which are mandatory for Node
74  		this.registerPermanentAttribute(PERMANENT_ID, ValueType.String, false, false);
75  		getAttribute(PERMANENT_ID).setDefaultMergePolicy(AttributeMergePolicy.MERGE_POLICY_ALWAYS_FIRST);
76  
77  		this.registerPermanentAttribute(PERMANENT_LABEL, ValueType.String, true, true);
78  		getAttribute(PERMANENT_LABEL).setDefaultMergePolicy(AttributeMergePolicy.MERGE_POLICY_ALWAYS_FIRST);
79  
80  		this.registerPermanentAttribute(PERMANENT_ISMAIN, ValueType.Boolean, true, false);
81  		getAttribute(PERMANENT_ISMAIN).setDefaultMergePolicy(AttributeMergePolicy.MERGE_POLICY_LOGICAL_OR);
82  
83  	}
84  
85  	/**
86  	 * Initializes new instance with attributes defined for {@link IEntity}'ies in given {@link IDataSet}s.
87  	 *
88  	 * @param dataSets collection of {@link IDataSet}s from which attributes should be extracted
89  	 */
90  	public NodeAttributeManager(Collection<IDataSet> dataSets) {
91  		this();
92  		Collection<IEntity> entities = Model.getEntities(dataSets);
93  		Map<String, Map<String, String>> map = modelUtil.getAttributeMap(dataSets);
94  		for (IEntity entityNode : entities) {
95  			for (String attributeName : entityNode.getAttributeNames()) {
96  				if (!this.isRegisteredId(attributeName)) {
97  					// TODO get type of attribute and set it properly
98  					// is this really needed, since the attribute is not modifiable?
99  
100 					// during tests map == null (due to ExtensionRegistry being absent)
101 					String modelExtensionId = map != null ? map.get(entityNode.getType()).get(attributeName) : null;
102 
103 					this.registerPermanentAttribute(attributeName, ValueType.String, true, false, entityNode.getType(),
104 					        modelExtensionId);
105 				}
106 			}
107 		}
108 	}
109 
110 }