View Javadoc
1   package org.argeo.connect.core;
2   
3   import java.util.Collections;
4   import java.util.Map;
5   import java.util.SortedMap;
6   import java.util.TreeMap;
7   
8   import javax.jcr.Node;
9   import javax.jcr.RepositoryException;
10  import javax.jcr.Session;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.argeo.connect.AppService;
15  import org.argeo.connect.ConnectException;
16  import org.argeo.connect.ServiceRanking;
17  import org.argeo.connect.SystemAppService;
18  import org.argeo.eclipse.ui.EclipseUiUtils;
19  
20  public class DynamicSystemAppService extends AbstractAppService implements SystemAppService {
21  	private final static Log log = LogFactory.getLog(DynamicSystemAppService.class);
22  
23  	private SortedMap<ServiceRanking, AppService> knownAppServices = Collections.synchronizedSortedMap(new TreeMap<>());
24  	// private List<AppService> knownAppServices = Collections.synchronizedList(new
25  	// ArrayList<>());
26  
27  	public DynamicSystemAppService() {
28  		super();
29  	}
30  
31  	@Override
32  	public Node publishEntity(Node parent, String nodeType, Node srcNode, boolean removeSrcNode)
33  			throws RepositoryException {
34  		for (AppService appService : knownAppServices.values()) {
35  			if (appService.isKnownType(nodeType))
36  				return appService.publishEntity(parent, nodeType, srcNode, removeSrcNode);
37  		}
38  		return null;
39  	}
40  
41  	@Override
42  	public String getAppBaseName() {
43  		return OfficeConstants.SUITE_APP_BASE_NAME;
44  	}
45  
46  	@Override
47  	public String getBaseRelPath(String nodeType) {
48  		for (AppService appService : knownAppServices.values()) {
49  			if (appService.isKnownType(nodeType))
50  				return appService.getBaseRelPath(nodeType);
51  		}
52  		return null;
53  		// return getAppBaseName();
54  	}
55  
56  	@Override
57  	public String getDefaultRelPath(Node entity) throws RepositoryException {
58  		for (AppService appService : knownAppServices.values()) {
59  			if (appService.isKnownType(entity))
60  				return appService.getDefaultRelPath(entity);
61  		}
62  		return null;
63  	}
64  
65  	@Override
66  	public String getMainNodeType(Node node) {
67  		for (AppService appService : knownAppServices.values()) {
68  			String foundType = appService.getMainNodeType(node);
69  			if (EclipseUiUtils.notEmpty(foundType))
70  				return foundType;
71  		}
72  		return null;
73  	}
74  
75  	@Override
76  	public String getDefaultRelPath(Session session, String nodetype, String id) {
77  		for (AppService appService : knownAppServices.values()) {
78  			if (appService.isKnownType(nodetype))
79  				return appService.getDefaultRelPath(session, nodetype, id);
80  		}
81  		return null;
82  	}
83  
84  	/** Insures the correct service is called on save */
85  	@Override
86  	public Node saveEntity(Node entity, boolean publish) {
87  		for (AppService appService : knownAppServices.values()) {
88  			if (appService.isKnownType(entity))
89  				return appService.saveEntity(entity, publish);
90  		}
91  		throw new ConnectException("Unknown NodeType for " + entity + ". Cannot save");
92  		// return AppService.super.saveEntity(entity, publish);
93  	}
94  
95  	@Override
96  	public boolean isKnownType(Node entity) {
97  		for (AppService appService : knownAppServices.values()) {
98  			if (appService.isKnownType(entity))
99  				return true;
100 		}
101 		return false;
102 	}
103 
104 	@Override
105 	public boolean isKnownType(String nodeType) {
106 		for (AppService appService : knownAppServices.values()) {
107 			if (appService.isKnownType(nodeType))
108 				return true;
109 		}
110 		return false;
111 	}
112 
113 	public void addAppService(AppService appService, Map<String, Object> properties) {
114 		knownAppServices.put(new ServiceRanking(properties), appService);
115 		if (log.isDebugEnabled())
116 			log.debug("Added app service " + appService);
117 	}
118 
119 	public void removeAppService(AppService appService, Map<String, Object> properties) {
120 		knownAppServices.remove(new ServiceRanking(properties));
121 	}
122 }