📄 kernel.java
字号:
/*
* Created on 10-Apr-2005
*
* TODO All
*/
package ke.core.kernel;
import java.util.ArrayList;
import java.util.Map;
import ke.core.Concept;
import ke.core.ConceptInstance;
import ke.core.ConceptType;
import ke.core.Relationship;
import ke.core.components.ComponentsException;
import ke.core.components.SearchIndexComponent;
import ke.core.components.UsersComponent;
/**
* @author James Cunningham
* a kernel provides a platform where concepts can be added/removed.
* TODO Maybe change so kernel is not singleton
*/
public class Kernel {
private Kernel() {
super();
}
/**
* @param searchIndexComponent
* @param usersComponent
* @throws ComponentsException
*/
public static void registerComponents(SearchIndexComponent searchIndexComponent,
UsersComponent usersComponent) throws KernelException {
if(searchIndexComponent == null ||
usersComponent == null) {
Kernel.isInitialised = false;
throw new KernelException("Error: attempted to register null component");
}
Kernel.searchIndexComponent = searchIndexComponent;
Kernel.usersComponent = usersComponent;
Kernel.isInitialised = true;
}
/**
* @return
*/
public static boolean isInitialised() {
return Kernel.isInitialised;
}
/**
* @return
* @throws ComponentsException
*/
public static Kernel instance() throws KernelException {
if(!Kernel.isInitialised)
throw new KernelException("Error: Kernel not initialised");
else if(Kernel.instance == null) {
Kernel.instance = new Kernel();
}
return Kernel.instance;
}
/**
* @param id
* @return
*/
public boolean idExists(String id) throws KernelException{
try {
return Kernel.searchIndexComponent.idExists(id);
} catch(ComponentsException ce) {
throw new KernelException("Error: " + ce);
}
}
/**
* @param id
* @return
*/
public Class getClassForId(String id) throws KernelException {
try {
return Kernel.searchIndexComponent.getClassForId(id);
} catch (ComponentsException ce) {
throw new KernelException("Error: " + ce);
}
}
/**
* @param groupId
* @param id , which identifies a concept.
* @return
* @throws KernelException
*/
public Concept getConcept(String groupId,
String id) throws KernelException {
try {
Class c = Kernel.searchIndexComponent.getClassForId(id);
if(ConceptInstance.class.isAssignableFrom(c)) {
if(!Kernel.usersComponent.hasViewInstanceRight(groupId, id)) {
throw new KernelException("Group: " + groupId + "does not have view right on instance: " + id);
}
}
return Kernel.searchIndexComponent.getConcept(id);
} catch (ComponentsException ce) {
throw new KernelException("Error: " + ce);
}
}
/**
* @param groupId
* @param concept
* @return
* @throws KernelException
*/
public boolean addConcept(String groupId,
Concept concept) throws KernelException {
System.out.println("adding concept called thorugh kernel");
Class c = concept.getClass();
if(ConceptInstance.class.isAssignableFrom(c)) {
String typeId = ((ConceptInstance)concept).getType().getId();
if(!Kernel.usersComponent.hasCreateEntityRight(groupId, typeId))
throw new KernelException("Group: " + groupId + "does not have create right for type: " + typeId);
}
else if(ConceptType.class.isAssignableFrom(c)) {
if(!Kernel.usersComponent.hasCreateTypeRight(groupId))
throw new KernelException("Group: " + groupId + " does not have add type right");
}
// else something odd's going on with new types and shit so we'll let it be
try {
return Kernel.searchIndexComponent.addConcept(concept);
} catch (ComponentsException ce) {
throw new KernelException("Error adding concept: " + ce);
}
}
/**
* @param groupId
* @param id
* @param propertyValues
* @return
* @throws KernelException
*/
/*public boolean updateConceptInstance(String groupId,
String id,
Map propertyValues) throws KernelException {
// check that the right exists to edit the properties in the map
// and that all the properties in the map are valid (by name)
try {
Concept concept = Kernel.searchIndexComponent.getConcept(id);
if(concept == null) {
throw new KernelException("Error: Concept with id " + id + " does not exist");
}
else if(!ConceptInstance.class.isAssignableFrom(concept.getClass())) {
throw new KernelException("Error: Concept with id " + id + " is not an instance");
}
ConceptInstance instance = (ConceptInstance)concept;
String[] instancePropNames = instance.getPropertyNames();
List namesList = Arrays.asList(instancePropNames);
Set keySet = propertyValues.keySet();
String key;
for(Iterator i = keySet.iterator(); i.hasNext();) {
key = (String)i.next();
// check that the supplied key is a valid property name for the instance
if(!namesList.contains(key)) {
throw new KernelException("Error: invalid property name supplied: " + key);
}
// if it is valid then check that the group has the right to edit it
else if(!Kernel.usersComponent.hasEditPropertyRight(groupId, id, key)) {
throw new KernelException("Group: " + groupId + " cannot edit " + key + " on instance " + id);
}
}
return Kernel.searchIndexComponent.updateConceptInstance(id, propertyValues);
} catch (ComponentsException ce) {
throw new KernelException("Error updating concept instance: " + ce);
}
}*/
/**
* @param groupId
* @param id
* @param propertName
* @param propertyValue
* @return
* @throws KernelException
*/
public boolean updateConceptInstanceProperty(String groupId,
String id,
String propertyName,
Object propertyValue) throws KernelException {
if(!Kernel.usersComponent.hasEditPropertyRight(groupId, id, propertyName)) {
throw new KernelException("Error: " + groupId + " does not have right to edit " + propertyName + " on " + id);
}
try {
Kernel.searchIndexComponent.updateConceptInstanceProperty(id, propertyName, propertyValue);
} catch (ComponentsException ce) {
ce.printStackTrace();
throw new KernelException("Error updating concept with id: " + id+" details:"+ce);
}
return false;
}
/**
* @param groupId
* @param id
* @return
* @throws KernelException
*/
public boolean removeConcept(String groupId,
String id) throws KernelException {
if(!Kernel.usersComponent.hasRemoveConceptRight(groupId, id)) {
throw new KernelException("Group: " + groupId + " does not have right to remove concept " + id);
}
try {
boolean res = Kernel.searchIndexComponent.removeConcept(id);
if(res) return true;
else return false;
} catch (ComponentsException ce) {
throw new KernelException("Error removing concept with id: " + id);
}
}
/**
* @param groupId
* @param typeId
* @return
*/
public ConceptInstance[] getConceptInstancesByType(String groupId,
String typeId) throws KernelException{
// we will filter the results returned by the search using the
// view right
try {
ConceptInstance[] ci = Kernel.searchIndexComponent.getConceptInstancesByType(typeId);
return this.filterInstancesByViewRight(groupId, ci);
} catch (ComponentsException ce) {
throw new KernelException("Error getting concept instances: " + ce);
}
}
/**
* @param groupId
* @param values
* @return
*/
public ConceptInstance[] getConceptIntancesByPropertyValue(String groupId,
String typeId,
Map values) throws KernelException {
// we will filter the results returned by the search using the
// view right
try {
ConceptInstance[] ci = Kernel.searchIndexComponent.getConceptIntancesByPropertyValue(typeId, values);
return this.filterInstancesByViewRight(groupId, ci);
} catch (ComponentsException ce) {
throw new KernelException("Error getting instances by property: " + ce);
}
}
/**
* @param groupId
* @param sourceId
* @param destId
* @return
*/
public Relationship[] getRelationshipsBySourceDestinationIds(String groupId,
String sourceId,
String destId) throws KernelException{
try {
// we will filter the results returned by the search using the
// view right
Relationship[] rels = Kernel.searchIndexComponent.getRelationshipsBySourceDestinationIds(sourceId, destId);
// TODO check that this actually works!
//return (Relationship[])this.filterInstancesByViewRight(groupId, rels);
return rels; // TODO URGENT FIX
} catch (ComponentsException ce) {
throw new KernelException("Error getting relationship: " + ce);
}
}
/**
* @param groupId
* @param ci
* @return
*/
private ConceptInstance[] filterInstancesByViewRight(String groupId,
ConceptInstance[] ci) {
ArrayList results = new ArrayList();
for(int i = 0; i < ci.length; i++) {
if(Kernel.usersComponent.hasViewInstanceRight(groupId, ci[i].getId())) {
results.add(ci[i]);
}
}
return (ConceptInstance[])results.toArray(new ConceptInstance[results.size()]);
}
/**
* @return
*/
public UsersComponent getUsersComponent() {
return Kernel.usersComponent;
}
public String getIdForType(String groupId, String typeName) throws KernelException {
try {
return Kernel.searchIndexComponent.getIdForType(typeName);
} catch (ComponentsException ce) {
throw new KernelException("Error: " + ce);
}
}
//------------------------------------
private static SearchIndexComponent searchIndexComponent;
private static UsersComponent usersComponent;
private static boolean isInitialised = false;
private static Kernel instance;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -