Search in sources :

Example 1 with WorkspaceMaterialConnectFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialConnectFieldAnswer in project muikku by otavanopisto.

the class WorkspaceMaterialConnectFieldAnswerDAO method create.

public WorkspaceMaterialConnectFieldAnswer create(WorkspaceMaterialField field, WorkspaceMaterialReply reply, QueryConnectFieldTerm term, QueryConnectFieldCounterpart counterpart) {
    WorkspaceMaterialConnectFieldAnswer workspaceMaterialConectFieldAnswer = new WorkspaceMaterialConnectFieldAnswer();
    workspaceMaterialConectFieldAnswer.setField(field);
    workspaceMaterialConectFieldAnswer.setReply(reply);
    workspaceMaterialConectFieldAnswer.setTerm(term);
    workspaceMaterialConectFieldAnswer.setCounterpart(counterpart);
    return persist(workspaceMaterialConectFieldAnswer);
}
Also used : WorkspaceMaterialConnectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialConnectFieldAnswer)

Example 2 with WorkspaceMaterialConnectFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialConnectFieldAnswer 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 3 with WorkspaceMaterialConnectFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialConnectFieldAnswer in project muikku by otavanopisto.

the class WorkspaceMaterialConnectFieldAnswerDAO method findByQueryFieldAndReplyAndTerm.

public WorkspaceMaterialConnectFieldAnswer findByQueryFieldAndReplyAndTerm(WorkspaceMaterialField field, WorkspaceMaterialReply reply, QueryConnectFieldTerm term) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceMaterialConnectFieldAnswer> criteria = criteriaBuilder.createQuery(WorkspaceMaterialConnectFieldAnswer.class);
    Root<WorkspaceMaterialConnectFieldAnswer> root = criteria.from(WorkspaceMaterialConnectFieldAnswer.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceMaterialConnectFieldAnswer_.field), field), criteriaBuilder.equal(root.get(WorkspaceMaterialConnectFieldAnswer_.reply), reply), criteriaBuilder.equal(root.get(WorkspaceMaterialConnectFieldAnswer_.term), term)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceMaterialConnectFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialConnectFieldAnswer)

Example 4 with WorkspaceMaterialConnectFieldAnswer

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialConnectFieldAnswer 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

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