Search in sources :

Example 1 with MaterialMetaKey

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

the class MaterialMetaRESTService method listMaterialMetaKeys.

@GET
@Path("/metakeys")
@Produces(MediaType.APPLICATION_JSON)
@RESTPermitUnimplemented
public Response listMaterialMetaKeys() {
    List<MaterialMetaKey> materialMetaKeys = materialController.listMaterialMetaKeys();
    List<String> result = new ArrayList<>(materialMetaKeys.size());
    for (MaterialMetaKey materialMetaKey : materialMetaKeys) {
        result.add(materialMetaKey.getName());
    }
    return Response.ok(result).build();
}
Also used : ArrayList(java.util.ArrayList) MaterialMetaKey(fi.otavanopisto.muikku.plugins.material.model.MaterialMetaKey) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 2 with MaterialMetaKey

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

the class MaterialMetaRESTService method createMaterialMeta.

@POST
@Path("/materials/{id}/meta")
@Produces(MediaType.APPLICATION_JSON)
@RESTPermitUnimplemented
public Response createMaterialMeta(@PathParam("id") Long id, fi.otavanopisto.muikku.plugins.material.rest.MaterialMeta payload) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.MANAGE_MATERIAL_META)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    Material material = materialController.findMaterialById(id);
    if (material == null) {
        return Response.status(Status.NOT_FOUND).entity("Material not found").build();
    }
    if (StringUtils.isBlank(payload.getKey())) {
        return Response.status(Status.BAD_REQUEST).entity("Missing key").build();
    }
    MaterialMetaKey key = materialController.findMaterialMetaKey(payload.getKey());
    if (key == null) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid key").build();
    }
    return Response.ok(createRestModel(materialController.createMaterialMeta(material, key, payload.getValue()))).build();
}
Also used : MaterialMetaKey(fi.otavanopisto.muikku.plugins.material.model.MaterialMetaKey) Material(fi.otavanopisto.muikku.plugins.material.model.Material) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)

Example 3 with MaterialMetaKey

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

the class MaterialMetaKeyDAO method create.

public MaterialMetaKey create(String name) {
    MaterialMetaKey materialMetaKey = new MaterialMetaKey();
    materialMetaKey.setName(name);
    return persist(materialMetaKey);
}
Also used : MaterialMetaKey(fi.otavanopisto.muikku.plugins.material.model.MaterialMetaKey)

Example 4 with MaterialMetaKey

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

the class MaterialMetaKeyDAO method findByName.

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

Example 5 with MaterialMetaKey

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

the class MaterialMetaRESTService method updateMaterialMeta.

@PUT
@Path("/materials/{id}/meta/{KEY}")
@Produces(MediaType.APPLICATION_JSON)
@RESTPermitUnimplemented
public Response updateMaterialMeta(@PathParam("id") Long id, @PathParam("KEY") String key, fi.otavanopisto.muikku.plugins.material.rest.MaterialMeta payload) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.MANAGE_MATERIAL_META)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    if (payload.getKey() != null && !StringUtils.equals(payload.getKey(), key)) {
        return Response.status(Status.BAD_REQUEST).entity("Can not update material meta key").build();
    }
    Material material = materialController.findMaterialById(id);
    if (material == null) {
        return Response.status(Status.NOT_FOUND).entity("Material not found").build();
    }
    MaterialMetaKey materialMetaKey = materialController.findMaterialMetaKey(key);
    if (materialMetaKey == null) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid key").build();
    }
    MaterialMeta materialMeta = materialController.findMaterialMeta(material, materialMetaKey);
    if (materialMeta == null) {
        return Response.status(Status.BAD_REQUEST).entity("MaterialMeta not found").build();
    }
    return Response.ok(createRestModel(materialController.updateMaterialMeta(materialMeta, payload.getValue()))).build();
}
Also used : MaterialMeta(fi.otavanopisto.muikku.plugins.material.model.MaterialMeta) MaterialMetaKey(fi.otavanopisto.muikku.plugins.material.model.MaterialMetaKey) Material(fi.otavanopisto.muikku.plugins.material.model.Material) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) PUT(javax.ws.rs.PUT)

Aggregations

MaterialMetaKey (fi.otavanopisto.muikku.plugins.material.model.MaterialMetaKey)6 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 Material (fi.otavanopisto.muikku.plugins.material.model.Material)3 MaterialMeta (fi.otavanopisto.muikku.plugins.material.model.MaterialMeta)2 GET (javax.ws.rs.GET)2 ArrayList (java.util.ArrayList)1 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1