Commit 72f51ffc authored by Matija Obreza's avatar Matija Obreza

Organization API

parent 0816bf45
......@@ -41,6 +41,7 @@ import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Entity
......@@ -81,10 +82,12 @@ public class FaoInstitute extends BusinessModel implements GeoReferencedEntity,
private boolean current;
@JsonIgnore
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "countryId")
private Country country;
@JsonIgnore
@OneToMany(cascade = {})
@JoinColumn(referencedColumnName = "code", name = "instCode")
@MapKey(name = "setting")
......
......@@ -19,7 +19,6 @@ package org.genesys2.server.model.impl;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
......@@ -36,6 +35,8 @@ import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "organization")
@Indexed
......@@ -52,7 +53,8 @@ public class Organization extends AuditedModel {
@Field(name = "body", store = Store.NO)
private String title;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = FaoInstitute.class)
@JsonIgnore
@ManyToMany(cascade = {}, fetch = FetchType.LAZY, targetEntity = FaoInstitute.class)
@JoinTable(name = "organizationinstitute", joinColumns = @JoinColumn(name = "organizationId"), inverseJoinColumns = @JoinColumn(name = "instituteId"))
@OrderBy("code")
private List<FaoInstitute> members = new ArrayList<FaoInstitute>();
......
......@@ -19,6 +19,7 @@ package org.genesys2.server.service;
import java.util.List;
import java.util.Locale;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Organization;
import org.springframework.data.domain.Page;
......@@ -30,7 +31,7 @@ public interface OrganizationService {
Organization getOrganization(String slug);
void updateBlurp(Organization organization, String blurp, Locale locale);
Article updateBlurp(Organization organization, String blurp, Locale locale);
Organization update(long id, String newSlug, String title);
......@@ -48,4 +49,8 @@ public interface OrganizationService {
boolean addOrganizationInstitutes(Organization organization, List<String> instituteList);
boolean setOrganizationInstitutes(Organization organization, List<String> instituteList);
Organization deleteOrganization(Organization organization);
Article getBlurp(Organization organization, Locale locale);
}
......@@ -24,6 +24,7 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Organization;
import org.genesys2.server.persistence.domain.FaoInstituteRepository;
......@@ -56,16 +57,31 @@ public class OrganizationServiceImpl implements OrganizationService {
return organizationRepository.findBySlug(slug);
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Override
@Transactional
public Organization deleteOrganization(Organization organization) {
organization = organizationRepository.findOne(organization.getId());
organizationRepository.delete(organization);
organization.setId(null);
return organization;
}
@Override
public Page<Organization> list(Pageable pageable) {
return organizationRepository.findAll(pageable);
}
@Override
public Article getBlurp(Organization organization, Locale locale) {
return contentService.getArticle(organization, "blurp", locale);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Transactional(readOnly = false)
public void updateBlurp(Organization organization, String blurp, Locale locale) {
contentService.updateArticle(organization, "blurp", null, blurp, locale);
public Article updateBlurp(Organization organization, String blurp, Locale locale) {
return contentService.updateArticle(organization, "blurp", null, blurp, locale);
}
@Override
......
......@@ -17,13 +17,25 @@
package org.genesys2.server.servlet.controller.rest;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.xml.bind.ValidationException;
import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Organization;
import org.genesys2.server.service.BatchRESTService;
import org.genesys2.server.service.OrganizationService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
......@@ -31,6 +43,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.core.JsonProcessingException;
......@@ -46,9 +59,113 @@ public class OrganizationController extends RestController {
@Autowired
private BatchRESTService batchRESTService;
/**
* List organizations
*
* @return
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
List<Organization> listOrganizations(@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
return organizationService.list(new PageRequest(page - 1, 50)).getContent();
}
/**
* Create or update organization
*
* @return
* @throws ValidationException
*/
@RequestMapping(value = "", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object updateOrganization(@RequestBody Organization organizationJson) throws ValidationException {
final Validator validator = new Validator();
final List<ConstraintViolation> violations = validator.validate(organizationJson);
if (violations.size() > 0) {
// TODO We could do better messages on validation error
throw new ModelValidationException("Validation failed", violations);
}
Organization organization = organizationService.getOrganization(organizationJson.getSlug());
if (organization == null) {
organization = organizationService.create(organizationJson.getSlug(), organizationJson.getTitle());
}
return organizationService.update(organization.getId(), organization.getSlug(), organizationJson.getTitle());
}
/**
* Get organization details
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Organization getOrganization(@PathVariable("shortName") String shortName) throws AuthorizationException {
return organizationService.getOrganization(shortName);
}
/**
* Get organization blurp
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}/blurp/{language}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
String getBlurp(@PathVariable("shortName") String shortName, @PathVariable("language") String language) throws AuthorizationException {
Organization org = organizationService.getOrganization(shortName);
Article article = organizationService.getBlurp(org, new Locale(language));
return article == null ? null : article.getBody();
}
/**
* Update blurp
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}/blurp", method = RequestMethod.PUT, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Article updateBlurp(@PathVariable("shortName") String shortName, @RequestBody OrganizationBlurpJson blurp) throws AuthorizationException {
Organization org = organizationService.getOrganization(shortName);
return organizationService.updateBlurp(org, blurp.blurp, blurp.getLocale());
}
/**
* Get organization details
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}", method = RequestMethod.DELETE, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Organization deleteOrganization(@PathVariable("shortName") String shortName) throws AuthorizationException {
return organizationService.deleteOrganization(organizationService.getOrganization(shortName));
}
/**
* Get organization details
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}/institutes", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Set<String> getOrganizationMembers(@PathVariable("shortName") String shortName) throws AuthorizationException {
Set<String> instCodes = new HashSet<String>();
for (FaoInstitute inst : organizationService.getMembers(organizationService.getOrganization(shortName))) {
instCodes.add(inst.getCode());
}
return instCodes;
}
/**
* Add Institutes to Organization
*
*
* @return
* @throws IOException
* @throws JsonProcessingException
......@@ -67,8 +184,8 @@ public class OrganizationController extends RestController {
}
/**
* Add Institutes to Organization
*
* Set Institutes of Organization
*
* @return
* @throws IOException
* @throws JsonProcessingException
......@@ -86,4 +203,12 @@ public class OrganizationController extends RestController {
return organizationService.setOrganizationInstitutes(organization, instituteList);
}
public static class OrganizationBlurpJson {
public String blurp;
public String language;
public Locale getLocale() {
return language == null ? null : new Locale(language);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment