com.reprezen.genflow.rapidml.xsd.ResourceHelper Maven / Gradle / Ivy
/**
* Copyright © 2013, 2016 Modelsolv, Inc.
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property
* of ModelSolv, Inc. See the file license.html in the root directory of
* this project for further information.
*/
package com.reprezen.genflow.rapidml.xsd;
import com.google.common.base.Objects;
import com.reprezen.genflow.rapidml.xsd.FeatureHelper;
import com.reprezen.genflow.rapidml.xsd.Helpers;
import com.reprezen.genflow.rapidml.xsd.XMLSchemaHelper;
import com.reprezen.rapidml.ObjectResource;
import com.reprezen.rapidml.ReferenceLink;
import com.reprezen.rapidml.ReferenceProperty;
import com.reprezen.rapidml.ResourceAPI;
import com.reprezen.rapidml.ResourceDefinition;
import com.reprezen.rapidml.ServiceDataResource;
import com.reprezen.rapidml.Structure;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
@SuppressWarnings("all")
public class ResourceHelper {
@Extension
private XMLSchemaHelper xmlSchemaHelper;
@Extension
private FeatureHelper featureHelper;
public ResourceHelper(final Helpers helpers) {
XMLSchemaHelper _xmlSchemaHelper = helpers.getXmlSchemaHelper();
this.xmlSchemaHelper = _xmlSchemaHelper;
FeatureHelper _featureHelper = helpers.getFeatureHelper();
this.featureHelper = _featureHelper;
}
public ObjectResource getCorrespondingResource(final Structure complexType, final ResourceAPI resourceAPI) {
ObjectResource _xblockexpression = null;
{
final ObjectResource defaultResource = this.getDefaultResource(complexType, resourceAPI);
ObjectResource _xifexpression = null;
if ((defaultResource != null)) {
_xifexpression = defaultResource;
} else {
ObjectResource _xblockexpression_1 = null;
{
final ServiceDataResource onlyResource = this.getOnlyResource(complexType, resourceAPI);
ObjectResource _xifexpression_1 = null;
if ((onlyResource != null)) {
_xifexpression_1 = defaultResource;
} else {
_xifexpression_1 = null;
}
_xblockexpression_1 = _xifexpression_1;
}
_xifexpression = _xblockexpression_1;
}
_xblockexpression = _xifexpression;
}
return _xblockexpression;
}
private ObjectResource getDefaultResource(final Structure complexType, final ResourceAPI resourceAPI) {
ObjectResource _xblockexpression = null;
{
Iterable _allResources = this.getAllResources(complexType, resourceAPI);
final Function1 _function = (ObjectResource it) -> {
return Boolean.valueOf(it.isDefault());
};
final Iterable resources = IterableExtensions.filter(_allResources, _function);
ObjectResource _xifexpression = null;
boolean _isNullOrEmpty = IterableExtensions.isNullOrEmpty(resources);
if (_isNullOrEmpty) {
_xifexpression = null;
} else {
final Function1 _function_1 = (ObjectResource it) -> {
return null;
};
_xifexpression = IterableExtensions.findFirst(resources, _function_1);
}
_xblockexpression = _xifexpression;
}
return _xblockexpression;
}
private ServiceDataResource getOnlyResource(final Structure complexType, final ResourceAPI resourceAPI) {
ObjectResource _xblockexpression = null;
{
final Iterable resources = this.getAllResources(complexType, resourceAPI);
ObjectResource _xifexpression = null;
int _size = IterableExtensions.size(resources);
boolean _equals = (_size == 1);
if (_equals) {
final Function1 _function = (ObjectResource it) -> {
return null;
};
_xifexpression = IterableExtensions.findFirst(resources, _function);
} else {
_xifexpression = null;
}
_xblockexpression = _xifexpression;
}
return _xblockexpression;
}
public ServiceDataResource getTargetResource(final ReferenceProperty feature, final ServiceDataResource dataResource) {
ServiceDataResource _xblockexpression = null;
{
EList _referenceLinks = dataResource.getReferenceLinks();
final ReferenceLink referenceLink = this.featureHelper.getReferenceLink(feature, ((Iterable) _referenceLinks));
ServiceDataResource _xifexpression = null;
if ((referenceLink != null)) {
ResourceDefinition _targetResource = referenceLink.getTargetResource();
_xifexpression = ((ServiceDataResource) _targetResource);
} else {
Structure _type = feature.getType();
ResourceAPI _interface = this.getInterface(dataResource);
_xifexpression = this.getCorrespondingResource(_type, _interface);
}
_xblockexpression = _xifexpression;
}
return _xblockexpression;
}
private Iterable getAllResources(final Structure complexType, final ResourceAPI resourceAPI) {
EList _ownedResourceDefinitions = resourceAPI.getOwnedResourceDefinitions();
final Function1 _function = (ResourceDefinition it) -> {
return ((ObjectResource) it);
};
List _map = ListExtensions.map(_ownedResourceDefinitions, _function);
final Function1 _function_1 = (ObjectResource it) -> {
Structure _dataType = it.getDataType();
return Boolean.valueOf(Objects.equal(_dataType, complexType));
};
return IterableExtensions.filter(_map, _function_1);
}
public ResourceAPI getInterface(final ServiceDataResource dataResource) {
ResourceAPI _xifexpression = null;
if ((dataResource == null)) {
_xifexpression = null;
} else {
_xifexpression = this.xmlSchemaHelper.getEContainer(dataResource, ResourceAPI.class);
}
return _xifexpression;
}
}