Commit 7c5f2703 authored by Romain Bioteau's avatar Romain Bioteau
Browse files

fix xtext resource

parent 6235aeec
......@@ -16,12 +16,7 @@
*/
package org.bonitasoft.studio.condition.ui.expression;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bonitasoft.studio.common.ExpressionConstants;
import org.bonitasoft.studio.common.emf.tools.ExpressionHelper;
......@@ -30,31 +25,22 @@ import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.condition.conditionModel.ConditionModelPackage;
import org.bonitasoft.studio.condition.conditionModel.Expression_ProcessRef;
import org.bonitasoft.studio.condition.conditionModel.Operation_Compare;
import org.bonitasoft.studio.condition.scoping.ConditionModelGlobalScopeProvider;
import org.bonitasoft.studio.condition.ui.internal.ConditionModelActivator;
import org.bonitasoft.studio.expression.editor.ExpressionEditorPlugin;
import org.bonitasoft.studio.expression.editor.provider.IExpressionValidator;
import org.bonitasoft.studio.model.expression.Expression;
import org.bonitasoft.studio.model.expression.ExpressionPackage;
import org.bonitasoft.studio.model.parameter.Parameter;
import org.bonitasoft.studio.model.process.AbstractProcess;
import org.bonitasoft.studio.model.process.Data;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.resource.SynchronizedXtextResourceSet;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.StringInputStream;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;
......@@ -76,38 +62,19 @@ public class ComparisonExpressionValidator implements IExpressionValidator {
*/
@Override
public IStatus validate(final Object value) {
if(value == null || value.toString().isEmpty() || !ExpressionConstants.CONDITION_TYPE.equals(inputExpression.getType())){
return ValidationStatus.ok();
}
final Injector injector = ConditionModelActivator.getInstance().getInjector(ConditionModelActivator.ORG_BONITASOFT_STUDIO_CONDITION_CONDITIONMODEL);
final IResourceValidator xtextResourceChecker = injector.getInstance(IResourceValidator.class);
final ResourceSet resourceSet = new SynchronizedXtextResourceSet();
final XtextResource resource = (XtextResource) resourceSet.createResource(URI.createURI("somefile.cmodel"));
final Map<String, String> options = new HashMap<String, String>();
options.put(XtextResource.OPTION_ENCODING, "UTF-8");
try {
resource.load(new StringInputStream(value.toString(), "UTF-8"), options);
} catch (final UnsupportedEncodingException e1) {
BonitaStudioLog.error(e1, ExpressionEditorPlugin.PLUGIN_ID);
} catch (final IOException e1) {
BonitaStudioLog.error(e1, ExpressionEditorPlugin.PLUGIN_ID);
}
final ConditionModelGlobalScopeProvider globalScopeProvider = injector.getInstance(ConditionModelGlobalScopeProvider.class);
final List<String> accessibleObjects = new ArrayList<String>();
for(final Data d : ModelHelper.getAccessibleData(context)){
accessibleObjects.add(ModelHelper.getEObjectID(d));
}
final AbstractProcess process = ModelHelper.getParentProcess(context);
if(process != null){
for(final Parameter p : process.getParameters()){
accessibleObjects.add(ModelHelper.getEObjectID(p));
}
}
globalScopeProvider.setAccessibleEObjects(accessibleObjects);
Resource resource = null;
try {
resource = new XtextComparisonExpressionLoader(injector).loadResource(value.toString(), context);
} catch (final ComparisonExpressionLoadException e) {
BonitaStudioLog.error(e);
return ValidationStatus.error(e.getMessage());
}
final IResourceValidator xtextResourceChecker = injector.getInstance(IResourceValidator.class);
final MultiStatus status = new MultiStatus(ExpressionEditorPlugin.PLUGIN_ID, 0, "", null);
final List<Issue> issues = xtextResourceChecker.validate(resource, CheckMode.FAST_ONLY, null);
......@@ -127,7 +94,7 @@ public class ComparisonExpressionValidator implements IExpressionValidator {
return status;
}
private void updateDependencies(final XtextResource resource) {
private void updateDependencies(final Resource resource) {
if(domain != null && inputExpression != null){
domain.getCommandStack().execute(new RemoveCommand(domain, inputExpression, ExpressionPackage.Literals.EXPRESSION__REFERENCED_ELEMENTS, inputExpression.getReferencedElements()));
final Operation_Compare compareOp = (Operation_Compare) resource.getContents().get(0);
......@@ -153,7 +120,7 @@ public class ComparisonExpressionValidator implements IExpressionValidator {
private EObject getResolvedDependency(final Expression_ProcessRef ref) {
EObject dep = resolveProxy(ref.getValue());
EObject dep = ref.getValue();
final List<EObject> orignalDep = ModelHelper.getAllItemsOfType( ModelHelper.getMainProcess(context), dep.eClass());
for(final EObject d : orignalDep){
if(EcoreUtil.equals(dep, d)){
......@@ -164,18 +131,6 @@ public class ComparisonExpressionValidator implements IExpressionValidator {
return dep;
}
private EObject resolveProxy(final EObject ref) {
ResourceSet rSet = null;
if(ref.eIsProxy()){
rSet =context.eResource().getResourceSet();
}
final EObject dep = EcoreUtil2.resolve(ref, rSet);
if(rSet != null){
rSet.getResources().remove(ref.eResource());
}
return dep;
}
@Override
public void setInputExpression(final Expression inputExpression) {
this.inputExpression = inputExpression;
......
......@@ -56,23 +56,7 @@ public class XtextComparisonExpressionLoader {
}
public Operation_Compare loadConditionExpression(final String comparisonExpression, final EObject context) throws ComparisonExpressionLoadException {
final XtextResourceSetProvider xtextResourceSetProvider = injector.getInstance(XtextResourceSetProvider.class);
final IProject project = RepositoryManager.getInstance().getCurrentRepository().getProject();
final ResourceSet resourceSet = xtextResourceSetProvider.get(project);
IFile file;
try {
file = createTmpFile(comparisonExpression, project);
} catch (final UnsupportedEncodingException e) {
throw new ComparisonExpressionLoadException("Failed to create a temporary file for comparison expression " + comparisonExpression, e);
} catch (final CoreException e) {
throw new ComparisonExpressionLoadException("Failed to create a temporary file for comparison expression " + comparisonExpression, e);
}
final Resource resource = resourceSet.getResource(URI.createPlatformResourceURI(file.getFullPath().toOSString(), true), true);
final ConditionModelGlobalScopeProvider globalScopeProvider = injector.getInstance(ConditionModelGlobalScopeProvider.class);
globalScopeProvider.setAccessibleEObjects(getAccessibleReferences(context));
//Resolve reference proxies
EcoreUtil2.resolveLazyCrossReferences(resource, CancelIndicator.NullImpl);
final Resource resource = loadResource(comparisonExpression, context);
final EList<EObject> contents = resource.getContents();
if (contents.isEmpty()) {
throw new ComparisonExpressionLoadException("Failed to laod comparison expression " + comparisonExpression);
......@@ -85,8 +69,10 @@ public class XtextComparisonExpressionLoader {
if (file.exists()) {
file.delete(true, null);
}
final InputStream is = new StringInputStream(content, "UTF-8");
file.create(is, true, null);
if (content != null && !content.isEmpty()) {
final InputStream is = new StringInputStream(content, "UTF-8");
file.create(is, true, null);
}
return file;
}
......@@ -104,4 +90,38 @@ public class XtextComparisonExpressionLoader {
return accessibleObjects;
}
public Resource loadResource(final String comparisonExpression, final EObject context) throws ComparisonExpressionLoadException {
final XtextResourceSetProvider xtextResourceSetProvider = injector.getInstance(XtextResourceSetProvider.class);
final IProject project = RepositoryManager.getInstance().getCurrentRepository().getProject();
final ResourceSet resourceSet = xtextResourceSetProvider.get(project);
if (context != null && context.eResource() != null) {
resourceSet.getResources().add(context.eResource());
}
IFile file;
try {
file = createTmpFile(comparisonExpression, project);
} catch (final UnsupportedEncodingException e) {
throw new ComparisonExpressionLoadException("Failed to create a temporary file for comparison expression " + comparisonExpression, e);
} catch (final CoreException e) {
throw new ComparisonExpressionLoadException("Failed to create a temporary file for comparison expression " + comparisonExpression, e);
}
Resource resource = null;
if (file.exists()) {
resource = resourceSet.getResource(URI.createPlatformResourceURI(file.getFullPath().toOSString(), true), true);
} else {
resource = resourceSet.createResource(URI.createPlatformResourceURI(file.getFullPath().toOSString(), true));
}
final ConditionModelGlobalScopeProvider globalScopeProvider = injector.getInstance(ConditionModelGlobalScopeProvider.class);
globalScopeProvider.setAccessibleEObjects(getAccessibleReferences(context));
if (comparisonExpression != null && !comparisonExpression.isEmpty()) {
//Resolve reference proxies
EcoreUtil2.resolveLazyCrossReferences(resource, CancelIndicator.NullImpl);
}
return resource;
}
}
......@@ -26,7 +26,9 @@ Require-Bundle: org.eclipse.xtext;visibility:=reexport,
com.google.guava,
org.eclipse.xtext.xbase.lib,
org.junit;bundle-version="4.11.0";resolution:=optional,
assertj-core;bundle-version="1.5.0";resolution:=optional
assertj-core;bundle-version="1.5.0";resolution:=optional,
org.bonitasoft.studio.diagram.custom;bundle-version="6.4.0",
org.bonitasoft.studio.common.repository;bundle-version="6.4.0"
Import-Package: org.apache.log4j,
org.eclipse.xtext.xbase.lib
Bundle-RequiredExecutionEnvironment: J2SE-1.5
......
......@@ -20,13 +20,7 @@ import org.bonitasoft.studio.model.process.JavaObjectData;
import org.bonitasoft.studio.model.process.LongType;
import org.bonitasoft.studio.model.process.StringType;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.impl.RootNode;
import org.eclipse.xtext.validation.Check;
......@@ -101,16 +95,15 @@ public class ConditionModelJavaValidator extends AbstractConditionModelJavaValid
} else {
if (!e1Type.equals(e2Type)) {
return Messages.incompatibleTypes;
}
}
return null;
}
private String getDataType(final Expression_ProcessRef e) {
final EObject data = resolveProxyReferenceOnCurrentResourceSet(e);
if (data instanceof JavaObjectData) {
final JavaObjectData javaData = (JavaObjectData) data;
final EObject reference = e.getValue();
if (reference instanceof JavaObjectData) {
final JavaObjectData javaData = (JavaObjectData) reference;
final String className = javaData.getClassName();
if (className.equals(Boolean.class.getName())) {
return ConditionModelPackage.Literals.EXPRESSION_BOOLEAN.getName();
......@@ -131,8 +124,8 @@ public class ConditionModelJavaValidator extends AbstractConditionModelJavaValid
return ConditionModelPackage.Literals.EXPRESSION_DOUBLE.getName();
}
} else {
if (data instanceof Data) {
final DataType type = ((Data) data).getDataType();
if (reference instanceof Data) {
final DataType type = ((Data) reference).getDataType();
if (type instanceof BooleanType) {
return ConditionModelPackage.Literals.EXPRESSION_BOOLEAN.getName();
}
......@@ -154,8 +147,8 @@ public class ConditionModelJavaValidator extends AbstractConditionModelJavaValid
if (type instanceof EnumType) {
return ConditionModelPackage.Literals.EXPRESSION_STRING.getName();
}
} else if (data instanceof Parameter) {
final String type = ((Parameter) data).getTypeClassname();
} else if (reference instanceof Parameter) {
final String type = ((Parameter) reference).getTypeClassname();
if (Boolean.class.getName().equals(type)) {
return ConditionModelPackage.Literals.EXPRESSION_BOOLEAN.getName();
}
......@@ -180,26 +173,4 @@ public class ConditionModelJavaValidator extends AbstractConditionModelJavaValid
}
protected EObject resolveProxyReferenceOnCurrentResourceSet(
final Expression_ProcessRef e) {
final EObject proxy = (EObject) e.eGet(ConditionModelPackage.Literals.EXPRESSION_PROCESS_REF__VALUE , false);
final ResourceSet rSet = getCurrentResourceSet(proxy);
final EObject data = EcoreUtil2.resolve(proxy, rSet);
e.setValue(data);
if (rSet != null) {
rSet.getResources().remove(e.eResource());
}
return data;
}
private ResourceSet getCurrentResourceSet(final EObject proxy) {
final ResourceSetImpl resourceSetImpl = new ResourceSetImpl();
final URI uri = EcoreUtil.getURI(proxy);
if (uri != null && uri.toString().contains(".proc")) {
final Resource resource = resourceSetImpl.getResource(uri.trimFragment(), true);
return resource.getResourceSet();
}
return null;
}
}
......@@ -173,7 +173,6 @@ public class DiagramFileStore extends EMFFileStore implements IRepositoryFileSto
if(editor instanceof ProcessDiagramEditor){
return (DiagramEditor) editor;
}
}
} catch (final PartInitException e) {
BonitaStudioLog.error(e,Activator.PLUGIN_ID);
......
......@@ -57,25 +57,25 @@ public class TestConditionExpression extends SWTBotGefTestCase implements SWTBot
SWTBotTestUtil.selectTabbedPropertyView(bot, "Data");
bot.button(Messages.Add).click();
SWTBotTestUtil.addNewData(bot, "myData","Text" , false, null);
bot.button(Messages.Add).click();
SWTBotTestUtil.addNewData(bot, "myData2","Boolean" , false, null);
SWTBotTestUtil.configureSequenceFlow(bot, "testCondition", poolName, false, "", ExpressionConstants.CONDITION_TYPE);
bot.button(IDialogConstants.OK_LABEL).click();
testValidConditionExpression("1234555<122233445",ExpressionConstants.CONDITION_TYPE);
testValidConditionExpression("123444.345345==324234324.23423432",ExpressionConstants.CONDITION_TYPE);
testValidConditionExpression("123.567777>=234234234324",ExpressionConstants.CONDITION_TYPE);
testValidConditionExpression("myData<\"myString\"",ExpressionConstants.CONDITION_TYPE);
testValidConditionExpression("!myData2",ExpressionConstants.CONDITION_TYPE);
testValidConditionExpression("myData2",ExpressionConstants.CONDITION_TYPE);
testValidConditionExpression("myData2", ExpressionConstants.CONDITION_TYPE);
testUnvalidConditionExpression("myData != true",ExpressionConstants.CONDITION_TYPE,true,true);
testUnvalidConditionExpression("myData",ExpressionConstants.CONDITION_TYPE,true,true);
testValidConditionExpression("\"myString\"<\"myString1\"",ExpressionConstants.CONDITION_TYPE);
testUnvalidConditionExpression("myString<\"myString1\"",ExpressionConstants.CONDITION_TYPE,true,false);
testUnvalidConditionExpression("\"myString\"<myString1",ExpressionConstants.CONDITION_TYPE,true,true);
......@@ -99,7 +99,7 @@ public class TestConditionExpression extends SWTBotGefTestCase implements SWTBot
} else {
assertFalse(validErrorMessage+" expression = "+condition,styles.underline);
}
bot.button(IDialogConstants.CANCEL_LABEL).click();
bot.button(IDialogConstants.CANCEL_LABEL).click();
}
private void testValidConditionExpression(final String condition, final String expressionType) throws OperationCanceledException, InterruptedException{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment