Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6eb3c8fd88afa98bdd8d420f3ab9c646ae6b583c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.tasks.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.mylar.core.MylarStatusHandler;

/**
 * @author Rob Elves
 */
public class AttributeContainer implements Serializable {

	public static final String ERROR_NO_ATTRIBUTE_FACTORY = "Attribute factory not available.";

	private static final long serialVersionUID = 3538078709450471836L;

	/** The keys for the report attributes */
	private ArrayList<String> attributeKeys;

	/** report attributes (status, resolution, etc.) */
	private HashMap<String, RepositoryTaskAttribute> attributes;
	
	private transient AbstractAttributeFactory attributeFactory;

	public AttributeContainer(AbstractAttributeFactory attributeFactory) {
		this.attributeFactory = attributeFactory;
		attributeKeys = new ArrayList<String>();
		attributes = new HashMap<String, RepositoryTaskAttribute>();
	}

	public void setAttributeFactory(AbstractAttributeFactory factory) {
		this.attributeFactory = factory;
	}

	public void addAttribute(String key, RepositoryTaskAttribute attribute) {
		if (attributeFactory == null) {
			MylarStatusHandler.log(ERROR_NO_ATTRIBUTE_FACTORY, this);
			return;
		}
		String mapped = attributeFactory.mapCommonAttributeKey(key);
		if(mapped == null) {
			MylarStatusHandler.log("Mylar Error: mapped value for "+key+" returned null.", this);
			return;
		} if (!attributes.containsKey(mapped)) {
			attributeKeys.add(mapped);
		}
		attributes.put(mapped, attribute);
	}

	public RepositoryTaskAttribute getAttribute(String key) {
		if (attributeFactory == null) {
			MylarStatusHandler.log(ERROR_NO_ATTRIBUTE_FACTORY, this);
			return null;
		}
		String mapped = attributeFactory.mapCommonAttributeKey(key);
		return attributes.get(mapped);
	}

	public void removeAttribute(Object key) {
		attributeKeys.remove(key);
		attributes.remove(key);
	}

	public List<RepositoryTaskAttribute> getAttributes() {
		ArrayList<RepositoryTaskAttribute> attributeEntries = new ArrayList<RepositoryTaskAttribute>(attributeKeys
				.size());
		for (Iterator<String> it = attributeKeys.iterator(); it.hasNext();) {
			String key = it.next();
			RepositoryTaskAttribute attribute = attributes.get(key);
			attributeEntries.add(attribute);
		}
		return attributeEntries;
	}

	public void removeAllAttributes() {
		attributeKeys.clear();
		attributes.clear();
	}

	public void addAttributeValue(String key, String value) {
		if (attributeFactory == null) {
			MylarStatusHandler.log(ERROR_NO_ATTRIBUTE_FACTORY, this);
			return;
		}
		RepositoryTaskAttribute attrib = getAttribute(key);
		if (attrib != null) {
			attrib.addValue(value);
		} else {
			attrib = attributeFactory.createAttribute(key);
			attrib.addValue(value);
			addAttribute(key, attrib);
		}
	}

	/**
	 * sets a value on an attribute, if attribute doesn't exist, appropriate
	 * attribute is created
	 */
	public void setAttributeValue(String key, String value) {
		if (attributeFactory == null) {
			MylarStatusHandler.log(ERROR_NO_ATTRIBUTE_FACTORY, this);
			return;
		}
		RepositoryTaskAttribute attrib = getAttribute(key);
		if (attrib == null) {
			attrib = attributeFactory.createAttribute(key);
			addAttribute(key, attrib);
		}
		attrib.setValue(value);
	}

	public String getAttributeValue(String key) {
		if (attributeFactory == null) {
			MylarStatusHandler.log(ERROR_NO_ATTRIBUTE_FACTORY, this);
			return "";
		}
		String returnValue = "";
		RepositoryTaskAttribute attrib = getAttribute(key);
		if (attrib != null) {
			returnValue = attrib.getValue();
		}
		return returnValue;
	}

	public List<String> getAttributeValues(String key) {
		List<String> returnValue = new ArrayList<String>();
		if (attributeFactory == null) {
			MylarStatusHandler.log(ERROR_NO_ATTRIBUTE_FACTORY, this);
			return returnValue;
		}
		RepositoryTaskAttribute attrib = getAttribute(key);
		if (attrib != null) {
			returnValue = attrib.getValues();
		}
		return returnValue;
	}
	
	public AbstractAttributeFactory getAttributeFactory(){
		return attributeFactory;
	}

}

Back to the top