Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 30f53e4589bbad2ff0d7c2ce890cdc0559b58c80 (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) 2010 Oracle Corporation 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:
 *    Oracle Corporation - initial API and implementation and/or initial documentation
 *******************************************************************************/

package org.eclipse.jst.jsf.common.metadata.query.internal.taglib;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.jst.jsf.common.metadata.Entity;
import org.eclipse.jst.jsf.common.metadata.Model;
import org.eclipse.jst.jsf.common.metadata.internal.IMetaDataDomainContext;
import org.eclipse.jst.jsf.common.metadata.internal.IMetaDataModelContext;
import org.eclipse.jst.jsf.common.metadata.internal.IMetaDataModelManager;
import org.eclipse.jst.jsf.common.metadata.internal.MetaDataModelContext;
import org.eclipse.jst.jsf.common.metadata.internal.MetaDataModelManagerFactory;
import org.eclipse.jst.jsf.common.metadata.query.AbstractEntityQueryVisitor;
import org.eclipse.jst.jsf.common.metadata.query.IResultSet;
import org.eclipse.jst.jsf.common.metadata.query.internal.HierarchicalSearchControl;
import org.eclipse.jst.jsf.common.metadata.query.internal.IHierarchicalEntityVisitor;
import org.eclipse.jst.jsf.common.metadata.query.internal.IPredicateMatcher.MATCH;
import org.eclipse.jst.jsf.common.metadata.query.internal.SearchControl;
import org.eclipse.jst.jsf.common.metadata.query.internal.SimpleResultSet;

/**
 * @author gekessle
 *
 */
public class TaglibDomainEntityQuerySpecVisitor extends AbstractEntityQueryVisitor implements IHierarchicalEntityVisitor {
	private HierarchicalSearchControl 			_control;
	private ITaglibDomainEntityPredicateMatcher	_matcher;
	private IMetaDataDomainContext 				_context;
	private List<Entity> 						_entityResults;
	private Model 								_initialEntity;
	private boolean 							_stop;

	/**
	 * Constructor with default SearchControl 
	 * @param context 
	 * @param matcher
	 */
	public TaglibDomainEntityQuerySpecVisitor(final IMetaDataDomainContext context, final ITaglibDomainEntityPredicateMatcher matcher) {
		this(context, matcher, new HierarchicalSearchControl());
	}
	
	/**
	 * Constructor 
	 * @param context 
	 * @param matcher
	 * @param control 
	 */
	public TaglibDomainEntityQuerySpecVisitor(final IMetaDataDomainContext context, final ITaglibDomainEntityPredicateMatcher matcher, final HierarchicalSearchControl control) {
		_matcher = matcher;
		_context = context;		
		_control = control;
	}

	/**
	 * @return Set of {@link Entity}s matching the entity query spec
	 * Note: does not support wild card match of models yet.
	 */
	public IResultSet<Entity> findEntities() {// throws InvalidQuerySpecification, MetaDataException {
		_matcher.reset();
		
		//first find model according to matcher...   
		//DOES NOT SUPPORT WILD CARDS FOR MODEL URI YET!!!
		//we cannot support wild cards because models are loaded lazily; even their id's.
		final IMetaDataModelManager mgr = MetaDataModelManagerFactory.getMetaDataModelManagerInstance(getProject());
		if (mgr != null) {
			final Model model= mgr.getModel(getModelContext());
			findEntities(model);			
		} else {
			//throw err?
		}
		return new SimpleResultSet<Entity>(getInternalEntityResults());
	}

	private void findEntities(final Model model) {

		if (model != null){
			_initialEntity = model;	
			_initialEntity.accept(this);			
		}

	}

	private List<Entity> getInternalEntityResults(){
		if (_entityResults == null){
			_entityResults = new ArrayList<Entity>();
		}
		return _entityResults;
	}
	
	private IMetaDataModelContext getModelContext() {
		return new MetaDataModelContext(getProject(), _context.getDomainId(), _matcher.getUri());
	}

	private IProject getProject() {		
		return (IProject)_context.getAdapter(IProject.class);
	}

	public boolean visitEnter(final Entity entity) {
		_matcher.pushLevel();
		if (_matcher.getCurrentLevel() <= _matcher.getMaxLevel()) {
			final MATCH match = _matcher.matches(entity);
			if (match == MATCH.FULLY) {
				getInternalEntityResults().add(entity);
				checkShouldStopVisitingEntities();
				return false;
			}
			else if (match == MATCH.PARTIALLY) {
				return true;
			}
		}
		return false;
	}

	public boolean stopVisiting() {
		return _stop;
	}

	private void checkShouldStopVisitingEntities(){
		//implement how to set stop to signal to the entity accept() to skip visiting
		if (_stop == false
				&& _control.getCountLimit()== getInternalEntityResults().size() 
				&& _control.getCountLimit() != SearchControl.COUNT_LIMIT_NONE )
			
			_stop = true;
	}

	public boolean visitLeave(Entity entity) {
//		_stop = _matcher.checkShouldStopVisitingEntities();
//		if (entity != _initialEntity)
//			_entityQuery.popLevel();
		if (entity != _initialEntity) 
			_matcher.popLevel();
		return true;
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.jst.jsf.common.metadata.query.IEntityVisitor#visit(org.eclipse.jst.jsf.common.metadata.Entity)
	 */
	public void visit(final Entity key) {
		//do nothing... all work now done in visitEnter/visitLeave
	}
	
}

Back to the top