Search in sources :

Example 11 with EntityDescriptor

use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.

the class EntityTypeSerializerTest method testEntityTypeSerializer.

@Test
public void testEntityTypeSerializer() throws RDFHandlerException {
    EntityDescriptor entityDescriptor = module.entityDescriptor(TestEntity.class.getName());
    Iterable<Statement> graph = serializer.serialize(entityDescriptor);
    String[] prefixes = new String[] { "rdf", "dc", " vc", "qi4j" };
    String[] namespaces = new String[] { Rdfs.RDF, DcRdf.NAMESPACE, "http://www.w3.org/2001/vcard-rdf/3.0#", Qi4jEntityType.NAMESPACE };
    new RdfXmlSerializer().serialize(graph, new PrintWriter(System.out), prefixes, namespaces);
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) Statement(org.openrdf.model.Statement) RdfXmlSerializer(org.qi4j.library.rdf.serializer.RdfXmlSerializer) PrintWriter(java.io.PrintWriter) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 12 with EntityDescriptor

use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.

the class EntityResource method representHtml.

private Representation representHtml(final EntityState entity) {
    return new WriterRepresentation(MediaType.TEXT_HTML) {

        @Override
        public void write(Writer writer) throws IOException {
            PrintWriter out = new PrintWriter(writer);
            out.println("<html><head><title>" + entity.identity() + "</title>" + "<link rel=\"alternate\" type=\"application/rdf+xml\" " + "href=\"" + entity.identity() + ".rdf\"/></head><body>");
            out.println("<h1>" + entity.identity() + "</h1>");
            out.println("<form method=\"post\" action=\"" + getRequest().getResourceRef().getPath() + "\">\n");
            out.println("<fieldset><legend>Properties</legend>\n<table>");
            final EntityDescriptor descriptor = entity.entityDescriptor();
            for (PropertyDescriptor persistentProperty : descriptor.state().properties()) {
                Object value = entity.propertyValueOf(persistentProperty.qualifiedName());
                out.println("<tr><td>" + "<label for=\"" + persistentProperty.qualifiedName() + "\" >" + persistentProperty.qualifiedName().name() + "</label></td>\n" + "<td><input " + "size=\"80\" " + "type=\"text\" " + (persistentProperty.isImmutable() ? "readonly=\"true\" " : "") + "name=\"" + persistentProperty.qualifiedName() + "\" " + "value=\"" + (value == null ? "" : valueSerialization.serialize(value)) + "\"/></td></tr>");
            }
            out.println("</table></fieldset>\n");
            out.println("<fieldset><legend>Associations</legend>\n<table>");
            for (AssociationDescriptor associationType : descriptor.state().associations()) {
                Object value = entity.associationValueOf(associationType.qualifiedName());
                if (value == null) {
                    value = "";
                }
                out.println("<tr><td>" + "<label for=\"" + associationType.qualifiedName() + "\" >" + associationType.qualifiedName().name() + "</label></td>\n" + "<td><input " + "type=\"text\" " + "size=\"80\" " + "name=\"" + associationType.qualifiedName() + "\" " + "value=\"" + value + "\"/></td></tr>");
            }
            out.println("</table></fieldset>\n");
            out.println("<fieldset><legend>ManyAssociations</legend>\n<table>");
            for (AssociationDescriptor associationType : descriptor.state().manyAssociations()) {
                ManyAssociationState identities = entity.manyAssociationValueOf(associationType.qualifiedName());
                String value = "";
                for (EntityReference identity : identities) {
                    value += identity.identity() + "\n";
                }
                out.println("<tr><td>" + "<label for=\"" + associationType.qualifiedName() + "\" >" + associationType.qualifiedName().name() + "</label></td>\n" + "<td><textarea " + "rows=\"10\" " + "cols=\"80\" " + "name=\"" + associationType.qualifiedName() + "\" >" + value + "</textarea></td></tr>");
            }
            out.println("</table></fieldset>\n");
            out.println("<fieldset><legend>NamedAssociations</legend>\n<table>");
            for (AssociationDescriptor associationType : descriptor.state().namedAssociations()) {
                NamedAssociationState identities = entity.namedAssociationValueOf(associationType.qualifiedName());
                String value = "";
                for (String name : identities) {
                    value += name + "\n" + identities.get(name).identity() + "\n";
                }
                out.println("<tr><td>" + "<label for=\"" + associationType.qualifiedName() + "\" >" + associationType.qualifiedName().name() + "</label></td>\n" + "<td><textarea " + "rows=\"10\" " + "cols=\"80\" " + "name=\"" + associationType.qualifiedName() + "\" >" + value + "</textarea></td></tr>");
            }
            out.println("</table></fieldset>\n");
            out.println("<input type=\"submit\" value=\"Update\"/></form>\n");
            out.println("</body></html>\n");
        }
    };
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) WriterRepresentation(org.restlet.representation.WriterRepresentation) NamedAssociationState(org.qi4j.spi.entity.NamedAssociationState) EntityReference(org.qi4j.api.entity.EntityReference) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 13 with EntityDescriptor

use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.

the class PropertiesPanel method createData.

/**
     * Create table table or properties using the supplied query
     *
     * @param query the Query
     *
     * @return TableModel
     */
protected TableModel createData(Query query) {
    DefaultTableModel model = new DefaultTableModel();
    for (Object qObj : query) {
        AssociationStateHolder state = qi4jspi.stateOf((EntityComposite) qObj);
        EntityDescriptor descriptor = qi4jspi.entityDescriptorFor((EntityComposite) qObj);
        // genereate column, first time only
        if (model.getColumnCount() < 1) {
            for (PropertyDescriptor persistentPropertyDescriptor : descriptor.state().properties()) {
                model.addColumn(persistentPropertyDescriptor.qualifiedName().name());
            }
        }
        Object[] rowData = new Object[model.getColumnCount()];
        int i = 0;
        for (PropertyDescriptor persistentPropertyDescriptor : descriptor.state().properties()) {
            rowData[i++] = state.propertyFor(persistentPropertyDescriptor.accessor());
        }
        model.addRow(rowData);
    }
    return model;
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) DefaultTableModel(javax.swing.table.DefaultTableModel) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder)

Example 14 with EntityDescriptor

use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.

the class PDFWriter method writeTypeGeneralPage.

private void writeTypeGeneralPage(Object objectDesciptor) {
    setFont(header5Font, header5FontSize);
    writeString("General: ", headerLineSpace);
    setFont(normalFont, normalFontSize);
    if (objectDesciptor instanceof ServiceDetailDescriptor) {
        ServiceDescriptor descriptor = ((ServiceDetailDescriptor) objectDesciptor).descriptor();
        writeString("- identity: " + descriptor.identity());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
        writeString("- startup: " + ((ServiceDetailDescriptor) objectDesciptor).descriptor().isInstantiateOnStartup());
    } else if (objectDesciptor instanceof EntityDetailDescriptor) {
        EntityDescriptor descriptor = ((EntityDetailDescriptor) objectDesciptor).descriptor();
        writeString("- name: " + descriptor.toString());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
    } else if (objectDesciptor instanceof ValueDetailDescriptor) {
        ValueDescriptor descriptor = ((ValueDetailDescriptor) objectDesciptor).descriptor();
        writeString("- name: " + descriptor.toString());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
    } else if (objectDesciptor instanceof ObjectDetailDescriptor) {
        ObjectDescriptor descriptor = ((ObjectDetailDescriptor) objectDesciptor).descriptor();
        writeString("- name: " + descriptor.toString());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
    } else if (objectDesciptor instanceof CompositeDetailDescriptor) {
        CompositeDescriptor descriptor = ((CompositeDetailDescriptor) objectDesciptor).descriptor();
        writeString("- name: " + descriptor.toString());
        writeString("- class: " + descriptor.toString());
        writeString("- visibility: " + descriptor.visibility().toString());
    }
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ServiceDescriptor(org.qi4j.api.service.ServiceDescriptor) ImportedServiceDescriptor(org.qi4j.api.service.ImportedServiceDescriptor) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) CompositeDescriptor(org.qi4j.api.composite.CompositeDescriptor) ObjectDescriptor(org.qi4j.api.object.ObjectDescriptor)

Example 15 with EntityDescriptor

use of org.qi4j.api.entity.EntityDescriptor in project qi4j-sdk by Qi4j.

the class StateInjectionProviderFactory method newInjectionProvider.

@Override
public InjectionProvider newInjectionProvider(Resolution resolution, DependencyModel dependencyModel) throws InvalidInjectionException {
    if (StateHolder.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State StateHolder properties;
        return new StateInjectionProvider();
    } else if (UnitOfWork.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        if (!(resolution.model() instanceof EntityDescriptor)) {
            throw new InvalidInjectionException("Only EntityComposites can be injected with '@State UnitOfWork'");
        }
        return new UnitOfWorkInjectionProvider();
    } else if (Property.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State Property<String> name;
        StateDescriptor descriptor;
        descriptor = ((StatefulCompositeDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        PropertyDescriptor propertyDescriptor = descriptor.findPropertyModelByName(name);
        return new PropertyInjectionProvider(propertyDescriptor);
    } else if (Association.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State Association<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getAssociationByName(name);
        return new AssociationInjectionProvider(model);
    } else if (ManyAssociation.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State ManyAssociation<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getManyAssociationByName(name);
        return new ManyAssociationInjectionProvider(model);
    } else if (NamedAssociation.class.isAssignableFrom(dependencyModel.rawInjectionType())) {
        // @State NamedAssociation<MyEntity> name;
        AssociationStateDescriptor descriptor = ((EntityDescriptor) resolution.model()).state();
        State annotation = (State) dependencyModel.injectionAnnotation();
        String name;
        if (annotation.value().isEmpty()) {
            name = resolution.field().getName();
        } else {
            name = annotation.value();
        }
        AssociationDescriptor model = descriptor.getNamedAssociationByName(name);
        return new NamedAssociationInjectionProvider(model);
    }
    throw new InjectionProviderException("Injected value has invalid type");
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) NamedAssociation(org.qi4j.api.association.NamedAssociation) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ManyAssociation(org.qi4j.api.association.ManyAssociation) AbstractAssociation(org.qi4j.api.association.AbstractAssociation) Association(org.qi4j.api.association.Association) NamedAssociation(org.qi4j.api.association.NamedAssociation) State(org.qi4j.api.injection.scope.State) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) StateDescriptor(org.qi4j.api.composite.StateDescriptor) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Aggregations

EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)24 EntityReference (org.qi4j.api.entity.EntityReference)11 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)8 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)8 IOException (java.io.IOException)6 Writer (java.io.Writer)6 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)6 QualifiedName (org.qi4j.api.common.QualifiedName)6 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)5 HashSet (java.util.HashSet)4 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)4