Search in sources :

Example 6 with WorkspaceMaterialField

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

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

the class WorkspaceRESTService method getWorkspaceMaterialAnswers.

@GET
@Path("/workspaces/{WORKSPACEENTITYID}/materials/{WORKSPACEMATERIALID}/compositeMaterialReplies")
@RESTPermitUnimplemented
public Response getWorkspaceMaterialAnswers(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId, @QueryParam("userEntityId") Long userEntityId) {
    // TODO: Security
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).entity("Not logged in").build();
    }
    // TODO Return everyone's answers
    if (userEntityId == null) {
        return Response.status(Status.NOT_IMPLEMENTED).build();
    }
    UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
    WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
    if (workspaceMaterial == null) {
        return Response.status(Status.NOT_FOUND).entity("Workspace material could not be found").build();
    }
    List<WorkspaceMaterialFieldAnswer> answers = new ArrayList<>();
    try {
        fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply reply = workspaceMaterialReplyController.findWorkspaceMaterialReplyByWorkspaceMaterialAndUserEntity(workspaceMaterial, userEntity);
        if (reply != null) {
            List<WorkspaceMaterialField> fields = workspaceMaterialFieldController.listWorkspaceMaterialFieldsByWorkspaceMaterial(workspaceMaterial);
            for (WorkspaceMaterialField field : fields) {
                String value = workspaceMaterialFieldController.retrieveFieldValue(field, reply);
                Material material = field.getQueryField().getMaterial();
                WorkspaceMaterialFieldAnswer answer = new WorkspaceMaterialFieldAnswer(workspaceMaterial.getId(), material.getId(), field.getEmbedId(), field.getQueryField().getName(), value);
                answers.add(answer);
            }
        }
        WorkspaceMaterialCompositeReply result = new WorkspaceMaterialCompositeReply(answers, reply != null ? reply.getState() : null, reply != null ? reply.getCreated() : null, reply != null ? reply.getLastModified() : null, reply != null ? reply.getSubmitted() : null, reply != null ? reply.getWithdrawn() : null);
        return Response.ok(result).build();
    } catch (WorkspaceFieldIOException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Internal error occurred while retrieving field answers: " + e.getMessage()).build();
    }
}
Also used : WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) ArrayList(java.util.ArrayList) WorkspaceMaterialCompositeReply(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialCompositeReply) Material(fi.otavanopisto.muikku.plugins.material.model.Material) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceMaterialFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialFieldAnswer) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 8 with WorkspaceMaterialField

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

the class WorkspaceMaterialFieldChangeListener method onWorkspaceMaterialOrganizerFieldUpdate.

// Create
// Update
// Organizer field
public void onWorkspaceMaterialOrganizerFieldUpdate(@Observes WorkspaceMaterialFieldUpdateEvent event) throws MaterialFieldMetaParsingExeption, WorkspaceMaterialContainsAnswersExeption {
    if (event.getMaterialField().getType().equals("application/vnd.muikku.field.organizer")) {
        ObjectMapper objectMapper = new ObjectMapper();
        OrganizerFieldMeta organizerFieldMeta;
        try {
            organizerFieldMeta = objectMapper.readValue(event.getMaterialField().getContent(), OrganizerFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse organizer field meta", e);
        }
        List<WorkspaceMaterialField> fields = workspaceMaterialFieldDAO.listByQueryField(event.getWorkspaceMaterialField().getQueryField());
        for (WorkspaceMaterialField field : fields) {
            List<WorkspaceMaterialOrganizerFieldAnswer> answers = workspaceMaterialOrganizerFieldAnswerDAO.listByWorkspaceMaterialField(field);
            for (WorkspaceMaterialOrganizerFieldAnswer answer : answers) {
                try {
                    boolean answerModified = false;
                    HashMap<String, Set<String>> answerObject = objectMapper.readValue(answer.getValue(), new TypeReference<HashMap<String, Set<String>>>() {
                    });
                    Set<String> terms = new HashSet<String>();
                    Set<String> categories = answerObject.keySet();
                    for (String category : categories) {
                        terms.addAll(answerObject.get(category));
                        if (!organizerFieldMeta.hasCategoryWithId(category)) {
                            if (!event.getRemoveAnswers()) {
                                throw new WorkspaceMaterialContainsAnswersExeption("Could not update organizer field because it contains answers");
                            } else {
                                answerObject.remove(category);
                                answerModified = true;
                            }
                        }
                    }
                    for (String term : terms) {
                        if (!organizerFieldMeta.hasTermWithId(term)) {
                            if (!event.getRemoveAnswers()) {
                                throw new WorkspaceMaterialContainsAnswersExeption("Could not update organizer field because it contains answers");
                            } else {
                                Iterator<String> categoryIterator = answerObject.keySet().iterator();
                                while (categoryIterator.hasNext()) {
                                    Set<String> categoryTerms = answerObject.get(categoryIterator.next());
                                    if (categoryTerms.contains(term)) {
                                        categoryTerms.remove(term);
                                        answerModified = true;
                                    }
                                }
                            }
                        }
                    }
                    if (answerModified) {
                        workspaceMaterialOrganizerFieldAnswerDAO.updateValue(answer, objectMapper.writeValueAsString(answerObject));
                    }
                } catch (IOException e) {
                    throw new MaterialFieldMetaParsingExeption("Could not parse organizer field answer meta", e);
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) OrganizerFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta) WorkspaceMaterialOrganizerFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer) IOException(java.io.IOException) MaterialFieldMetaParsingExeption(fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) HashSet(java.util.HashSet)

Example 9 with WorkspaceMaterialField

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

the class WorkspaceMaterialFieldChangeListener method onWorkspaceMaterialSorterFieldUpdate.

// Sorter field
public void onWorkspaceMaterialSorterFieldUpdate(@Observes WorkspaceMaterialFieldUpdateEvent event) throws MaterialFieldMetaParsingExeption, WorkspaceMaterialContainsAnswersExeption {
    if (event.getMaterialField().getType().equals("application/vnd.muikku.field.sorter")) {
        ObjectMapper objectMapper = new ObjectMapper();
        SorterFieldMeta sorterFieldMeta;
        try {
            sorterFieldMeta = objectMapper.readValue(event.getMaterialField().getContent(), SorterFieldMeta.class);
        } catch (IOException e) {
            throw new MaterialFieldMetaParsingExeption("Could not parse sorter field meta", e);
        }
        List<WorkspaceMaterialField> fields = workspaceMaterialFieldDAO.listByQueryField(event.getWorkspaceMaterialField().getQueryField());
        for (WorkspaceMaterialField field : fields) {
            List<WorkspaceMaterialSorterFieldAnswer> answers = workspaceMaterialSorterFieldAnswerDAO.listByWorkspaceMaterialField(field);
            for (WorkspaceMaterialSorterFieldAnswer answer : answers) {
                try {
                    boolean answerModified = false;
                    List<String> answerItems = objectMapper.readValue(answer.getValue(), new TypeReference<ArrayList<String>>() {
                    });
                    for (int i = answerItems.size() - 1; i >= 0; i--) {
                        String answerItem = answerItems.get(i);
                        if (!sorterFieldMeta.hasItemWithId(answerItem)) {
                            if (!event.getRemoveAnswers()) {
                                throw new WorkspaceMaterialContainsAnswersExeption("Could not update sorter field because it contains answers");
                            } else {
                                answerItems.remove(answerItem);
                                answerModified = true;
                            }
                        }
                    }
                    if (answerModified) {
                        workspaceMaterialSorterFieldAnswerDAO.updateValue(answer, objectMapper.writeValueAsString(answerItems));
                    }
                } catch (IOException e) {
                    throw new MaterialFieldMetaParsingExeption("Could not parse sorter field answer meta", e);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) WorkspaceMaterialSorterFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialSorterFieldAnswer) SorterFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta) MaterialFieldMetaParsingExeption(fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 10 with WorkspaceMaterialField

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

the class WorkspaceMaterialFieldController method createWorkspaceMaterialField.

public WorkspaceMaterialField createWorkspaceMaterialField(WorkspaceMaterial workspaceMaterial, QueryField queryField, String embedId) {
    WorkspaceMaterialField workspaceMaterialField = workspaceMaterialFieldDAO.create(queryField, workspaceMaterial, embedId);
    workspaceMaterialFieldCreateEvent.fire(new WorkspaceMaterialFieldCreateEvent(workspaceMaterialField));
    return workspaceMaterialField;
}
Also used : WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) WorkspaceMaterialFieldCreateEvent(fi.otavanopisto.muikku.plugins.workspace.events.WorkspaceMaterialFieldCreateEvent)

Aggregations

WorkspaceMaterialField (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)16 QueryField (fi.otavanopisto.muikku.plugins.material.model.QueryField)6 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)3 IOException (java.io.IOException)3 EntityManager (javax.persistence.EntityManager)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)2 MaterialFieldMetaParsingExeption (fi.otavanopisto.muikku.plugins.material.MaterialFieldMetaParsingExeption)2 Material (fi.otavanopisto.muikku.plugins.material.model.Material)2 WorkspaceFieldIOException (fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException)2 ArrayList (java.util.ArrayList)2 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)1 OrganizerFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.OrganizerFieldMeta)1 SorterFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.SorterFieldMeta)1 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)1 WebSocketMessage (fi.otavanopisto.muikku.plugins.websocket.WebSocketMessage)1 WorkspaceMaterialFieldCreateEvent (fi.otavanopisto.muikku.plugins.workspace.events.WorkspaceMaterialFieldCreateEvent)1 WorkspaceMaterialFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFieldAnswer)1 WorkspaceMaterialOrganizerFieldAnswer (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialOrganizerFieldAnswer)1