use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class PyramusCourseMetaSchoolDataBridge method findCurriculum.
@Override
public Curriculum findCurriculum(String identifier) {
Long curriculumId = pyramusIdentifierMapper.getPyramusCurriculumId(identifier);
fi.otavanopisto.pyramus.rest.model.Curriculum curriculum = pyramusClient.get("/common/curriculums/" + curriculumId, fi.otavanopisto.pyramus.rest.model.Curriculum.class);
if (curriculum != null) {
return new PyramusCurriculum(new SchoolDataIdentifier(identifier, getSchoolDataSource()), curriculum.getName());
}
return null;
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class PyramusCourseMetaSchoolDataBridge method findEducationType.
@Override
public EducationType findEducationType(String identifier) {
Long educationTypeId = pyramusIdentifierMapper.getPyramusEducationTypeId(identifier);
fi.otavanopisto.pyramus.rest.model.EducationType restEducationType = pyramusClient.get("/common/educationTypes/" + educationTypeId, fi.otavanopisto.pyramus.rest.model.EducationType.class);
if (restEducationType != null) {
return new PyramusEducationType(new SchoolDataIdentifier(identifier, getSchoolDataSource()), restEducationType.getName());
}
return null;
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class PyramusCourseMetaSchoolDataBridge method listEducationTypes.
@Override
public List<EducationType> listEducationTypes() {
List<EducationType> result = new ArrayList<>();
fi.otavanopisto.pyramus.rest.model.EducationType[] types = pyramusClient.get("/common/educationTypes", fi.otavanopisto.pyramus.rest.model.EducationType[].class);
if (types != null) {
for (fi.otavanopisto.pyramus.rest.model.EducationType type : types) {
SchoolDataIdentifier identifier = pyramusIdentifierMapper.getEducationTypeIdentifier(type.getId());
result.add(new PyramusEducationType(identifier, type.getName()));
}
}
return result;
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class PyramusUpdater method updatePerson.
@Lock(LockType.WRITE)
@AccessTimeout(value = 30000)
public void updatePerson(Person person) {
Long userEntityId = null;
String defaultIdentifier = null;
Long defaultUserId = person.getDefaultUserId();
UserRole defaultUserPyramusRole = null;
List<String> identifiers = new ArrayList<>();
List<String> removedIdentifiers = new ArrayList<>();
List<String> updatedIdentifiers = new ArrayList<>();
List<String> discoveredIdentifiers = new ArrayList<>();
Map<SchoolDataIdentifier, List<String>> emails = new HashMap<>();
// List all person's students and staffMembers
Student[] students = pyramusClient.get().get(String.format("/persons/persons/%d/students", person.getId()), Student[].class);
StaffMember[] staffMembers = pyramusClient.get().get(String.format("/persons/persons/%d/staffMembers", person.getId()), StaffMember[].class);
// If person does not have a defaultUserId specified, we try to guess something
if (defaultUserId == null) {
if ((staffMembers != null) && (staffMembers.length > 0)) {
// If person has a staffMember instance, lets use that one
defaultUserId = staffMembers[0].getId();
} else {
if (students != null) {
// Otherwise just use first non archived student (if any)
for (Student student : students) {
if (!student.getArchived()) {
defaultUserId = student.getId();
break;
}
}
}
}
}
if (students != null) {
// Iterate over all student instances
for (Student student : students) {
String identifier = identifierMapper.getStudentIdentifier(student.getId());
SchoolDataIdentifier schoolDataIdentifier = toIdentifier(identifier);
List<String> identifierEmails = new ArrayList<String>();
if (!student.getArchived()) {
// If student is not archived, add it to identifiers list
identifiers.add(identifier);
// If it's the specified defaultUserId, update defaultIdentifier and role accordingly
if ((defaultIdentifier == null) && student.getId().equals(defaultUserId)) {
defaultIdentifier = identifier;
defaultUserPyramusRole = UserRole.STUDENT;
}
// List emails and add all emails that are not specified non unique (e.g. contact persons) to the emails list
Email[] studentEmails = pyramusClient.get().get("/students/students/" + student.getId() + "/emails", Email[].class);
if (studentEmails != null) {
for (Email studentEmail : studentEmails) {
if (studentEmail.getContactTypeId() != null) {
ContactType contactType = pyramusClient.get().get("/common/contactTypes/" + studentEmail.getContactTypeId(), ContactType.class);
if (!contactType.getNonUnique() && !identifierEmails.contains(studentEmail.getAddress())) {
identifierEmails.add(studentEmail.getAddress());
}
} else {
logger.log(Level.WARNING, "ContactType of email is null - email is ignored");
}
}
}
} else {
// If the student instance if archived, we add it the the removed identifiers list
removedIdentifiers.add(identifier);
}
emails.put(schoolDataIdentifier, identifierEmails);
}
}
if (staffMembers != null) {
for (StaffMember staffMember : staffMembers) {
// Add staffMember identifier into the identifier list
String identifier = identifierMapper.getStaffIdentifier(staffMember.getId());
SchoolDataIdentifier schoolDataIdentifier = toIdentifier(identifier);
List<String> identifierEmails = new ArrayList<String>();
identifiers.add(identifier);
// If it's the specified defaultUserId, update defaultIdentifier and role accordingly
if ((defaultIdentifier == null) && staffMember.getId().equals(defaultUserId)) {
defaultIdentifier = identifier;
defaultUserPyramusRole = staffMember.getRole();
}
// List emails and add all emails that are not specified non unique (e.g. contact persons) to the emails list
Email[] staffMemberEmails = pyramusClient.get().get("/staff/members/" + staffMember.getId() + "/emails", Email[].class);
if (staffMemberEmails != null) {
for (Email staffMemberEmail : staffMemberEmails) {
if (staffMemberEmail.getContactTypeId() != null) {
ContactType contactType = pyramusClient.get().get("/common/contactTypes/" + staffMemberEmail.getContactTypeId(), ContactType.class);
if (!contactType.getNonUnique() && !identifierEmails.contains(staffMemberEmail.getAddress())) {
identifierEmails.add(staffMemberEmail.getAddress());
}
} else {
logger.log(Level.WARNING, "ContactType of email is null - email is ignored");
}
}
}
emails.put(schoolDataIdentifier, identifierEmails);
}
}
// Iterate over all discovered identifiers (students and staff members)
for (String identifier : identifiers) {
UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, identifier);
if (userSchoolDataIdentifier == null) {
// If no user entity can be found by the identifier, add it the the discovered identities list
discoveredIdentifiers.add(identifier);
} else {
// user entity found with given identity, so we need to make sure they all belong to same user
UserEntity userEntity = userSchoolDataIdentifier.getUserEntity();
if (userEntityId == null) {
userEntityId = userEntity.getId();
} else if (!userEntityId.equals(userEntity.getId())) {
logger.warning(String.format("Person %d synchronization failed. Found two userEntitys bound to it (%d and %d)", person.getId(), userEntityId, userEntity.getId()));
return;
}
}
}
UserEntity userEntity = userEntityId != null ? userEntityController.findUserEntityById(userEntityId) : null;
if (userEntity != null) {
// User already exists in the system so we check which of the identifiers have been removed and which just updated
List<UserSchoolDataIdentifier> existingSchoolDataIdentifiers = userSchoolDataIdentifierController.listUserSchoolDataIdentifiersByUserEntity(userEntity);
for (UserSchoolDataIdentifier existingSchoolDataIdentifier : existingSchoolDataIdentifiers) {
if (existingSchoolDataIdentifier.getDataSource().getIdentifier().equals(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE)) {
if (!identifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
if (!removedIdentifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
removedIdentifiers.add(existingSchoolDataIdentifier.getIdentifier());
}
} else if (!discoveredIdentifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
updatedIdentifiers.add(existingSchoolDataIdentifier.getIdentifier());
}
}
}
}
// Resolve the user's desired environment role
SchoolDataIdentifier environmentRoleIdentifier = null;
if (defaultUserPyramusRole != null) {
String roleIdentifier = identifierMapper.getEnvironmentRoleIdentifier(defaultUserPyramusRole);
environmentRoleIdentifier = new SchoolDataIdentifier(roleIdentifier, SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE);
}
// And finally fire the update event
fireSchoolDataUserUpdated(userEntityId, defaultIdentifier, removedIdentifiers, updatedIdentifiers, discoveredIdentifiers, emails, environmentRoleIdentifier);
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class PyramusUpdater method identifierRemoved.
private void identifierRemoved(SchoolDataIdentifier identifier) {
UserSchoolDataIdentifier schoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(identifier.getDataSource(), identifier.getIdentifier());
if (schoolDataIdentifier != null) {
UserEntity userEntity = schoolDataIdentifier.getUserEntity();
List<UserSchoolDataIdentifier> existingIdentifiers = userSchoolDataIdentifierController.listUserSchoolDataIdentifiersByUserEntity(userEntity);
SchoolDataIdentifier defaultIdentifier = null;
List<SchoolDataIdentifier> removedIdentifiers = Arrays.asList(identifier);
List<SchoolDataIdentifier> updatedIdentifiers = new ArrayList<>();
List<SchoolDataIdentifier> discoveredIdentifiers = new ArrayList<>();
Map<SchoolDataIdentifier, List<String>> emails = new HashMap<>();
for (SchoolDataIdentifier removedIdentifier : removedIdentifiers) {
emails.put(removedIdentifier, new ArrayList<String>());
}
for (UserSchoolDataIdentifier existingIdentifier : existingIdentifiers) {
if (!(existingIdentifier.getDataSource().getIdentifier().equals(identifier.getDataSource()) && existingIdentifier.getIdentifier().equals(identifier.getIdentifier()))) {
SchoolDataIdentifier updatedIdentifier = new SchoolDataIdentifier(existingIdentifier.getIdentifier(), existingIdentifier.getDataSource().getIdentifier());
updatedIdentifiers.add(updatedIdentifier);
emails.put(updatedIdentifier, userEmailEntityController.getUserEmailAddresses(updatedIdentifier));
}
}
SchoolDataIdentifier enivormentRoleIdentifier = getUserEntityEnvironmentRoleIdentifier(userEntity);
fireSchoolDataUserUpdated(userEntity.getId(), defaultIdentifier, removedIdentifiers, updatedIdentifiers, discoveredIdentifiers, emails, enivormentRoleIdentifier);
}
}
Aggregations