Search in sources :

Example 6 with QueryField

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

the class QueryFieldDAO method findByMaterialAndName.

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

Example 7 with QueryField

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

the class QueryFieldDAO method listByMaterial.

public List<QueryField> listByMaterial(Material material) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<QueryField> criteria = criteriaBuilder.createQuery(QueryField.class);
    Root<QueryField> root = criteria.from(QueryField.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(QueryField_.material), material));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField)

Example 8 with QueryField

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

the class QueryFieldChangeListener method onQueryFieldDelete.

public void onQueryFieldDelete(@Observes QueryFieldDeleteEvent event) {
    QueryField queryField = event.getQueryField();
    List<WorkspaceMaterialField> workspaceMaterialFields = workspaceMaterialFieldController.listWorkspaceMaterialFieldsByQueryField(queryField);
    for (WorkspaceMaterialField workspaceMaterialField : workspaceMaterialFields) {
        workspaceMaterialFieldController.deleteWorkspaceMaterialField(workspaceMaterialField, event.getRemoveAnswers());
    }
}
Also used : QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)

Example 9 with QueryField

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

the class SaveFieldAnswerWebSocketMessageHandler method handleMessage.

public void handleMessage(@Observes @MuikkuWebSocketEvent("workspace:field-answer-save") WebSocketMessageEvent event) {
    // TODO: Localize error messages
    WebSocketMessage webSocketMessage = event.getMessage();
    ObjectMapper mapper = new ObjectMapper();
    try {
        SaveFieldAnswerWebSocketMessage message = mapper.readValue((String) webSocketMessage.getData(), SaveFieldAnswerWebSocketMessage.class);
        Date now = new Date();
        if (message.getMaterialId() == null) {
            logger.log(Level.SEVERE, "Missing material id");
            handleError("Missing material id", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
            return;
        }
        if (message.getWorkspaceMaterialId() == null) {
            logger.log(Level.SEVERE, "Missing workspace material id");
            handleError("Missing workspace material id", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
            return;
        }
        if (message.getUserEntityId() == null) {
            logger.log(Level.SEVERE, String.format("Missing user entity id for ticket %s (field %s in workspace material %d)", event.getTicket(), message.getFieldName(), message.getWorkspaceMaterialId()));
            handleError("Missing user entity id", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
            return;
        }
        Material material = materialController.findMaterialById(message.getMaterialId());
        if (material == null) {
            logger.log(Level.SEVERE, "Could not find material");
            handleError("Could not find material", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
            return;
        }
        UserEntity userEntity = userEntityController.findUserEntityById(message.getUserEntityId());
        if (userEntity == null) {
            logger.log(Level.SEVERE, "Could not find user");
            handleError("Could not find user", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
            return;
        }
        WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(message.getWorkspaceMaterialId());
        if (workspaceMaterial == null) {
            logger.log(Level.SEVERE, "Could not find workspace material");
            handleError("Could not find workspace material", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
            return;
        }
        if (!workspaceMaterial.getMaterialId().equals(material.getId())) {
            logger.log(Level.SEVERE, "Invalid materialId or workspaceMaterialId");
            handleError("Invalid materialId or workspaceMaterialId", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
            return;
        }
        QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(material, message.getFieldName());
        if (queryField == null) {
            logger.log(Level.SEVERE, "Could not find query field");
            handleError("Could not find query field", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
            return;
        }
        WorkspaceMaterialField materialField = workspaceMaterialFieldController.findWorkspaceMaterialFieldByWorkspaceMaterialAndQueryFieldAndEmbedId(workspaceMaterial, queryField, message.getEmbedId());
        if (materialField == null) {
            materialField = workspaceMaterialFieldController.createWorkspaceMaterialField(workspaceMaterial, queryField, message.getEmbedId());
        }
        fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply reply = workspaceMaterialReplyController.findWorkspaceMaterialReplyByWorkspaceMaterialAndUserEntity(workspaceMaterial, userEntity);
        if (reply == null) {
            reply = workspaceMaterialReplyController.createWorkspaceMaterialReply(workspaceMaterial, WorkspaceMaterialReplyState.ANSWERED, userEntity, 1l, now, now);
        } else {
            workspaceMaterialReplyController.incWorkspaceMaterialReplyTries(reply);
        }
        if (workspaceMaterial.getAssignmentType() == WorkspaceMaterialAssignmentType.EVALUATED) {
            switch(reply.getState()) {
                case PASSED:
                case FAILED:
                case SUBMITTED:
                    handleError("Assignment is already submitted thus can not be modified", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
                    return;
                default:
                    break;
            }
        }
        try {
            workspaceMaterialFieldController.storeFieldValue(materialField, reply, message.getAnswer());
        } catch (WorkspaceFieldIOException e) {
            logger.log(Level.SEVERE, "Could not store field value", e);
            handleError("Could not store field value", message.getEmbedId(), message.getMaterialId(), message.getFieldName(), message.getWorkspaceMaterialId(), message.getWorkspaceEntityId(), event.getTicket());
            return;
        }
        message.setOriginTicket(event.getTicket());
        String data = mapper.writeValueAsString(message);
        webSocketMessenger.sendMessage("workspace:field-answer-saved", data, Arrays.asList(userEntity));
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to unmarshal SaveFieldAnswerWebSocketMessage", e);
    }
}
Also used : WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) Material(fi.otavanopisto.muikku.plugins.material.model.Material) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) IOException(java.io.IOException) WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) Date(java.util.Date) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField) WebSocketMessage(fi.otavanopisto.muikku.plugins.websocket.WebSocketMessage) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 10 with QueryField

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

the class QueryFieldChangeListener method onQueryMultiSelectFieldUpdate.

public void onQueryMultiSelectFieldUpdate(@Observes QueryFieldUpdateEvent event) {
    if (event.getMaterialField().getType().equals("application/vnd.muikku.field.multiselect")) {
        QueryField queryField = event.getQueryField();
        List<WorkspaceMaterialField> workspaceMaterialFields = workspaceMaterialFieldController.listWorkspaceMaterialFieldsByQueryField(queryField);
        for (WorkspaceMaterialField workspaceMaterialField : workspaceMaterialFields) {
            workspaceMaterialFieldController.updateWorkspaceMaterialField(workspaceMaterialField, event.getMaterialField(), event.getRemoveAnswers());
        }
    }
}
Also used : QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)

Aggregations

QueryField (fi.otavanopisto.muikku.plugins.material.model.QueryField)17 IOException (java.io.IOException)9 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 WorkspaceMaterialField (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)6 MultiSelectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldMeta)2 MultiSelectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.MultiSelectFieldOptionMeta)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 AudioFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.AudioFieldMeta)1 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 FileFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.FileFieldMeta)1 OrganizerFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta)1 SelectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldMeta)1 SelectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SelectFieldOptionMeta)1 SorterFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta)1 TextFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.TextFieldMeta)1 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)1