Commit 01f76b17 authored by Matija Obreza's avatar Matija Obreza
Browse files

Organization REST controller to manage org. members

parent 5ec8fb37
......@@ -45,4 +45,8 @@ public interface OrganizationService {
List<FaoInstitute> getMembers(Organization organization);
boolean addOrganizationInstitutes(Organization organization, List<String> instituteList);
boolean setOrganizationInstitutes(Organization organization, List<String> instituteList);
}
......@@ -32,6 +32,8 @@ import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.BatchRESTService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.OrganizationService;
import org.genesys2.server.service.TaxonomyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -55,6 +57,12 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Autowired
TaxonomyService taxonomyService;
@Autowired
OrganizationService organizationService;
@Autowired
InstituteService instituteService;
@Override
@Transactional
public boolean upsertAccessionData(FaoInstitute institute, Map<BatchRESTService.DataJson, ObjectNode> batch) {
......@@ -222,4 +230,5 @@ public class BatchRESTServiceImpl implements BatchRESTService {
return toSave.size() > 0 || toSaveColl.size() > 0 || toSaveGeo.size() > 0;
}
}
......@@ -16,8 +16,11 @@
package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -96,4 +99,87 @@ public class OrganizationServiceImpl implements OrganizationService {
throw new NullPointerException("organization");
return organizationRepository.findInstitutesByOrganization(organization);
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
public boolean setOrganizationInstitutes(Organization org, List<String> instituteList) {
LOG.info("Setting institutes for organization " + org);
Organization organization = organizationRepository.findOne(org.getId());
List<FaoInstitute> toRemove = new ArrayList<FaoInstitute>();
// Make set of INSTCODEs
Set<String> existingMembers = new HashSet<String>();
for (FaoInstitute member : organization.getMembers()) {
if (instituteList.contains(member.getCode())) {
existingMembers.add(member.getCode());
} else {
if (LOG.isDebugEnabled())
LOG.debug("Will remove " + member);
toRemove.add(member);
}
}
boolean updated = false;
for (FaoInstitute institute : toRemove) {
LOG.info("Removing " + institute);
organization.getMembers().remove(institute);
updated = true;
}
for (String wiewsCode : instituteList) {
if (!existingMembers.contains(wiewsCode)) {
FaoInstitute newMemberInstitute = instituteRepository.findByCode(wiewsCode);
if (newMemberInstitute == null) {
LOG.warn("No such institute " + wiewsCode);
} else {
LOG.info("Adding " + newMemberInstitute);
organization.getMembers().add(newMemberInstitute);
updated = true;
}
}
}
if (updated) {
LOG.info("Saving " + organization);
organizationRepository.save(organization);
}
return updated;
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
public boolean addOrganizationInstitutes(final Organization organization, final List<String> instituteList) {
LOG.info("Adding institutes to organization " + organization);
// Make set of INSTCODEs
Set<String> existingMembers = new HashSet<String>();
for (FaoInstitute member : organization.getMembers()) {
existingMembers.add(member.getCode());
}
boolean updated = false;
for (String wiewsCode : instituteList) {
if (!existingMembers.contains(wiewsCode)) {
FaoInstitute newMemberInstitute = instituteRepository.findByCode(wiewsCode);
if (newMemberInstitute == null) {
LOG.warn("No such institute " + wiewsCode);
} else {
organization.getMembers().add(newMemberInstitute);
updated = true;
}
}
}
if (updated) {
organizationRepository.save(organization);
}
return updated;
}
}
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.controller.rest;
import java.io.IOException;
import java.util.List;
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.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import com.fasterxml.jackson.core.JsonProcessingException;
@Controller("restOrganizationController")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = { "/api/v0/org", "/json/v0/org" })
public class OrganizationController extends RestController {
@Autowired
private OrganizationService organizationService;
@Autowired
private BatchRESTService batchRESTService;
/**
* Add Institutes to Organization
*
* @return
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{slug}/add-institutes", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
boolean addOrganizationInstitutes(@PathVariable(value = "slug") String slug, @RequestBody List<String> instituteList) throws JsonProcessingException, IOException {
// TODO Check user's permissions to update this organization.
Organization organization = organizationService.getOrganization(slug);
if (organization == null) {
throw new ResourceNotFoundException();
}
return organizationService.addOrganizationInstitutes(organization, instituteList);
}
/**
* Add Institutes to Organization
*
* @return
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{slug}/set-institutes", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
boolean setOrganizationInstitutes(@PathVariable(value = "slug") String slug, @RequestBody List<String> instituteList) throws JsonProcessingException, IOException {
// TODO Check user's permissions to update this organization.
Organization organization = organizationService.getOrganization(slug);
if (organization == null) {
throw new ResourceNotFoundException();
}
return organizationService.setOrganizationInstitutes(organization, instituteList);
}
}
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