/** * Converts a primitive type value, this implementation only converts an EEnum to an Enum value. * * @param value the value to convert * @param eDataType its EDataType * @return the converted value */ @SuppressWarnings({"unchecked", "rawtypes"}) protected Object convertEAttributeValue(final Object value, final EDataType eDataType) { if (value instanceof Enum<?>) { final EDataType enumDataType = getDataTypeOrBaseType(eDataType); Check.isInstanceOf(enumDataType, EEnum.class); final ModelPackage modelPackage = ModelResolver.getInstance().getModelPackage(enumDataType.getEPackage().getNsURI()); final Class<? extends Enum> enumClass = (Class<? extends Enum>) modelPackage.getEClassifierClass(enumDataType); return Enum.valueOf(enumClass, ((Enum<?>) value).name().toUpperCase(Locale.ENGLISH)); } else if (value instanceof EEnumLiteral) { final EDataType enumDataType = getDataTypeOrBaseType(eDataType); Check.isInstanceOf(enumDataType, EEnum.class); final EEnumLiteral eeNumLiteral = (EEnumLiteral) value; final ModelPackage modelPackage = ModelResolver.getInstance().getModelPackage(enumDataType.getEPackage().getNsURI()); if (modelPackage == null) { // dynamic model return eeNumLiteral; } final Class<? extends Enum> enumClass = (Class<? extends Enum>) modelPackage.getEClassifierClass(enumDataType); return Enum.valueOf(enumClass, eeNumLiteral.getName().toUpperCase(Locale.ENGLISH)); } // convert these to a Date always if (value instanceof XMLGregorianCalendar) { final XMLGregorianCalendar xmlCalendar = (XMLGregorianCalendar) value; final Date date = xmlCalendar.toGregorianCalendar().getTime(); return date; } return value; }
/** * Initializes this {@link ModelPackage}. * <!-- begin-user-doc --> * <!-- end-user-doc --> * * @return an initialized instance of this class * @generated */ public static IncludeModelPackage initialize() { if (isInitialized) { return (IncludeModelPackage) ModelResolver.getInstance().getModelPackage(NS_URI); } final IncludeModelPackage modelPackage = new IncludeModelPackage(); ModelResolver.getInstance().registerModelPackage(modelPackage); // read the model from the ecore file, the EPackage is registered in the EPackage.Registry // see the ModelResolver getEPackageRegistry method ModelUtils.readEPackagesFromFile(modelPackage); isInitialized = true; IdentifiableModelPackage.initialize(); // force the initialization of the EFactory proxy modelPackage.getEPackage(); // register the relation between a Class and its EClassifier ModelResolver.getInstance() .registerClassModelMapping(Include1.class, modelPackage.getInclude1EClass(), modelPackage); ModelResolver.getInstance() .registerClassModelMapping(Include2.class, modelPackage.getInclude2EClass(), modelPackage); DaoRegistry.getInstance().registerDao(Include1.class, Include1Dao.class); DaoRegistry.getInstance().registerDao(Include2.class, Include2Dao.class); // and return ourselves return modelPackage; }
/** * Converts the values of an FeatureMap, the values of the collection are converted to and added * to the list in the correct feature in the modelObject. * * @param eObject the eObject from which the value is read * @param modelObject the {@link ModelObject} in which the value is to be set * @param eFeature the eFeature which is converted */ protected void convertFeatureMap( final EObject eObject, final ModelObject<?> modelObject, final EStructuralFeature eFeature) { final Collection<?> eValues = (Collection<?>) eObject.eGet(eFeature); @SuppressWarnings("unchecked") final Collection<Object> values = (Collection<Object>) modelObject.eGet(eFeature); // clear as the object may have been read from the db values.clear(); for (final Object eValue : eValues) { final FeatureMap.Entry eEntry = (FeatureMap.Entry) eValue; final Object featureMapEntry = ModelResolver.getInstance().createFeatureMapEntry(eFeature); final ModelFeatureMapEntry<?> mEntry = ModelResolver.getInstance().getModelFeatureMapEntry(eFeature, featureMapEntry); mEntry.setEStructuralFeature(eEntry.getEStructuralFeature()); final Object convertedValue; if (mEntry.getEStructuralFeature() instanceof EAttribute) { convertedValue = convertEAttributeValue( eEntry.getValue(), ((EAttribute) eEntry.getEStructuralFeature()).getEAttributeType()); } else { convertedValue = createTarget((EObject) eEntry.getValue()); } mEntry.setValue(convertedValue); values.add(featureMapEntry); } }
/** * Converts the values of an FeatureMap, the values of the collection are converted to and added * to the list in the correct feature in the modelObject. * * @param eObject the eObject from which the value is read * @param modelObject the {@link ModelObject} in which the value is to be set * @param eFeature the eFeature which is converted */ protected void convertFeatureMap( final ModelObject<?> modelObject, final EObject eObject, final EStructuralFeature eFeature) { final Collection<?> mValues = (Collection<?>) modelObject.eGet(eFeature); @SuppressWarnings("unchecked") final Collection<Object> values = (Collection<Object>) eObject.eGet(eFeature); for (final Object mValue : mValues) { final ModelFeatureMapEntry<?> mEntry = ModelResolver.getInstance().getModelFeatureMapEntry(eFeature, mValue); EStructuralFeature entryFeature = mEntry.getEStructuralFeature(); Object entryValue = mEntry.getValue(); // flatten the tree of feature map entries, feature maps maybe nested // EMF uses a flattened api, while Texo builds this actual tree of // featuremaps, the findFeature and findValue find the deepest featureMap entry if (FeatureMapUtil.isFeatureMap(entryFeature)) { final ModelFeatureMapEntry<?> modelFeatureMapEntry = ModelResolver.getInstance().getModelFeatureMapEntry(entryFeature, entryValue); entryFeature = ModelUtils.findFeature(modelFeatureMapEntry); entryValue = ModelUtils.findValue(modelFeatureMapEntry); } final Object convertedValue; if (entryFeature instanceof EAttribute) { convertedValue = convertEAttributeValue(entryValue, ((EAttribute) entryFeature).getEAttributeType()); } else { convertedValue = createTarget(entryValue); } final FeatureMap.Entry eEntry = FeatureMapUtil.createEntry(entryFeature, convertedValue); values.add(eEntry); } }
public void repair() { final Object target = objectMapping.get(owner); final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(target); final Object listObject = modelObject.eGet(eReference); if (!(listObject instanceof List<?>)) { // no order maintained anyway return; } final EList<?> eList = (EList<?>) owner.eGet(eReference); @SuppressWarnings("unchecked") final List<Object> list = (List<Object>) listObject; int correctIndex = 0; for (Object objectElement : eList) { final EObject eObjectElement = (EObject) objectElement; final Object element = objectMapping.get(eObjectElement); final int newIndex = list.indexOf(element); if (newIndex != correctIndex) { try { list.remove(element); list.add(correctIndex, element); } catch (UnsupportedOperationException ignore) { // unmodifiable collection, can't handle those, go away return; } } correctIndex++; } }
protected String getObjectUrlPart(Object object) { final Identifiable identifiable = (Identifiable) object; final ModelObject<?> valueModelObject = ModelResolver.getInstance().getModelObject(object); return ModelUtils.getQualifiedNameFromEClass(valueModelObject.eClass()) + "/" + identifiable.getDb_Id(); // $NON-NLS-1$ }
private ModelObject<?> getModelObject(Object target) { if (target == lastObject) { return lastModelObject; } lastObject = target; lastModelObject = ModelResolver.getInstance().getModelObject(target); return lastModelObject; }
/** * Initializes this {@link ModelPackage}. * <!-- begin-user-doc --> * <!-- end-user-doc --> * * @return an initialized instance of this class * @generated */ public static ListunionModelPackage initialize() { if (isInitialized) { return (ListunionModelPackage) ModelResolver.getInstance().getModelPackage(NS_URI); } final ListunionModelPackage modelPackage = new ListunionModelPackage(); ModelResolver.getInstance().registerModelPackage(modelPackage); // read the model from the ecore file, the EPackage is registered in the EPackage.Registry // see the ModelResolver getEPackageRegistry method ModelUtils.readEPackagesFromFile(modelPackage); isInitialized = true; IdentifiableModelPackage.initialize(); // force the initialization of the EFactory proxy modelPackage.getEPackage(); // register the relation between a Class and its EClassifier ModelResolver.getInstance() .registerClassModelMapping( StatesByCountry.class, modelPackage.getStatesByCountryEClass(), modelPackage); ModelResolver.getInstance() .registerClassModelMapping( SimpleUnionTypeMember1.class, modelPackage.getSimpleUnionTypeMember1EEnum(), modelPackage); DaoRegistry.getInstance().registerDao(StatesByCountry.class, StatesByCountryDao.class); // and return ourselves return modelPackage; }
protected void convertContent(EObject eObject) { final Object target = objectMapping.get(eObject); final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(target); for (final EStructuralFeature eStructuralFeature : modelObject.eClass().getEAllStructuralFeatures()) { if (!eStructuralFeature.isChangeable() || eStructuralFeature.isVolatile()) { continue; } // not set, not convert if (!eStructuralFeature.isMany() && !eObject.eIsSet(eStructuralFeature)) { continue; } if (FeatureMapUtil.isFeatureMap(eStructuralFeature)) { convertFeatureMap(eObject, modelObject, eStructuralFeature); } else if (eStructuralFeature.isMany()) { if (eStructuralFeature instanceof EAttribute) { final EAttribute eAttribute = (EAttribute) eStructuralFeature; convertManyEAttribute(eObject, modelObject, eAttribute); } else { final EReference eReference = (EReference) eStructuralFeature; if (eReference.isContainer()) { continue; } convertManyEReference(eObject, modelObject, eReference); } } else { if (eStructuralFeature instanceof EAttribute) { final EAttribute eAttribute = (EAttribute) eStructuralFeature; convertSingleEAttribute(eObject, modelObject, eAttribute); } else { final EReference eReference = (EReference) eStructuralFeature; if (eReference.isContainer()) { continue; } convertSingleEReference(eObject, modelObject, eReference); } } } }
public void repair() { final EObject eObjectOwner = objectMapping.get(owner.getTarget()); final ModelObject<?> modelObjectOwner = ModelResolver.getInstance().getModelObject(owner); final Object listObject = modelObjectOwner.eGet(eReference); if (!(listObject instanceof List<?>)) { // no order maintained anyway return; } final EList<?> eList = (EList<?>) eObjectOwner.eGet(eReference); final List<?> list = (List<?>) listObject; int currentIndex = 0; for (Object objectElement : list) { final EObject eObjectElement = objectMapping.get(objectElement); final int newIndex = eList.indexOf(eObjectElement); if (newIndex != currentIndex) { eList.move(currentIndex, newIndex); } currentIndex++; } }
protected void convertContent(Object target) { // if a proxy then do no feature conversions as this may load // the object InternalEObject eObject = objectMapping.get(target); final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(target); if (getProxyObjects().contains(target)) { final URI proxyURI = getProxyId(modelObject); if (proxyURI != null) { eObject.eSetProxyURI(proxyURI); } return; } for (final EStructuralFeature eStructuralFeature : eObject.eClass().getEAllStructuralFeatures()) { if (!eStructuralFeature.isChangeable() || eStructuralFeature.isVolatile()) { continue; } if (FeatureMapUtil.isFeatureMap(eStructuralFeature)) { convertFeatureMap(modelObject, eObject, eStructuralFeature); } else if (eStructuralFeature.isMany()) { if (eStructuralFeature instanceof EAttribute) { final EAttribute eAttribute = (EAttribute) eStructuralFeature; convertManyEAttribute(modelObject, eObject, eAttribute); } else { final EReference eReference = (EReference) eStructuralFeature; convertManyEReference(modelObject, eObject, eReference); } } else { if (eStructuralFeature instanceof EAttribute) { final EAttribute eAttribute = (EAttribute) eStructuralFeature; convertSingleEAttribute(modelObject, eObject, eAttribute); } else { final EReference eReference = (EReference) eStructuralFeature; convertSingleEReference(modelObject, eObject, eReference); } } } }
/** * Initializes this {@link ModelPackage}. * <!-- begin-user-doc --> * <!-- end-user-doc --> * * @return an initialized instance of this class * @generated */ public static AnytypeModelPackage initialize() { if (isInitialized) { return (AnytypeModelPackage) ModelResolver.getInstance().getModelPackage(NS_URI); } final AnytypeModelPackage modelPackage = new AnytypeModelPackage(); ModelResolver.getInstance().registerModelPackage(modelPackage); // read the model from the ecore file, the EPackage is registered in the EPackage.Registry // see the ModelResolver getEPackageRegistry method ModelUtils.readEPackagesFromFile(modelPackage); isInitialized = true; IdentifiableModelPackage.initialize(); // force the initialization of the EFactory proxy modelPackage.getEPackage(); // register the relation between a Class and its EClassifier ModelResolver.getInstance() .registerClassModelMapping(A.class, modelPackage.getAEClass(), modelPackage); ModelResolver.getInstance() .registerClassModelMapping(B.class, modelPackage.getBEClass(), modelPackage); ModelResolver.getInstance() .registerClassModelMapping(C.class, modelPackage.getCEClass(), modelPackage); ModelResolver.getInstance() .registerClassModelMapping(TestAny.class, modelPackage.getTestAnyEClass(), modelPackage); DaoRegistry.getInstance().registerDao(A.class, ADao.class); DaoRegistry.getInstance().registerDao(B.class, BDao.class); DaoRegistry.getInstance().registerDao(C.class, CDao.class); DaoRegistry.getInstance().registerDao(TestAny.class, TestAnyDao.class); // and return ourselves return modelPackage; }
/** * Converts the value of an EReference with isMany==true, the values of the collection are * converted to Objects and added to the list in the correct feature in the {@link ModelObject}. * * @param eObject the eObject from which the value is read * @param modelObject the Object in which the value is set * @param eReference the eReference which is converted */ protected void convertManyEReference( final EObject eObject, final ModelObject<?> modelObject, final EReference eReference) { @SuppressWarnings("unchecked") final Collection<EObject> eValues = (Collection<EObject>) eObject.eGet(eReference); if (ModelUtils.isEMap(eReference)) { @SuppressWarnings("unchecked") final Map<Object, Object> mValues = (Map<Object, Object>) modelObject.eGet(eReference); // clear as there can be current values if the target is read from the db mValues.clear(); for (final Object eValue : eValues) { @SuppressWarnings("unchecked") final Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) eValue; // key and value can also be an EObject final Object key; if (entry.getKey() instanceof EObject) { key = createTarget((EObject) entry.getKey()); } else { key = entry.getKey(); } final Object value; if (entry.getValue() instanceof EObject) { value = createTarget((EObject) entry.getValue()); } else { value = entry.getValue(); } mValues.put(key, value); } } else { // a many to many if (eReference.getEOpposite() != null && eReference.getEOpposite().isMany()) { final ManyToMany mtm = new ManyToMany(); mtm.setOwner(eObject); mtm.setEReference(eReference); toRepairManyToMany.add(mtm); } final Collection<?> mValues = (Collection<?>) modelObject.eGet(eReference); // make a copy final List<Object> copiedMValues = new ArrayList<Object>(mValues); // clear as there can be current values if the target is read from the db // use forloop as the collection can be unmodifiable for (Object o : new ArrayList<Object>(mValues)) { modelObject.eRemoveFrom(eReference, o); } // check that the eopposite is indeed cleared, this is not the case if // there is no bi-directional code generated final EReference eOpposite = eReference.getEOpposite(); if (eOpposite != null && !eOpposite.isMany()) { for (Object mValue : copiedMValues) { final ModelObject<?> modelMValue = ModelResolver.getInstance().getModelObject(mValue); modelMValue.eSet(eOpposite, null); } } for (final EObject eValue : eValues) { final Object target = createTarget(eValue); // first add to the many reference modelObject.eAddTo(eReference, target); // add to the other side, this is needed because the bi-directional // api is not always generated if (eOpposite != null && !eOpposite.isMany()) { final ModelObject<?> modelObjectTarget = ModelResolver.getInstance().getModelObject(target); modelObjectTarget.eSet(eReference.getEOpposite(), modelObject.getTarget()); } } } }
@Test public void testRetrievalAndDelete() { final Library lib = createTestData(); // children final Writer w = lib.getWriters().get(0); final Book bk = lib.getBooks().get(0); // get model information if (!isXmlTest()) { { final String content = doGetRequest("model/epackage?id=library", null, HttpServletResponse.SC_OK); final List<Object> objects = deserialize(content); Assert.assertEquals(1, objects.size()); Assert.assertTrue(objects.get(0) instanceof DynamicModelObject); final DynamicModelObject dmo = (DynamicModelObject) objects.get(0); Assert.assertTrue(dmo.eClass() == EcorePackage.eINSTANCE.getEPackage()); Assert.assertEquals("library", dmo.eGet(EcorePackage.eINSTANCE.getENamedElement_Name())); Assert.assertEquals( LibraryModelPackage.NS_URI, dmo.eGet(EcorePackage.eINSTANCE.getEPackage_NsURI())); } { final String content = doGetRequest( "model/epackage?id=" + LibraryModelPackage.NS_URI, null, HttpServletResponse.SC_OK); final List<Object> objects = deserialize(content); Assert.assertEquals(1, objects.size()); Assert.assertTrue(objects.get(0) instanceof DynamicModelObject); final DynamicModelObject dmo = (DynamicModelObject) objects.get(0); Assert.assertTrue(dmo.eClass() == EcorePackage.eINSTANCE.getEPackage()); Assert.assertEquals("library", dmo.eGet(EcorePackage.eINSTANCE.getENamedElement_Name())); Assert.assertEquals( LibraryModelPackage.NS_URI, dmo.eGet(EcorePackage.eINSTANCE.getEPackage_NsURI())); } { final String content = doGetRequest( "model/eclass?name=Book&epackage=" + LibraryModelPackage.NS_URI, null, HttpServletResponse.SC_OK); final List<Object> objects = deserialize(content); Assert.assertEquals(1, objects.size()); Assert.assertTrue(objects.get(0) instanceof DynamicModelObject); final DynamicModelObject dmo = (DynamicModelObject) objects.get(0); Assert.assertTrue(dmo.eClass() == EcorePackage.eINSTANCE.getEClass()); Assert.assertEquals("Book", dmo.eGet(EcorePackage.eINSTANCE.getENamedElement_Name())); } { final String content = doGetRequest( "model/eclassifier?name=BookCategory&epackage=" + LibraryModelPackage.NS_URI, null, HttpServletResponse.SC_OK); final List<Object> objects = deserialize(content); Assert.assertEquals(1, objects.size()); Assert.assertTrue(objects.get(0) instanceof DynamicModelObject); final DynamicModelObject dmo = (DynamicModelObject) objects.get(0); Assert.assertTrue(dmo.eClass() == EcorePackage.eINSTANCE.getEEnum()); Assert.assertEquals( "BookCategory", dmo.eGet(EcorePackage.eINSTANCE.getENamedElement_Name())); } } // get all libraries { final String content = doGetRequest( LibraryModelPackage.INSTANCE.getLibraryEClass().getName(), null, HttpServletResponse.SC_OK); final List<Object> objects = deserialize(content); Assert.assertEquals(1, objects.size()); final ResponseType responseType = (ResponseType) objects.get(0); final Library libResult = (Library) responseType.getData().get(0); Assert.assertNotSame(lib, libResult); Assert.assertEquals(lib.getDb_Id(), libResult.getDb_Id()); } // get one library, and call its content { String content = getOneObject(lib); getValidateOneObject(lib); System.err.println(content); // and all its content final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(lib); for (EReference eReference : modelObject.eClass().getEAllReferences()) { final Object value = modelObject.eGet(eReference); if (value == null) { continue; } if (eReference.isMany()) { for (Object element : (List<?>) value) { if (element instanceof Identifiable) { getValidateOneObject(element); } } } else { if (value instanceof Identifiable) { getValidateOneObject(value); } } } } // now delete the library doDeleteRequest( LibraryModelPackage.INSTANCE.getLibraryEClass().getName() + "/" + lib.getDb_Id(), HttpServletResponse.SC_OK); // $NON-NLS-1$ // this should fail { final String content = doGetRequest( LibraryModelPackage.INSTANCE.getLibraryEClass().getName() + "/" + lib.getDb_Id(), null, HttpServletResponse.SC_NOT_FOUND); // $NON-NLS-1$ final List<Object> objects = deserialize(content); Assert.assertEquals(1, objects.size()); final ErrorType errorType = (ErrorType) objects.get(0); Assert.assertTrue(errorType.getMessage().contains("Resource not found")); // $NON-NLS-1$ } // children are removed checkExists(w, false); checkExists(bk, false); }
@Override protected boolean isModelEnabled(Object target) { return ModelResolver.getInstance().isModelEnabled(target); }