summaryrefslogtreecommitdiffstats
path: root/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentProcessorsMap.java
blob: d7d35474bd21fdd32e67a179b2666888d2050abc (plain)
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
package org.apache.tuscany.sca.domain.search;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.tuscany.sca.domain.search.impl.Document;

public class DocumentProcessorsMap extends
		HashMap<Class<?>, List<DocumentProcessor>> implements DocumentProcessor {

	private static final long serialVersionUID = 3967390896890947159L;

	private Object documentKey;

	public void addDocumentProcessor(Class<?> clazz, DocumentProcessor processor) {
		List<DocumentProcessor> processors = get(clazz);

		if (processors == null) {
			processors = new LinkedList<DocumentProcessor>();
			put(clazz, processors);

		}

		processors.add(processor);

	}

	private void appendProcessors(LinkedList<DocumentProcessor> processorsList,
			List<DocumentProcessor> processors, Object object) {

		if (processors != null) {

			for (DocumentProcessor processor : processors) {

				if (this.documentKey == null) {
					this.documentKey = processor.getDocumentKey(object);
					
					if (processorsList == null) {
						return;
					}
					
				}

				if (processorsList != null) {
					processorsList.add(processor);
				}

			}

		}

	}

	private void findAllDocumentProcessors(
			LinkedList<DocumentProcessor> processorsList, Object object) {
		Class<?> clazz = object.getClass();
		appendProcessors(processorsList, get(clazz), object);

		while (clazz != null) {
			Class<?>[] interfaces = clazz.getInterfaces();

			for (Class<?> interfac : interfaces) {
				Class<?>[] interfaces2 = interfac.getInterfaces();
				appendProcessors(processorsList, get(interfac), object);

				for (Class<?> interface2 : interfaces2) {
					appendProcessors(processorsList, get(interface2), object);
				}

			}

			clazz = clazz.getSuperclass();
			appendProcessors(processorsList, get(clazz), object);

		}

	}

	public Document process(DocumentProcessor parentProcessor,
			DocumentMap documents, Object object, Document document, String parent) {

		LinkedList<DocumentProcessor> processorsList;

		try {

			this.documentKey = document;
			processorsList = new LinkedList<DocumentProcessor>();
			findAllDocumentProcessors(processorsList, object);

			if (document == null && this.documentKey != null) {
				document = documents.get(this.documentKey);

				if (document == null) {
					document = FAKE_DOCUMENT;
				}

			}

		} finally {
			this.documentKey = null;
		}

		for (DocumentProcessor processor : processorsList) {
			processor.process(parentProcessor, documents, object, document, parent);
		}

		return document;

	}

	public Object getDocumentKey(Object object) {
		
		try {
			findAllDocumentProcessors(null, object);
			
			return this.documentKey;
			
		} finally {
			this.documentKey = null;
		}
		
	}

}