Add scripts package for migration/export operations

master
parent 4212384e42
commit d11ef469c6

@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@RestController
@RequestMapping("/member")
@ -90,22 +89,9 @@ public class MemberController {
throw new ResourceNotFoundException("Role has not been found");
}
/**
* Get the number of the members of a type(Community, etc.)
*/
@RequestMapping(value = "/{type}/count", method = RequestMethod.GET)
public ResponseEntity<Integer> getCountByType(@PathVariable("type") String type) {
JsonArray cous = registryService.getCous(type + ".");
AtomicInteger counter = new AtomicInteger(0);
cous.forEach(cou -> {
counter.addAndGet(registryService.getUserIdByCouId(cou.getAsJsonObject().get("Id").getAsInt(), false).size());
});
return ResponseEntity.ok(counter.get());
}
/**
* Assign member role to logged in user or user with @email
* If role doesn't exists, use force=true to create and assign the role
* If role doesn't exist, use force=true to create and assign the role
*/
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST)
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email,

@ -0,0 +1,62 @@
package eu.dnetlib.dnetrolemanagement.scripts;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import eu.dnetlib.dnetrolemanagement.entities.User;
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@Component
@Profile("export-managers")
public class TypeManagersExport implements CommandLineRunner {
private static class Manager {
String entity;
String email;
public Manager(String entity, String email) {
this.entity = entity;
this.email = email;
}
public String toCSV() {
return entity + "," + email;
}
}
@Autowired
RegistryService registryService;
private final static String TYPE = "community";
@Override
public void run(String... args) throws Exception {
File file = new File(TYPE + "-managers.csv");
List<Manager> managers = new ArrayList<>();
JsonArray cous = registryService.getCous(TYPE + ".");
cous.forEach(cou -> {
String entity = cou.getAsJsonObject().get("Name").getAsString().replace(TYPE + ".", "");
Integer couId = cou.getAsJsonObject().get("Id").getAsInt();
User[] emails = new Gson().fromJson(registryService.getUserEmailByCouId(couId, true), User[].class);
for(User email:emails) {
managers.add(new Manager(entity, email.getEmail()));
}
});
try (PrintWriter pw = new PrintWriter(file)) {
pw.println(TYPE + ",email");
managers.stream()
.map(Manager::toCSV)
.forEach(pw::println);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Loading…
Cancel
Save