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();
}
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();
}
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);
}
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));
}
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();
}
Aggregations