package tool.model;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.content.IContentDescriber;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.texteditor.IDocumentProvider;
import tool.ToolPlugin;
import tool.ToolProjectSupport;
import tool.model.grammar.ForteParser.serviceDeclaration_return;
import tool.model.grammar.ForteSOTree;
import tool.model.grammar.IErrorReporter;
import tool.navigator.cdf.ServiceObjectContentDescriber;
import tool.navigator.common.LabelProvider;
public class ToolServiceObject extends ToolComponent implements IProjectComponent, IErrorReporter{
private String type;
private String visability;
private String dialogDuration;
private boolean loadBalance;
private boolean failOver;
private int ID;
private String uuid;
private String planName;
private ToolPlan plan;
private String externalManager = "";
private String searchPath = "";
private String resourceName = "";
private String userName = "";
private String userPassword = "";
private static ServiceObjectContentDescriber describer = new ServiceObjectContentDescriber();
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public void setID(String iD) {
if (iD != null)
this.setID(Integer.parseInt(iD));
}
public String getDialogDuration() {
return dialogDuration;
}
public static final String REGEX = "service\\s+(.+)\\s*:\\s*(.+)\\s*=\\s*\\(DialogDuration = (.+)," +
"\\s*Visibility = (.+)" +
"(,\\s*ExternalManager = '(.+)'|)" +
"(,\\s*FailOver = (TRUE|FALSE|true|false)|)" +
"(,\\s*LoadBalance = (TRUE|FALSE|true|false)|)\\)" +
"\\s*HAS PROPERTY\\s*extended = \\(UUID = '([A-Za-z0-9\\-]+)'\\);";
public static final Pattern pattern = Pattern.compile(REGEX, Pattern.MULTILINE);
public synchronized static ToolServiceObject fetch(IProject project, String planName, String soName){
ToolServiceObject model = getSOCache(project).get((planName + "." + soName).toUpperCase());
if (model == null){
/*
* if its not in the cashe we look for it in the resource tree.
* if it is there we parse it which adds it to the cache
*
* first we check in the user classes
*/
List<IFolder> sourceFolders = ToolProjectSupport.getSourceFolders(project);
for (IFolder projectsFolder : sourceFolders){
IFolder planFolder = (IFolder)projectsFolder.findMember(planName);
if (planFolder != null){
IResource clsResource = planFolder.findMember(soName + ".cdf");
if (clsResource != null){
ToolServiceObject cls = fetch((IFile)clsResource);
if (cls!= null)
return cls;
}
}
}
/*
* then we check on the forte libraries
*/
IFolder libFolder = (IFolder)project.findMember(ToolProjectSupport.UDS_LIBRARY_FOLDER_NAME);
IResource resource = libFolder.findMember(planName + ".pex");
if (resource != null){
ToolPlan plan = ToolPlan.getInstance(resource);
if (plan != null){
ToolServiceObject cls = plan.findSO(soName);
if (cls!= null)
return cls;
}
}
/*
* finally we check in the user libraries
*/
libFolder = (IFolder)project.findMember(ToolProjectSupport.LIBRARY_FOLDER_NAME);
resource = libFolder.findMember(planName + ".pex");
if (resource != null){
ToolPlan plan = ToolPlan.getInstance(resource);
if (plan != null){
ToolServiceObject cls = plan.findSO(soName);
if (cls!= null)
return cls;
}
}
}
return model;
}
public synchronized static ToolServiceObject fetch(IFile modelFile){
IProject project = modelFile.getProject();
String planName = modelFile.getParent().getName();
String soName = modelFile.getName().substring(0, modelFile.getName().lastIndexOf("."));
String keyString = (planName + "." + soName).toUpperCase();
ToolServiceObject model = getSOCache(project).get(keyString);
if (model == null){
try {
if (describer.describe(modelFile.getContents(), null) != IContentDescriber.VALID)
return null;
model = new ToolServiceObject();
model.setFile(modelFile);
model.setPlanName(planName);
model.setParent(model.getPlan());
IDocumentProvider provider = new TextFileDocumentProvider();
provider.connect(modelFile);
model.document = provider.getDocument(modelFile);
model.parse(model.document);
addToCache(model.getProject(), model);
model.setDirty(false);
} catch (CoreException e) {
ToolPlugin.log(IStatus.ERROR,"Error updating Interface Model.", e);
} catch (IOException e) {
ToolPlugin.log(IStatus.ERROR,"Error updating Interface Model.", e);
}
}
return model;
}
public static void addToCache(IProject project, ToolServiceObject newSO){
String planName = newSO.getPlanName();
String soName = newSO.getToolName();
String keyString = (planName + "." + soName).toUpperCase();
getSOCache(project).put(keyString, newSO);
}
private ToolServiceObject(){
super();
}
public ToolServiceObject(String name){//Testing only
super();
setToolName(name);
}
@SuppressWarnings("unused")
private ToolServiceObject(Object parent) {
super();
setParent(parent);
}
public void parse(IDocument doc){
this.document = doc;
String source = document.get();
parse(source);
}
public void parse(String source){
try {
CommonTree tree = null;
CommonTokenStream tokens = createTokenStream(source);
parser.setTokenStream(tokens);
parser.setErrorReporter(this);
parser.setTreeAdaptor(adaptor);
serviceDeclaration_return result = parser.serviceDeclaration();
if (parser.getNumberOfSyntaxErrors() > 0){
ToolPlugin.showError(parser.getNumberOfSyntaxErrors() + " Syntax error in project " + getFile().getName() + "\n"
+ this.parseErrors.toString(), null);
} else {
tree = (CommonTree) result.getTree();
CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
nodes.setTokenStream(tokens);
ForteSOTree walker = new ForteSOTree(this, nodes);
walker.serviceDeclaration();
}
} catch (RecognitionException e) {
ToolPlugin.showError("Cannot parse service " + getFile().getName(), e);
}
// Matcher matcher = pattern.matcher(source);
// if (matcher.find()){
// String wholeMatch = matcher.group(0);
// setToolName(matcher.group(1).trim());
// setType(matcher.group(2).trim());
// setDialogDuration(matcher.group(3));
// setVisability(matcher.group(4));
// //TODO matcher.group(6) External Manager
// setFailOver(matcher.group(8));
// setLoadBalance(matcher.group(10));
//
// setUuid(matcher.group(11));
// }
}
public String toSource(){
StringBuilder source = new StringBuilder();
source.append("service ");
source.append(getToolName());
source.append(" : ");
source.append(getType());
source.append(" = (DialogDuration = ");
source.append(getDialogDuration().toLowerCase());
source.append(",\n");
source.append("\tVisibility = ");
source.append(getVisability().toLowerCase());
source.append(",\n");
source.append("\tFailOver = ");
source.append(isFailOver());
source.append(",\n");
source.append("\tLoadBalance = ");
source.append(isLoadBalance());
source.append(") HAS PROPERTY\n");
source.append("\textended = (UUID = '");
source.append(getUuid());
source.append("');\n");
String sourceString = source.toString();
this.document.set(sourceString);
return sourceString;
}
public String getType() {
return type;
}
public void setType(String type) {
propertyChangeSupport.firePropertyChange("type", this.type,
this.type = type);
setDirty(true);
}
public void setVisability(String visability) {
propertyChangeSupport.firePropertyChange("visability", this.visability,
this.visability = visability.toUpperCase());
setDirty(true);
}
public void setDialogDuration(String dialogDuration) {
propertyChangeSupport.firePropertyChange("dialogDuration", this.dialogDuration,
this.dialogDuration = dialogDuration.toUpperCase());
setDirty(true);
}
public void setUuid(String uuid) {
propertyChangeSupport.firePropertyChange("uuid", this.uuid,
this.uuid = uuid);
}
public String getVisability() {
return visability;
}
public String getUuid() {
return uuid;
}
public boolean isLoadBalance() {
return loadBalance;
}
public void setLoadBalance(boolean loadBalance) {
propertyChangeSupport.firePropertyChange("loadBalance", this.loadBalance,
this.loadBalance = loadBalance);
setDirty(true);
}
public void setLoadBalance(String loadBalance) {
if (loadBalance == null || loadBalance.isEmpty())
return;
this.setLoadBalance(loadBalance.equalsIgnoreCase("TRUE"));
}
public String getExternalManager() {
return externalManager;
}
public void setExternalManager(String externalManager) {
propertyChangeSupport.firePropertyChange("externalManager", this.externalManager,
this.externalManager = externalManager);
setDirty(true);
}
public String getSearchPath() {
return searchPath;
}
public void setSearchPath(String searchPath) {
propertyChangeSupport.firePropertyChange("searchPath", this.searchPath,
this.searchPath = searchPath);
setDirty(true);
}
public String getResourceName() {
return resourceName;
}
public void setResourceName(String resourceName) {
propertyChangeSupport.firePropertyChange("resourceName", this.resourceName,
this.resourceName = resourceName);
setDirty(true);
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
propertyChangeSupport.firePropertyChange("userName", this.userName,
this.userName = userName);
setDirty(true);
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
propertyChangeSupport.firePropertyChange("userPassword", this.userPassword,
this.userPassword = userPassword);
setDirty(true);
}
public boolean isFailOver() {
return failOver;
}
public void setFailOver(boolean failOver) {
propertyChangeSupport.firePropertyChange("failOver", this.failOver,
this.failOver = failOver);
setDirty(true);
}
public void setFailOver(String failOver) {
if (failOver == null || failOver.isEmpty())
return;
this.setFailOver(failOver.equalsIgnoreCase("TRUE"));
}
@Override
public String getLabelText() {
return getToolName();
}
@Override
public String getLabelText(int options) {
if ((options & LabelProvider.FULL_PATH) == LabelProvider.FULL_PATH){
return getPlanName() + "." + getLabelText();
}
return getLabelText();
}
public String getPlanName(){
return this.planName;
}
public void setPlanName(String planName){
propertyChangeSupport.firePropertyChange("planName", this.planName,
this.planName = planName);
setDirty(true);
}
public ToolPlan getPlan() {
if (this.plan == null){
this.plan = ToolPlan.fetch(getProject(), this.planName);
}
return this.plan;
}
@SuppressWarnings("unchecked")
public static Map<String, ToolServiceObject> getSOCache(IProject project){
Map<String, ToolServiceObject> cache = null;
try {
cache = (Map<String, ToolServiceObject>)project.getSessionProperty(new QualifiedName(ToolProjectSupport.PROJECT_QUALIFIED_NAME, ToolProjectSupport.SO_CACHE));
if (cache == null){
cache = new HashMap<String, ToolServiceObject>();
project.setSessionProperty(new QualifiedName(ToolProjectSupport.PROJECT_QUALIFIED_NAME, ToolProjectSupport.SO_CACHE), cache);
}
} catch (CoreException e) {
ToolPlugin.showError("Error getting class cache", e);
}
return cache;
}
@Override
public IFile getProjectFile() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setProjectFile(IFile file) {
// TODO Auto-generated method stub
}
@Override
public String getIconString() {
return "icons/service_object.gif";
}
@Override
public void reportError(String error) {
this.parseErrors.append(error + "\n");
}
public boolean isDBSessionSO(){
return this.type.toLowerCase().contains("DBSession".toLowerCase()) ||
this.type.toLowerCase().contains("qqdb_Session".toLowerCase());
}
public boolean isDBResourceSO(){
return this.type.toLowerCase().contains("DBResource".toLowerCase()) ||
this.type.toLowerCase().contains("qqdb_RdbmsRM".toLowerCase());
}
public boolean isDatabaseSO(){
return this.type.toLowerCase().contains("GenericDBMS".toLowerCase());
}
public boolean isToolSO(){
return !this.type.toLowerCase().contains("GenericDBMS".toLowerCase());
}
}