Search in sources :

Example 1 with QueryConnectField

use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.

the class WorkspaceConnectFieldIOHandler method retrieve.

@Override
public String retrieve(WorkspaceMaterialField field, WorkspaceMaterialReply reply) throws WorkspaceFieldIOException {
    Map<String, String> answers = new HashMap<>();
    QueryConnectField queryConnectField = (QueryConnectField) field.getQueryField();
    List<QueryConnectFieldTerm> terms = queryConnectFieldController.listConnectFieldTermsByField(queryConnectField);
    for (QueryConnectFieldTerm term : terms) {
        WorkspaceMaterialConnectFieldAnswer connectFieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialConnectFieldAnswerByFieldAndReplyAndTerm(field, reply, term);
        if ((connectFieldAnswer != null) && (connectFieldAnswer.getCounterpart() != null)) {
            answers.put(term.getName(), connectFieldAnswer.getCounterpart().getName());
        }
    }
    try {
        return new ObjectMapper().writeValueAsString(answers);
    } catch (IOException e) {
        throw new WorkspaceFieldIOException("Could not marshal field response", e);
    }
}
Also used : WorkspaceMaterialConnectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialConnectFieldAnswer) HashMap(java.util.HashMap) IOException(java.io.IOException) QueryConnectFieldTerm(fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldTerm) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) QueryConnectField(fi.otavanopisto.muikku.plugins.material.model.QueryConnectField)

Example 2 with QueryConnectField

use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.

the class HtmlMaterialFieldChangeListener method onHtmlMaterialConnectFieldCreated.

// Connect field
public void onHtmlMaterialConnectFieldCreated(@Observes HtmlMaterialFieldCreateEvent event) throws MaterialQueryIntegrityExeption, MaterialFieldMetaParsingExeption {
    if (event.getField().getType().equals("application/vnd.muikku.field.connect")) {
        ObjectMapper objectMapper = new ObjectMapper();
        ConnectFieldMeta connectFieldMeta;
        try {
            connectFieldMeta = objectMapper.readValue(event.getField().getContent(), ConnectFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse connect field meta", e);
        }
        QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(event.getMaterial(), connectFieldMeta.getName());
        if (queryField != null) {
            throw new MaterialQueryIntegrityExeption("Field with same name already exists in the database");
        }
        QueryConnectField field = queryConnectFieldController.createQueryConnectField(event.getMaterial(), connectFieldMeta.getName());
        Map<String, QueryConnectFieldTerm> terms = new HashMap<String, QueryConnectFieldTerm>();
        Map<String, QueryConnectFieldCounterpart> counterparts = new HashMap<String, QueryConnectFieldCounterpart>();
        for (ConnectFieldOptionMeta option : connectFieldMeta.getFields()) {
            terms.put(option.getName(), queryConnectFieldController.createConnectFieldTerm(field, option.getName(), option.getText(), null));
        }
        for (ConnectFieldOptionMeta option : connectFieldMeta.getCounterparts()) {
            counterparts.put(option.getName(), queryConnectFieldController.createConnectFieldCounterpart(field, option.getName(), option.getText()));
        }
        for (ConnectFieldConnectionMeta connection : connectFieldMeta.getConnections()) {
            terms.get(connection.getField()).setCounterpart(counterparts.get(connection.getCounterpart()));
        }
    }
}
Also used : ConnectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta) HashMap(java.util.HashMap) QueryConnectFieldCounterpart(fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldCounterpart) ConnectFieldConnectionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldConnectionMeta) IOException(java.io.IOException) QueryConnectFieldTerm(fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldTerm) ConnectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldOptionMeta) QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) QueryConnectField(fi.otavanopisto.muikku.plugins.material.model.QueryConnectField)

Example 3 with QueryConnectField

use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.

the class QueryConnectFieldDAO method create.

public QueryConnectField create(Material material, String name) {
    QueryConnectField queryConnectField = new QueryConnectField();
    queryConnectField.setMaterial(material);
    queryConnectField.setName(name);
    return persist(queryConnectField);
}
Also used : QueryConnectField(fi.otavanopisto.muikku.plugins.material.model.QueryConnectField)

Example 4 with QueryConnectField

use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.

the class QueryConnectFieldDAO method findByMaterialAndName.

public QueryConnectField findByMaterialAndName(Material material, String name) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<QueryConnectField> criteria = criteriaBuilder.createQuery(QueryConnectField.class);
    Root<QueryConnectField> root = criteria.from(QueryConnectField.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(QueryConnectField_.material), material), criteriaBuilder.equal(root.get(QueryConnectField_.name), name)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) QueryConnectField(fi.otavanopisto.muikku.plugins.material.model.QueryConnectField)

Example 5 with QueryConnectField

use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.

the class WorkspaceConnectFieldIOHandler method store.

@Override
public void store(WorkspaceMaterialField field, WorkspaceMaterialReply reply, String value) throws WorkspaceFieldIOException {
    Map<String, String> valueMap = null;
    try {
        valueMap = new ObjectMapper().readValue(value, new TypeReference<Map<String, String>>() {
        });
    } catch (IOException e) {
        throw new WorkspaceFieldIOException("Could not unmarshal field response", e);
    }
    QueryConnectField queryConnectField = (QueryConnectField) field.getQueryField();
    if (queryConnectField != null) {
        List<QueryConnectFieldTerm> terms = queryConnectFieldController.listConnectFieldTermsByField(queryConnectField);
        for (QueryConnectFieldTerm term : terms) {
            String parameterValue = valueMap.get(term.getName());
            QueryConnectFieldCounterpart counterpart = StringUtils.isNotEmpty(parameterValue) ? queryConnectFieldController.findQueryConnectFieldCounterpartByFieldAndName(queryConnectField, parameterValue) : null;
            WorkspaceMaterialConnectFieldAnswer connectFieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialConnectFieldAnswerByFieldAndReplyAndTerm(field, reply, term);
            if (connectFieldAnswer != null) {
                workspaceMaterialFieldAnswerController.updateWorkspaceMaterialConnectFieldAnswerCounterpart(connectFieldAnswer, counterpart);
            } else {
                if (counterpart != null) {
                    workspaceMaterialFieldAnswerController.createWorkspaceMaterialConnectFieldAnswer(field, reply, term, counterpart);
                }
            }
        }
    } else {
        throw new WorkspaceFieldIOException("Workspace material connect field #" + field.getId() + " points to non-existing field ");
    }
}
Also used : WorkspaceMaterialConnectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialConnectFieldAnswer) QueryConnectFieldCounterpart(fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldCounterpart) TypeReference(org.codehaus.jackson.type.TypeReference) IOException(java.io.IOException) QueryConnectFieldTerm(fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldTerm) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) QueryConnectField(fi.otavanopisto.muikku.plugins.material.model.QueryConnectField)

Aggregations

QueryConnectField (fi.otavanopisto.muikku.plugins.material.model.QueryConnectField)5 QueryConnectFieldTerm (fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldTerm)3 IOException (java.io.IOException)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 QueryConnectFieldCounterpart (fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldCounterpart)2 WorkspaceMaterialConnectFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialConnectFieldAnswer)2 HashMap (java.util.HashMap)2 ConnectFieldConnectionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldConnectionMeta)1 ConnectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta)1 ConnectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldOptionMeta)1 QueryField (fi.otavanopisto.muikku.plugins.material.model.QueryField)1 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 TypeReference (org.codehaus.jackson.type.TypeReference)1