Search in sources :

Example 1 with DynamicType

use of org.eclipse.persistence.dynamic.DynamicType in project aai-aai-common by onap.

the class FromOxmVertexSchema method fromOxm.

public void fromOxm(String vertexType, DynamicJAXBContext jaxbContext, HashMap<String, DynamicType> xmlElementLookup) throws SchemaProviderException {
    name = vertexType;
    properties = new HashMap<String, PropertySchema>();
    annotations = new HashMap<String, String>();
    String javaTypeName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, vertexType);
    DynamicType modelObjectType = jaxbContext.getDynamicType(javaTypeName);
    if (modelObjectType == null) {
        // Try to lookup by xml root element by exact match
        modelObjectType = xmlElementLookup.get(vertexType);
    }
    if (modelObjectType == null) {
        // Try to lookup by xml root element by lowercase
        modelObjectType = xmlElementLookup.get(vertexType.toLowerCase());
    }
    if (modelObjectType == null) {
        // Direct lookup as java-type name
        modelObjectType = jaxbContext.getDynamicType(vertexType);
    }
    if (modelObjectType == null) {
        // Vertex isn't found in the OXM
        throw new SchemaProviderException("Vertex " + vertexType + " not found in OXM");
    }
    // Check annotations
    Map<String, Object> oxmProps = modelObjectType.getDescriptor().getProperties();
    for (Map.Entry<String, Object> entry : oxmProps.entrySet()) {
        if (entry.getValue() instanceof String) {
            annotations.put(entry.getKey().toLowerCase(), (String) entry.getValue());
        }
    }
    // Regular props
    for (DatabaseMapping mapping : modelObjectType.getDescriptor().getMappings()) {
        if (mapping instanceof XMLAnyObjectMapping)
            continue;
        if (mapping instanceof XMLAnyCollectionMapping)
            continue;
        FromOxmPropertySchema propSchema = new FromOxmPropertySchema();
        propSchema.fromOxm(mapping, modelObjectType, false);
        properties.put(propSchema.getName().toLowerCase(), propSchema);
    }
    // Reserved Props
    final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
    for (DatabaseMapping mapping : reservedType.getDescriptor().getMappings()) {
        if (mapping.isAbstractDirectMapping()) {
            FromOxmPropertySchema propSchema = new FromOxmPropertySchema();
            propSchema.fromOxm(mapping, reservedType, true);
            properties.put(propSchema.getName().toLowerCase(), propSchema);
        }
    }
}
Also used : PropertySchema(org.onap.aai.schemaif.definitions.PropertySchema) XMLAnyCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping) DynamicType(org.eclipse.persistence.dynamic.DynamicType) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException) Map(java.util.Map) HashMap(java.util.HashMap) XMLAnyObjectMapping(org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping)

Example 2 with DynamicType

use of org.eclipse.persistence.dynamic.DynamicType in project aai-aai-common by onap.

the class OxmSchemaLoader method loadXmlLookupMap.

private static void loadXmlLookupMap(String version, DynamicJAXBContext jaxbContext) {
    @SuppressWarnings("rawtypes") List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
    HashMap<String, DynamicType> types = new HashMap<String, DynamicType>();
    for (@SuppressWarnings("rawtypes") Descriptor desc : descriptorsList) {
        DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
        String entityName = desc.getDefaultRootElement();
        types.put(entityName, entity);
    }
    xmlElementLookup.put(version, types);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DynamicType(org.eclipse.persistence.dynamic.DynamicType) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor)

Example 3 with DynamicType

use of org.eclipse.persistence.dynamic.DynamicType in project eclipselink by eclipse-ee4j.

the class PersistenceContext method newEntity.

/**
 * A convenience method to create a new dynamic entity of the given type
 */
public DynamicEntity newEntity(Map<String, String> tenantId, String type) {
    JPADynamicHelper helper = new JPADynamicHelper(getEmf());
    DynamicEntity entity;
    try {
        entity = helper.newDynamicEntity(type);
    } catch (IllegalArgumentException e) {
        ClassDescriptor descriptor = getDescriptor(type);
        if (descriptor != null) {
            DynamicType jaxbType = (DynamicType) descriptor.getProperty(DynamicType.DESCRIPTOR_PROPERTY);
            if (jaxbType != null) {
                return jaxbType.newDynamicEntity();
            }
        }
        JPARSLogger.exception(getSessionLog(), "exception_thrown_while_creating_dynamic_entity", new Object[] { type }, e);
        throw e;
    }
    return entity;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) DynamicType(org.eclipse.persistence.dynamic.DynamicType) JPADynamicHelper(org.eclipse.persistence.jpa.dynamic.JPADynamicHelper)

Example 4 with DynamicType

use of org.eclipse.persistence.dynamic.DynamicType in project eclipselink by eclipse-ee4j.

the class JPADynamicHelper method addTypes.

/**
 * Add one or more EntityType instances to a session and optionally generate
 * needed tables with or without FK constraints.
 */
@Override
public void addTypes(boolean createMissingTables, boolean generateFKConstraints, DynamicType... types) {
    super.addTypes(createMissingTables, generateFKConstraints, types);
    for (DynamicType type : types) {
        ClassDescriptor descriptor = type.getDescriptor();
        descriptor.getQueryManager().checkDatabaseForDoesExist();
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DynamicType(org.eclipse.persistence.dynamic.DynamicType)

Example 5 with DynamicType

use of org.eclipse.persistence.dynamic.DynamicType in project eclipselink by eclipse-ee4j.

the class SimpleTypes_MultiTable method persistSimpleA.

@Test
public void persistSimpleA() {
    DynamicType simpleTypeA = dynamicHelper.getType("SimpleA");
    Assert.assertNotNull(simpleTypeA);
    DynamicEntity simpleInstance = simpleTypeA.newDynamicEntity();
    simpleInstance.set("id", 1);
    simpleInstance.set("value1", "A1");
    UnitOfWork uow = session.acquireUnitOfWork();
    uow.registerNewObject(simpleInstance);
    uow.commit();
    ReportQuery countQuery = dynamicHelper.newReportQuery("SimpleA", new ExpressionBuilder());
    countQuery.addCount();
    countQuery.setShouldReturnSingleValue(true);
    int simpleCount = ((Number) session.executeQuery(countQuery)).intValue();
    Assert.assertEquals(1, simpleCount);
    session.release();
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) ReportQuery(org.eclipse.persistence.queries.ReportQuery) DynamicType(org.eclipse.persistence.dynamic.DynamicType) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Test(org.junit.Test)

Aggregations

DynamicType (org.eclipse.persistence.dynamic.DynamicType)76 Test (org.junit.Test)57 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)56 EntityManager (jakarta.persistence.EntityManager)20 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)19 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)15 ReportQuery (org.eclipse.persistence.queries.ReportQuery)15 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)13 JPADynamicHelper (org.eclipse.persistence.jpa.dynamic.JPADynamicHelper)8 DirectToFieldMapping (org.eclipse.persistence.mappings.DirectToFieldMapping)8 HashMap (java.util.HashMap)7 DynamicClassLoader (org.eclipse.persistence.dynamic.DynamicClassLoader)5 OneToManyMapping (org.eclipse.persistence.mappings.OneToManyMapping)4 BufferedReader (java.io.BufferedReader)3 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 DynamicTypeBuilder (org.eclipse.persistence.dynamic.DynamicTypeBuilder)3 DynamicEntityImpl (org.eclipse.persistence.internal.dynamic.DynamicEntityImpl)3 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)3 DynamicHelper (org.eclipse.persistence.dynamic.DynamicHelper)2