Search in sources :

Example 1 with EjbRelation

use of org.apache.openejb.jee.EjbRelation in project tomee by apache.

the class EjbJarInfoBuilder method initRelationships.

private void initRelationships(final EjbModule jar, final Map<String, EnterpriseBeanInfo> infos) throws OpenEJBException {
    for (final EjbRelation ejbRelation : jar.getEjbJar().getRelationships().getEjbRelation()) {
        final Iterator<EjbRelationshipRole> iterator = ejbRelation.getEjbRelationshipRole().iterator();
        final EjbRelationshipRole left = iterator.next();
        final EjbRelationshipRole right = iterator.next();
        // left role info
        final CmrFieldInfo leftCmrFieldInfo = initRelationshipRole(left, right, infos);
        final CmrFieldInfo rightCmrFieldInfo = initRelationshipRole(right, left, infos);
        leftCmrFieldInfo.mappedBy = rightCmrFieldInfo;
        rightCmrFieldInfo.mappedBy = leftCmrFieldInfo;
    }
}
Also used : EjbRelation(org.apache.openejb.jee.EjbRelation) CmrFieldInfo(org.apache.openejb.assembler.classic.CmrFieldInfo) EjbRelationshipRole(org.apache.openejb.jee.EjbRelationshipRole)

Example 2 with EjbRelation

use of org.apache.openejb.jee.EjbRelation in project tomee by apache.

the class Relationships$JAXB method _write.

public static final void _write(final XoXMLStreamWriter writer, final Relationships relationships, RuntimeContext context) throws Exception {
    if (relationships == null) {
        writer.writeXsiNil();
        return;
    }
    if (context == null) {
        context = new RuntimeContext();
    }
    final String prefix = writer.getUniquePrefix("http://java.sun.com/xml/ns/javaee");
    if (Relationships.class != relationships.getClass()) {
        context.unexpectedSubclass(writer, relationships, Relationships.class);
        return;
    }
    context.beforeMarshal(relationships, LifecycleCallback.NONE);
    // ATTRIBUTE: id
    final String idRaw = relationships.id;
    if (idRaw != null) {
        String id = null;
        try {
            id = Adapters.collapsedStringAdapterAdapter.marshal(idRaw);
        } catch (final Exception e) {
            context.xmlAdapterError(relationships, "id", CollapsedStringAdapter.class, String.class, String.class, e);
        }
        writer.writeAttribute("", "", "id", id);
    }
    // ELEMENT: descriptions
    Text[] descriptions = null;
    try {
        descriptions = relationships.getDescriptions();
    } catch (final Exception e) {
        context.getterError(relationships, "descriptions", Relationships.class, "getDescriptions", e);
    }
    if (descriptions != null) {
        for (final Text descriptionsItem : descriptions) {
            if (descriptionsItem != null) {
                writer.writeStartElement(prefix, "description", "http://java.sun.com/xml/ns/javaee");
                writeText(writer, descriptionsItem, context);
                writer.writeEndElement();
            } else {
                context.unexpectedNullValue(relationships, "descriptions");
            }
        }
    }
    // ELEMENT: ejbRelation
    final List<EjbRelation> ejbRelation = relationships.ejbRelation;
    if (ejbRelation != null) {
        for (final EjbRelation ejbRelationItem : ejbRelation) {
            if (ejbRelationItem != null) {
                writer.writeStartElement(prefix, "ejb-relation", "http://java.sun.com/xml/ns/javaee");
                writeEjbRelation(writer, ejbRelationItem, context);
                writer.writeEndElement();
            } else {
                context.unexpectedNullValue(relationships, "ejbRelation");
            }
        }
    }
    context.afterMarshal(relationships, LifecycleCallback.NONE);
}
Also used : CollapsedStringAdapter(javax.xml.bind.annotation.adapters.CollapsedStringAdapter) EjbRelation$JAXB.writeEjbRelation(org.apache.openejb.jee.EjbRelation$JAXB.writeEjbRelation) EjbRelation$JAXB.readEjbRelation(org.apache.openejb.jee.EjbRelation$JAXB.readEjbRelation) Text$JAXB.readText(org.apache.openejb.jee.Text$JAXB.readText) Text$JAXB.writeText(org.apache.openejb.jee.Text$JAXB.writeText) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext)

Example 3 with EjbRelation

use of org.apache.openejb.jee.EjbRelation in project tomee by apache.

the class Relationships$JAXB method _read.

public static final Relationships _read(final XoXMLStreamReader reader, RuntimeContext context) throws Exception {
    // Check for xsi:nil
    if (reader.isXsiNil()) {
        return null;
    }
    if (context == null) {
        context = new RuntimeContext();
    }
    final Relationships relationships = new Relationships();
    context.beforeUnmarshal(relationships, LifecycleCallback.NONE);
    ArrayList<Text> descriptions = null;
    List<EjbRelation> ejbRelation = null;
    // Check xsi:type
    final QName xsiType = reader.getXsiType();
    if (xsiType != null) {
        if (("relationshipsType" != xsiType.getLocalPart()) || ("http://java.sun.com/xml/ns/javaee" != xsiType.getNamespaceURI())) {
            return context.unexpectedXsiType(reader, Relationships.class);
        }
    }
    // Read attributes
    for (final Attribute attribute : reader.getAttributes()) {
        if (("id" == attribute.getLocalName()) && (("" == attribute.getNamespace()) || (attribute.getNamespace() == null))) {
            // ATTRIBUTE: id
            final String id = Adapters.collapsedStringAdapterAdapter.unmarshal(attribute.getValue());
            context.addXmlId(reader, id, relationships);
            relationships.id = id;
        } else if (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI != attribute.getNamespace()) {
            context.unexpectedAttribute(attribute, new QName("", "id"));
        }
    }
    // Read elements
    for (final XoXMLStreamReader elementReader : reader.getChildElements()) {
        if (("description" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: descriptions
            final Text descriptionsItem = readText(elementReader, context);
            if (descriptions == null) {
                descriptions = new ArrayList<Text>();
            }
            descriptions.add(descriptionsItem);
        } else if (("ejb-relation" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: ejbRelation
            final EjbRelation ejbRelationItem = readEjbRelation(elementReader, context);
            if (ejbRelation == null) {
                ejbRelation = relationships.ejbRelation;
                if (ejbRelation != null) {
                    ejbRelation.clear();
                } else {
                    ejbRelation = new ArrayList<EjbRelation>();
                }
            }
            ejbRelation.add(ejbRelationItem);
        } else {
            context.unexpectedElement(elementReader, new QName("http://java.sun.com/xml/ns/javaee", "description"), new QName("http://java.sun.com/xml/ns/javaee", "ejb-relation"));
        }
    }
    if (descriptions != null) {
        try {
            relationships.setDescriptions(descriptions.toArray(new Text[descriptions.size()]));
        } catch (final Exception e) {
            context.setterError(reader, Relationships.class, "setDescriptions", Text[].class, e);
        }
    }
    if (ejbRelation != null) {
        relationships.ejbRelation = ejbRelation;
    }
    context.afterUnmarshal(relationships, LifecycleCallback.NONE);
    return relationships;
}
Also used : EjbRelation$JAXB.writeEjbRelation(org.apache.openejb.jee.EjbRelation$JAXB.writeEjbRelation) EjbRelation$JAXB.readEjbRelation(org.apache.openejb.jee.EjbRelation$JAXB.readEjbRelation) Attribute(org.metatype.sxc.util.Attribute) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Text$JAXB.readText(org.apache.openejb.jee.Text$JAXB.readText) Text$JAXB.writeText(org.apache.openejb.jee.Text$JAXB.writeText) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext) XoXMLStreamReader(org.metatype.sxc.util.XoXMLStreamReader)

Example 4 with EjbRelation

use of org.apache.openejb.jee.EjbRelation in project tomee by apache.

the class CmpJpaConversion method deploy.

public AppModule deploy(final AppModule appModule) throws OpenEJBException {
    if (!hasCmpEntities(appModule)) {
        return appModule;
    }
    // create mappings if no mappings currently exist
    EntityMappings cmpMappings = appModule.getCmpMappings();
    if (cmpMappings == null) {
        cmpMappings = new EntityMappings();
        cmpMappings.setVersion("1.0");
        appModule.setCmpMappings(cmpMappings);
    }
    final Set<String> definedMappedClasses = new HashSet<>();
    // check for an existing "cmp" persistence unit, and look at existing mappings
    final PersistenceUnit cmpPersistenceUnit = findCmpPersistenceUnit(appModule);
    if (cmpPersistenceUnit != null) {
        if (cmpPersistenceUnit.getMappingFile() != null && cmpPersistenceUnit.getMappingFile().size() > 0) {
            for (final String mappingFile : cmpPersistenceUnit.getMappingFile()) {
                final EntityMappings entityMappings = readEntityMappings(mappingFile, appModule);
                if (entityMappings != null) {
                    definedMappedClasses.addAll(entityMappings.getEntityMap().keySet());
                }
            }
        }
    }
    // app mapping data
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final EjbJar ejbJar = ejbModule.getEjbJar();
        // scan for CMP entity beans and merge the data into the collective set
        for (final EnterpriseBean enterpriseBean : ejbJar.getEnterpriseBeans()) {
            if (isCmpEntity(enterpriseBean)) {
                processEntityBean(ejbModule, definedMappedClasses, cmpMappings, (EntityBean) enterpriseBean);
            }
        }
        // if there are relationships defined in this jar, get a list of the defined
        // entities and process the relationship maps.
        final Relationships relationships = ejbJar.getRelationships();
        if (relationships != null) {
            final Map<String, Entity> entitiesByEjbName = new TreeMap<>();
            for (final Entity entity : cmpMappings.getEntity()) {
                entitiesByEjbName.put(entity.getEjbName(), entity);
            }
            for (final EjbRelation relation : relationships.getEjbRelation()) {
                processRelationship(entitiesByEjbName, relation);
            }
        }
        // Let's warn the user about any declarations we didn't end up using
        // so there can be no misunderstandings.
        final EntityMappings userMappings = getUserEntityMappings(ejbModule);
        for (final Entity mapping : userMappings.getEntity()) {
            LOGGER.warning("openejb-cmp-orm.xml mapping ignored: module=" + ejbModule.getModuleId() + ":  <entity class=\"" + mapping.getClazz() + "\">");
        }
        for (final MappedSuperclass mapping : userMappings.getMappedSuperclass()) {
            LOGGER.warning("openejb-cmp-orm.xml mapping ignored: module=" + ejbModule.getModuleId() + ":  <mapped-superclass class=\"" + mapping.getClazz() + "\">");
        }
    }
    if (!cmpMappings.getEntity().isEmpty()) {
        final PersistenceUnit persistenceUnit = getCmpPersistenceUnit(appModule);
        final boolean generatedOrmXmlProvided = appModule.getClassLoader().getResource(GENERATED_ORM_XML) != null;
        if (!persistenceUnit.getMappingFile().contains(GENERATED_ORM_XML)) {
            // explicit check for openejb-cmp-generated-orm, as this is generated and added to <mapping-file>
            if (generatedOrmXmlProvided) {
                LOGGER.warning("App module " + appModule.getModuleId() + " provides " + GENERATED_ORM_XML + ", but does not " + "specify it using <mapping-file> in persistence.xml for the CMP persistence unit, and it may conflict " + "with the generated mapping file. Consider renaming the file and explicitly referencing it in persistence.xml");
            }
            persistenceUnit.getMappingFile().add(GENERATED_ORM_XML);
        } else {
            if (generatedOrmXmlProvided) {
                LOGGER.warning("App module " + appModule.getModuleId() + " provides " + GENERATED_ORM_XML + " and additionally " + cmpMappings.getEntity().size() + "mappings have been generated. Consider renaming the " + GENERATED_ORM_XML + " in " + "your deployment archive to avoid any conflicts.");
            }
        }
        for (final Entity entity : cmpMappings.getEntity()) {
            if (!persistenceUnit.getClazz().contains(entity.getClazz())) {
                persistenceUnit.getClazz().add(entity.getClazz());
            }
        }
    }
    // causes some of the unit tests to fail.  Not sure why.  Should be fixed.
    for (final Entity entity : appModule.getCmpMappings().getEntity()) {
        if (entity.getAttributes() != null && entity.getAttributes().isEmpty()) {
            entity.setAttributes(null);
        }
    }
    return appModule;
}
Also used : Entity(org.apache.openejb.jee.jpa.Entity) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) TreeMap(java.util.TreeMap) Relationships(org.apache.openejb.jee.Relationships) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) EjbRelation(org.apache.openejb.jee.EjbRelation) MappedSuperclass(org.apache.openejb.jee.jpa.MappedSuperclass) EntityMappings(org.apache.openejb.jee.jpa.EntityMappings) HashSet(java.util.HashSet) EjbJar(org.apache.openejb.jee.EjbJar)

Aggregations

EjbRelation (org.apache.openejb.jee.EjbRelation)2 EjbRelation$JAXB.readEjbRelation (org.apache.openejb.jee.EjbRelation$JAXB.readEjbRelation)2 EjbRelation$JAXB.writeEjbRelation (org.apache.openejb.jee.EjbRelation$JAXB.writeEjbRelation)2 Text$JAXB.readText (org.apache.openejb.jee.Text$JAXB.readText)2 Text$JAXB.writeText (org.apache.openejb.jee.Text$JAXB.writeText)2 RuntimeContext (org.metatype.sxc.jaxb.RuntimeContext)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 CollapsedStringAdapter (javax.xml.bind.annotation.adapters.CollapsedStringAdapter)1 QName (javax.xml.namespace.QName)1 CmrFieldInfo (org.apache.openejb.assembler.classic.CmrFieldInfo)1 EjbJar (org.apache.openejb.jee.EjbJar)1 EjbRelationshipRole (org.apache.openejb.jee.EjbRelationshipRole)1 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)1 Relationships (org.apache.openejb.jee.Relationships)1 Entity (org.apache.openejb.jee.jpa.Entity)1 EntityMappings (org.apache.openejb.jee.jpa.EntityMappings)1 MappedSuperclass (org.apache.openejb.jee.jpa.MappedSuperclass)1 PersistenceUnit (org.apache.openejb.jee.jpa.unit.PersistenceUnit)1