use of fi.otavanopisto.pyramus.domainmodel.koski.KoskiPersonState in project pyramus by otavanopisto.
the class KoskiClient method updatePersonToKoski.
private boolean updatePersonToKoski(Oppija oppija, Person person, String personOid) {
try {
String uri = String.format("%s/oppija", getBaseUrl());
Client client = ClientBuilder.newClient();
WebTarget target = client.target(uri);
Builder request = target.request(MediaType.APPLICATION_JSON_TYPE).header("Authorization", "Basic " + getAuth());
ObjectMapper mapper = new ObjectMapper();
StringWriter writer = new StringWriter();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
mapper.writeValue(writer, oppija);
String requestStr = writer.toString();
Response response = request.put(Entity.json(requestStr));
if (response.getStatus() == 200) {
String ret = response.readEntity(String.class);
// For test environment the oid's are not saved
if (!settings.isTestEnvironment()) {
OppijaReturnVal oppijaReturnVal = mapper.readValue(ret, OppijaReturnVal.class);
String servedPersonOid = oppijaReturnVal.getHenkilo().getOid();
if (StringUtils.isEmpty(personOid)) {
// If the oid was empty in db, save the given one
personVariableDAO.setPersonVariable(person, KOSKI_HENKILO_OID, servedPersonOid);
} else {
// Validate the oid is the same
if (!StringUtils.equals(personOid, servedPersonOid))
throw new RuntimeException("Returned person oid doesn't match the saved oid");
}
Set<SourceSystemId> invalidatedSourceSystemIds = oppija.getOpiskeluoikeudet().stream().filter(opiskeluoikeus -> opiskeluoikeus.getTila().getOpiskeluoikeusjaksot().stream().anyMatch(opiskeluoikeusjakso -> opiskeluoikeusjakso.getTila().getValue() == OpiskeluoikeudenTila.mitatoity)).map(opiskeluoikeus -> parseSource(opiskeluoikeus.getLahdejarjestelmanId().getId())).collect(Collectors.toSet());
for (OpiskeluoikeusReturnVal opiskeluoikeus : oppijaReturnVal.getOpiskeluoikeudet()) {
if (opiskeluoikeus.getLahdejarjestelmanId() != null && opiskeluoikeus.getLahdejarjestelmanId().getLahdejarjestelma() != null && StringUtils.equals(opiskeluoikeus.getLahdejarjestelmanId().getLahdejarjestelma().getKoodiarvo(), "pyramus")) {
SourceSystemId sourceSystemId = parseSource(opiskeluoikeus.getLahdejarjestelmanId().getId());
if (sourceSystemId != null) {
Student reportedStudent = studentDAO.findById(sourceSystemId.getStudentId());
KoskiStudentHandler handler = koskiController.getStudentHandler(sourceSystemId.getHandler());
if (reportedStudent.getArchived() || invalidatedSourceSystemIds.contains(sourceSystemId)) {
// For archived or invalidated student the studypermission oid is cleared as Koski doesn't want to receive this id ever again
handler.removeOid(sourceSystemId.getHandler(), reportedStudent, opiskeluoikeus.getOid());
} else {
handler.saveOrValidateOid(sourceSystemId.getHandler(), reportedStudent, opiskeluoikeus.getOid());
}
} else {
logger.log(Level.WARNING, String.format("Could not update student oid because returned source system id couldn't be parsed (Person %d).", person.getId()));
}
} else {
logger.log(Level.WARNING, String.format("Could not update student oid because returned source system was not defined or isn't this system."));
}
}
}
// Log successful event
koskiPersonLogDAO.create(person, KoskiPersonState.SUCCESS, new Date());
logger.info(String.format("KoskiClient: successfully updated person %d.", person.getId()));
return true;
} else {
String ret = response.readEntity(String.class);
String errorMessage = errorResponseMessage(ret);
// Log failed event
koskiPersonLogDAO.create(person, KoskiPersonState.SERVER_FAILURE, new Date(), errorMessage);
logger.log(Level.SEVERE, String.format("Koski server returned %d when trying to create person %d. Content %s", response.getStatus(), person.getId(), ret));
return false;
}
} catch (Exception ex) {
try {
KoskiPersonState reason = KoskiPersonState.UNKNOWN_FAILURE;
if (ex instanceof KoskiException) {
reason = ((KoskiException) ex).getState();
}
if (ex instanceof ProcessingException) {
ProcessingException pe = (ProcessingException) ex;
if (pe.getCause() instanceof ConnectException) {
reason = KoskiPersonState.SERVER_UNAVAILABLE;
}
}
// Log failed event
koskiPersonLogDAO.create(person, reason, new Date(), ex.getMessage());
} catch (Exception e) {
}
logger.log(Level.SEVERE, String.format("Unknown failure while updating person %d", person.getId()), ex);
return false;
}
}
Aggregations