| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.ide.core.ui.softwarefactory.extender; |
| |
| import java.io.IOException; |
| import java.text.Normalizer; |
| import java.util.Iterator; |
| import java.util.List; |
| |
| import org.eclipse.emf.common.util.Diagnostic; |
| import org.eclipse.emf.ecore.EClass; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.util.Diagnostician; |
| import org.eclipse.osbp.xtext.addons.EObjectHelper; |
| import org.eclipse.xtext.naming.QualifiedName; |
| import org.eclipse.xtext.resource.IEObjectDescription; |
| import org.eclipse.xtext.resource.IResourceDescription; |
| import org.eclipse.xtext.resource.IResourceDescriptions; |
| import org.eclipse.xtext.resource.SaveOptions; |
| import org.eclipse.xtext.resource.XtextResource; |
| import org.eclipse.xtext.resource.XtextResourceSet; |
| import org.eclipse.xtext.ui.resource.XtextResourceSetProvider; |
| import org.eclipse.xtext.validation.FeatureBasedDiagnostic; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class ModelExtenderUtils { |
| private ModelExtenderUtils() { |
| } |
| |
| public enum DataType { |
| NONE("none", null, null), DOUBLE("double", null, null), DOUBLECOLON("double", "###.00", "de_DE"), INT("int", |
| null, null), UUID("uuid", null, null), TEXT("String", null, |
| null), DATE("Date", "yyyyMMdd", null), BOOLEAN("boolean", null, null), |
| // wora 15.12.2017 missing datatype |
| BLOBMAPPING("BlobMapping", null, null); |
| |
| private String value; |
| private String format; |
| private String locale; |
| |
| private DataType(String value, String format, String locale) { |
| this.value = value; |
| this.format = format; |
| this.locale = locale; |
| } |
| |
| private DataType(DataType type) { |
| this.value = type.value; |
| this.format = type.format; |
| this.locale = type.locale; |
| } |
| |
| public String getValue() { |
| return value; |
| } |
| |
| public String getFormat() { |
| return format; |
| } |
| |
| public void setFormat(String format) { |
| this.format = format; |
| } |
| |
| public String getLocale() { |
| return locale; |
| } |
| |
| } |
| |
| private static final Logger LOGGER = LoggerFactory.getLogger(ModelExtenderUtils.class); |
| |
| // persist the modified model |
| public static void writeModel(EObject model, String id) { |
| writeResource(model.eResource(), id); |
| } |
| |
| // persist the modified resource |
| public static void writeResource(Resource resource, String id) { |
| StringBuilder errors = new StringBuilder(); |
| try { |
| LOGGER.info("running diagnostic for {}", resource.getURI()); |
| XtextResource xR = (XtextResource) resource; |
| if (xR.getErrors().isEmpty()) { |
| LOGGER.info("writing {}", id); |
| resource.save(SaveOptions.newBuilder().format().getOptions().toOptionsMap()); |
| } else { |
| for(org.eclipse.emf.ecore.resource.Resource.Diagnostic error:xR.getErrors()) { |
| errors.append(error.toString()); |
| errors.append("\n"); |
| } |
| LOGGER.error("****************************"); |
| LOGGER.error("model {} diagnosic error {}", resource.getURI(), errors); |
| LOGGER.error("****************************"); |
| } |
| LOGGER.info("finished writing {}", resource.getURI()); |
| } catch (IOException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| // get a name without characters from locale anomaly or punctuation, replace |
| // blanks with underscores |
| // avoid some keywords |
| public static String normalizeName(String name) { |
| String normalized = Normalizer.normalize(name, Normalizer.Form.NFKD).toLowerCase().replaceAll("[^a-z0-9 ]", "") |
| .replace(" ", "_"); |
| if (normalized.equalsIgnoreCase("description")) { |
| normalized = "_" + normalized; |
| } |
| return normalized; |
| } |
| |
| // get an EObject from a full qualified datatype name |
| public static EObject findAllEObject(IResourceDescriptions descs, EClass type, String fqn) { |
| Iterable<IEObjectDescription> result = descs.getExportedObjects(type, QualifiedName.create(fqn.split("\\.")), |
| true); |
| if (result.iterator().hasNext()) { |
| IEObjectDescription desc = result.iterator().next(); |
| return desc.getEObjectOrProxy(); |
| } |
| return null; |
| } |
| |
| // get an EObject list |
| public static boolean findAllEObjects(IResourceDescriptions descs, EClass type, List<EObject> objects) { |
| Iterable<IEObjectDescription> result = descs.getExportedObjectsByType(type); |
| Iterator<IEObjectDescription> iter = result.iterator(); |
| while (iter.hasNext()) { |
| IEObjectDescription desc = iter.next(); |
| objects.add(desc.getEObjectOrProxy()); |
| } |
| return !objects.isEmpty(); |
| } |
| |
| // get an EObject from a full qualified datatype name |
| public static EObject findEObject(IResourceDescription descs, EClass type, String fqn) { |
| Iterable<IEObjectDescription> result = descs.getExportedObjects(type, QualifiedName.create(fqn.split("\\.")), |
| true); |
| if (result.iterator().hasNext()) { |
| IEObjectDescription desc = result.iterator().next(); |
| return desc.getEObjectOrProxy(); |
| } |
| return null; |
| } |
| |
| // get all EObjects from a class |
| public static boolean findEObjects(IResourceDescription descs, EClass type, List<EObject> objects) { |
| Iterable<IEObjectDescription> result = descs.getExportedObjectsByType(type); |
| Iterator<IEObjectDescription> iter = result.iterator(); |
| while (iter.hasNext()) { |
| IEObjectDescription desc = iter.next(); |
| objects.add(desc.getEObjectOrProxy()); |
| } |
| return !objects.isEmpty(); |
| } |
| |
| /** |
| * Returns the model which is to be modified, identified by projectPath. |
| * |
| * @param projectPath |
| * @return |
| */ |
| public static <A extends EObject> A loadSemanticModel(ModelInstanceDescription packageDelta, |
| XtextResourceSetProvider provider) { |
| LOGGER.info("loading:" + packageDelta.getProject().getName()); |
| XtextResourceSet rs = (XtextResourceSet) provider.get(packageDelta.getProject()); |
| Resource modelResource = rs.getResource(packageDelta.getModelURI(), true); |
| try { |
| modelResource.load(null); |
| } catch (IOException e) { |
| LOGGER.error(e.getLocalizedMessage()); |
| return null; |
| } |
| @SuppressWarnings("unchecked") |
| A model = (A) EObjectHelper.getSemanticElement(modelResource); |
| LOGGER.info("finished loading:" + packageDelta.getProject().getName()); |
| return model; |
| } |
| } |