Search in sources :

Example 11 with ContactTypeDAO

use of fi.otavanopisto.pyramus.dao.base.ContactTypeDAO in project pyramus by otavanopisto.

the class CreateUserJSONRequestController method process.

/**
 * Processes the request to create a new user. Simply gathers the fields submitted from the
 * web page and adds the user to the database.
 *
 * @param requestContext The JSON request context
 */
public void process(JSONRequestContext requestContext) {
    StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
    AddressDAO addressDAO = DAOFactory.getInstance().getAddressDAO();
    EmailDAO emailDAO = DAOFactory.getInstance().getEmailDAO();
    PhoneNumberDAO phoneNumberDAO = DAOFactory.getInstance().getPhoneNumberDAO();
    TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
    ContactTypeDAO contactTypeDAO = DAOFactory.getInstance().getContactTypeDAO();
    PersonDAO personDAO = DAOFactory.getInstance().getPersonDAO();
    UserIdentificationDAO userIdentificationDAO = DAOFactory.getInstance().getUserIdentificationDAO();
    OrganizationDAO organizationDAO = DAOFactory.getInstance().getOrganizationDAO();
    Long personId = requestContext.getLong("personId");
    int emailCount2 = requestContext.getInteger("emailTable.rowCount");
    for (int i = 0; i < emailCount2; i++) {
        String colPrefix = "emailTable." + i;
        String email = StringUtils.trim(requestContext.getString(colPrefix + ".email"));
        if (StringUtils.isNotBlank(email)) {
            ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
            if (!UserUtils.isAllowedEmail(email, contactType, personId)) {
                throw new RuntimeException(Messages.getInstance().getText(requestContext.getRequest().getLocale(), "generic.errors.emailInUse"));
            }
        }
    }
    // Fields from the web page
    String firstName = requestContext.getString("firstName");
    String lastName = requestContext.getString("lastName");
    String title = requestContext.getString("title");
    Role role = Role.getRole(requestContext.getInteger("role"));
    String tagsText = requestContext.getString("tags");
    String username = requestContext.getString("username");
    String password = requestContext.getString("password1");
    String password2 = requestContext.getString("password2");
    Long organizationId = requestContext.getLong("organizationId");
    User loggedUser = userDAO.findById(requestContext.getLoggedUserId());
    Organization organization = organizationId != null ? organizationDAO.findById(organizationId) : null;
    if (!UserUtils.canAccessOrganization(loggedUser, organization)) {
        throw new SmvcRuntimeException(PyramusStatusCode.UNAUTHORIZED, "Invalid organization.");
    }
    Set<Tag> tagEntities = new HashSet<>();
    if (!StringUtils.isBlank(tagsText)) {
        List<String> tags = Arrays.asList(tagsText.split("[\\ ,]"));
        for (String tag : tags) {
            if (!StringUtils.isBlank(tag)) {
                Tag tagEntity = tagDAO.findByText(tag.trim());
                if (tagEntity == null)
                    tagEntity = tagDAO.create(tag);
                tagEntities.add(tagEntity);
            }
        }
    }
    // User
    Person person = personId != null ? personDAO.findById(personId) : personDAO.create(null, null, null, null, Boolean.FALSE);
    StaffMember user = userDAO.create(organization, firstName, lastName, role, person, false);
    if (title != null)
        userDAO.updateTitle(user, title);
    if (person.getDefaultUser() == null) {
        personDAO.updateDefaultUser(person, user);
    }
    if (AuthenticationProviderVault.getInstance().hasInternalStrategies()) {
        boolean usernameBlank = StringUtils.isBlank(username);
        boolean passwordBlank = StringUtils.isBlank(password);
        // TODO: Support multiple internal authentication sources
        if (!usernameBlank) {
            // #921: Check username
            InternalAuthDAO internalAuthDAO = DAOFactory.getInstance().getInternalAuthDAO();
            InternalAuth internalAuth = internalAuthDAO.findByUsername(username);
            if (internalAuth != null) {
                throw new RuntimeException(Messages.getInstance().getText(requestContext.getRequest().getLocale(), "generic.errors.usernameInUse"));
            }
            InternalAuthenticationProvider internalAuthenticationProvider = AuthenticationProviderVault.getInstance().getInternalAuthenticationProviders().get(0);
            if (!passwordBlank) {
                if (!password.equals(password2))
                    throw new SmvcRuntimeException(PyramusStatusCode.PASSWORD_MISMATCH, "Passwords don't match");
            }
            String externalId = internalAuthenticationProvider.createCredentials(username, password);
            userIdentificationDAO.create(person, internalAuthenticationProvider.getName(), externalId);
        }
    }
    // Tags
    userDAO.updateTags(user, tagEntities);
    // Addresses
    int addressCount = requestContext.getInteger("addressTable.rowCount");
    for (int i = 0; i < addressCount; i++) {
        String colPrefix = "addressTable." + i;
        Boolean defaultAddress = requestContext.getBoolean(colPrefix + ".defaultAddress");
        ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
        String name = requestContext.getString(colPrefix + ".name");
        String street = requestContext.getString(colPrefix + ".street");
        String postal = requestContext.getString(colPrefix + ".postal");
        String city = requestContext.getString(colPrefix + ".city");
        String country = requestContext.getString(colPrefix + ".country");
        boolean hasAddress = name != null || street != null || postal != null || city != null || country != null;
        if (hasAddress) {
            addressDAO.create(user.getContactInfo(), contactType, name, street, postal, city, country, defaultAddress);
        }
    }
    // Email addresses
    int emailCount = requestContext.getInteger("emailTable.rowCount");
    for (int i = 0; i < emailCount; i++) {
        String colPrefix = "emailTable." + i;
        Boolean defaultAddress = requestContext.getBoolean(colPrefix + ".defaultAddress");
        ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
        String email = StringUtils.trim(requestContext.getString(colPrefix + ".email"));
        if (StringUtils.isNotBlank(email)) {
            emailDAO.create(user.getContactInfo(), contactType, defaultAddress, email);
        }
    }
    // Phone numbers
    int phoneCount = requestContext.getInteger("phoneTable.rowCount");
    for (int i = 0; i < phoneCount; i++) {
        String colPrefix = "phoneTable." + i;
        Boolean defaultNumber = requestContext.getBoolean(colPrefix + ".defaultNumber");
        ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
        String number = requestContext.getString(colPrefix + ".phone");
        if (number != null) {
            phoneNumberDAO.create(user.getContactInfo(), contactType, defaultNumber, number);
        }
    }
    // Redirect to the Edit User view
    requestContext.setRedirectURL(requestContext.getRequest().getContextPath() + "/users/edituser.page?userId=" + user.getId());
}
Also used : PhoneNumberDAO(fi.otavanopisto.pyramus.dao.base.PhoneNumberDAO) ContactType(fi.otavanopisto.pyramus.domainmodel.base.ContactType) User(fi.otavanopisto.pyramus.domainmodel.users.User) Organization(fi.otavanopisto.pyramus.domainmodel.base.Organization) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember) EmailDAO(fi.otavanopisto.pyramus.dao.base.EmailDAO) PersonDAO(fi.otavanopisto.pyramus.dao.base.PersonDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) InternalAuthenticationProvider(fi.otavanopisto.pyramus.plugin.auth.InternalAuthenticationProvider) ContactTypeDAO(fi.otavanopisto.pyramus.dao.base.ContactTypeDAO) InternalAuth(fi.otavanopisto.pyramus.domainmodel.users.InternalAuth) OrganizationDAO(fi.otavanopisto.pyramus.dao.base.OrganizationDAO) AddressDAO(fi.otavanopisto.pyramus.dao.base.AddressDAO) UserIdentificationDAO(fi.otavanopisto.pyramus.dao.users.UserIdentificationDAO) HashSet(java.util.HashSet) TagDAO(fi.otavanopisto.pyramus.dao.base.TagDAO) UserRole(fi.otavanopisto.pyramus.framework.UserRole) Role(fi.otavanopisto.pyramus.domainmodel.users.Role) InternalAuthDAO(fi.otavanopisto.pyramus.dao.users.InternalAuthDAO) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag) Person(fi.otavanopisto.pyramus.domainmodel.base.Person)

Example 12 with ContactTypeDAO

use of fi.otavanopisto.pyramus.dao.base.ContactTypeDAO in project pyramus by otavanopisto.

the class EditUserJSONRequestController method process.

/**
 * Processes the request to edit an user. Simply gathers the fields submitted from the
 * web page and updates the database.
 *
 * @param jsonRequestContext The JSON request context
 */
public void process(JSONRequestContext requestContext) {
    StaffMemberDAO staffDAO = DAOFactory.getInstance().getStaffMemberDAO();
    UserVariableDAO userVariableDAO = DAOFactory.getInstance().getUserVariableDAO();
    AddressDAO addressDAO = DAOFactory.getInstance().getAddressDAO();
    EmailDAO emailDAO = DAOFactory.getInstance().getEmailDAO();
    PhoneNumberDAO phoneNumberDAO = DAOFactory.getInstance().getPhoneNumberDAO();
    TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
    ContactTypeDAO contactTypeDAO = DAOFactory.getInstance().getContactTypeDAO();
    UserIdentificationDAO userIdentificationDAO = DAOFactory.getInstance().getUserIdentificationDAO();
    OrganizationDAO organizationDAO = DAOFactory.getInstance().getOrganizationDAO();
    Long loggedUserId = requestContext.getLoggedUserId();
    StaffMember loggedUser = staffDAO.findById(loggedUserId);
    Role loggedUserRole = loggedUser.getRole();
    Long userId = requestContext.getLong("userId");
    StaffMember user = staffDAO.findById(userId);
    if (user.getOrganization() != null) {
        // Check that the editing user has access to the organization
        if (!UserUtils.canAccessOrganization(loggedUser, user.getOrganization())) {
            throw new RuntimeException("Cannot access users' organization");
        }
    } else {
        // Check that the editing user has generic access when users' organization is null
        if (!UserUtils.canAccessAllOrganizations(loggedUser)) {
            throw new RuntimeException("Cannot access users' organization");
        }
    }
    String firstName = requestContext.getString("firstName");
    String lastName = requestContext.getString("lastName");
    String title = requestContext.getString("title");
    Role role = Role.getRole(requestContext.getInteger("role").intValue());
    String username = requestContext.getString("username");
    String password = requestContext.getString("password1");
    String password2 = requestContext.getString("password2");
    String tagsText = requestContext.getString("tags");
    Long organizationId = requestContext.getLong("organizationId");
    Organization organization = null;
    if (organizationId != null) {
        organization = organizationDAO.findById(organizationId);
    }
    if (organization != null) {
        // Check that the editing user has access to the organization
        if (!UserUtils.canAccessOrganization(loggedUser, organization)) {
            throw new RuntimeException("Cannot access organization");
        }
    } else {
        // Check that the editing user can set the organization as null
        if (!UserUtils.canAccessAllOrganizations(loggedUser)) {
            throw new RuntimeException("Cannot access organization");
        }
    }
    // #921: Check username
    if (!StringUtils.isBlank(username)) {
        InternalAuthDAO internalAuthDAO = DAOFactory.getInstance().getInternalAuthDAO();
        InternalAuth internalAuth = internalAuthDAO.findByUsername(username);
        if (internalAuth != null) {
            UserIdentification userIdentification = userIdentificationDAO.findByAuthSourceAndExternalId("internal", internalAuth.getId().toString());
            if (userIdentification != null && !user.getPerson().getId().equals(userIdentification.getPerson().getId())) {
                throw new RuntimeException(Messages.getInstance().getText(requestContext.getRequest().getLocale(), "generic.errors.usernameInUse"));
            }
        }
    }
    int emailCount2 = requestContext.getInteger("emailTable.rowCount");
    for (int i = 0; i < emailCount2; i++) {
        String colPrefix = "emailTable." + i;
        String email = StringUtils.trim(requestContext.getString(colPrefix + ".email"));
        if (StringUtils.isNotBlank(email)) {
            ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
            if (!UserUtils.isAllowedEmail(email, contactType, user.getPerson().getId())) {
                throw new RuntimeException(Messages.getInstance().getText(requestContext.getRequest().getLocale(), "generic.errors.emailInUse"));
            }
        }
    }
    Set<Tag> tagEntities = new HashSet<>();
    if (!StringUtils.isBlank(tagsText)) {
        List<String> tags = Arrays.asList(tagsText.split("[\\ ,]"));
        for (String tag : tags) {
            if (!StringUtils.isBlank(tag)) {
                Tag tagEntity = tagDAO.findByText(tag.trim());
                if (tagEntity == null)
                    tagEntity = tagDAO.create(tag);
                tagEntities.add(tagEntity);
            }
        }
    }
    staffDAO.update(user, organization, firstName, lastName, role);
    if (Role.ADMINISTRATOR.equals(loggedUserRole)) {
        Integer propertyCount = requestContext.getInteger("propertiesTable.rowCount");
        for (int i = 0; i < (propertyCount != null ? propertyCount : 0); i++) {
            String colPrefix = "propertiesTable." + i;
            String propertyKey = requestContext.getString(colPrefix + ".key");
            String propertyValue = requestContext.getString(colPrefix + ".value");
            if (StaffMemberProperties.isProperty(propertyKey)) {
                user.getProperties().put(propertyKey, propertyValue);
            }
        }
    }
    staffDAO.updateTitle(user, title);
    // SSN
    String ssn = requestContext.getString("ssn");
    String existingSsn = user.getPerson().getSocialSecurityNumber();
    if (!StringUtils.equals(ssn, existingSsn)) {
        PersonDAO personDAO = DAOFactory.getInstance().getPersonDAO();
        personDAO.updateSocialSecurityNumber(user.getPerson(), ssn);
    }
    // Tags
    staffDAO.updateTags(user, tagEntities);
    // Addresses
    Set<Long> existingAddresses = new HashSet<>();
    int addressCount = requestContext.getInteger("addressTable.rowCount");
    for (int i = 0; i < addressCount; i++) {
        String colPrefix = "addressTable." + i;
        Long addressId = requestContext.getLong(colPrefix + ".addressId");
        Boolean defaultAddress = requestContext.getBoolean(colPrefix + ".defaultAddress");
        ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
        String name = requestContext.getString(colPrefix + ".name");
        String street = requestContext.getString(colPrefix + ".street");
        String postal = requestContext.getString(colPrefix + ".postal");
        String city = requestContext.getString(colPrefix + ".city");
        String country = requestContext.getString(colPrefix + ".country");
        boolean hasAddress = name != null || street != null || postal != null || city != null || country != null;
        if (addressId == -1 && hasAddress) {
            Address address = addressDAO.create(user.getContactInfo(), contactType, name, street, postal, city, country, defaultAddress);
            existingAddresses.add(address.getId());
        } else if (addressId > 0) {
            Address address = addressDAO.findById(addressId);
            if (hasAddress) {
                existingAddresses.add(addressId);
                addressDAO.update(address, defaultAddress, contactType, name, street, postal, city, country);
            }
        }
    }
    List<Address> addresses = user.getContactInfo().getAddresses();
    for (int i = addresses.size() - 1; i >= 0; i--) {
        Address address = addresses.get(i);
        if (!existingAddresses.contains(address.getId())) {
            addressDAO.delete(address);
        }
    }
    // E-mail addresses
    Set<Long> existingEmails = new HashSet<>();
    int emailCount = requestContext.getInteger("emailTable.rowCount");
    for (int i = 0; i < emailCount; i++) {
        String colPrefix = "emailTable." + i;
        Boolean defaultAddress = requestContext.getBoolean(colPrefix + ".defaultAddress");
        ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
        String email = StringUtils.trim(requestContext.getString(colPrefix + ".email"));
        Long emailId = requestContext.getLong(colPrefix + ".emailId");
        if (emailId == -1 && email != null) {
            emailId = emailDAO.create(user.getContactInfo(), contactType, defaultAddress, email).getId();
            existingEmails.add(emailId);
        } else if (emailId > 0 && email != null) {
            existingEmails.add(emailId);
            emailDAO.update(emailDAO.findById(emailId), contactType, defaultAddress, email);
        }
    }
    List<Email> emails = user.getContactInfo().getEmails();
    for (int i = emails.size() - 1; i >= 0; i--) {
        Email email = emails.get(i);
        if (!existingEmails.contains(email.getId())) {
            emailDAO.delete(email);
        }
    }
    // Phone numbers
    Set<Long> existingPhoneNumbers = new HashSet<>();
    int phoneCount = requestContext.getInteger("phoneTable.rowCount");
    for (int i = 0; i < phoneCount; i++) {
        String colPrefix = "phoneTable." + i;
        Boolean defaultNumber = requestContext.getBoolean(colPrefix + ".defaultNumber");
        ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
        String number = requestContext.getString(colPrefix + ".phone");
        Long phoneId = requestContext.getLong(colPrefix + ".phoneId");
        if (phoneId == -1 && number != null) {
            phoneId = phoneNumberDAO.create(user.getContactInfo(), contactType, defaultNumber, number).getId();
            existingPhoneNumbers.add(phoneId);
        } else if (phoneId > 0 && number != null) {
            phoneNumberDAO.update(phoneNumberDAO.findById(phoneId), contactType, defaultNumber, number);
            existingPhoneNumbers.add(phoneId);
        }
    }
    List<PhoneNumber> phoneNumbers = user.getContactInfo().getPhoneNumbers();
    for (int i = phoneNumbers.size() - 1; i >= 0; i--) {
        PhoneNumber phoneNumber = phoneNumbers.get(i);
        if (!existingPhoneNumbers.contains(phoneNumber.getId())) {
            phoneNumberDAO.delete(phoneNumber);
        }
    }
    if (Role.ADMINISTRATOR.equals(loggedUserRole)) {
        Integer variableCount = requestContext.getInteger("variablesTable.rowCount");
        for (int i = 0; i < (variableCount != null ? variableCount : 0); i++) {
            String colPrefix = "variablesTable." + i;
            String variableKey = requestContext.getString(colPrefix + ".key");
            String variableValue = requestContext.getString(colPrefix + ".value");
            userVariableDAO.setUserVariable(user, variableKey, variableValue);
        }
    }
    boolean usernameBlank = StringUtils.isBlank(username);
    boolean passwordBlank = StringUtils.isBlank(password);
    if (!usernameBlank || !passwordBlank) {
        if (!passwordBlank) {
            if (!password.equals(password2))
                throw new SmvcRuntimeException(PyramusStatusCode.PASSWORD_MISMATCH, "Passwords don't match");
        }
        // TODO: Support for multiple internal authentication providers
        List<InternalAuthenticationProvider> internalAuthenticationProviders = AuthenticationProviderVault.getInstance().getInternalAuthenticationProviders();
        if (internalAuthenticationProviders.size() == 1) {
            InternalAuthenticationProvider internalAuthenticationProvider = internalAuthenticationProviders.get(0);
            if (internalAuthenticationProvider != null) {
                UserIdentification userIdentification = userIdentificationDAO.findByAuthSourceAndPerson(internalAuthenticationProvider.getName(), user.getPerson());
                if (internalAuthenticationProvider.canUpdateCredentials()) {
                    if (userIdentification == null) {
                        String externalId = internalAuthenticationProvider.createCredentials(username, password);
                        userIdentificationDAO.create(user.getPerson(), internalAuthenticationProvider.getName(), externalId);
                    } else {
                        if ("-1".equals(userIdentification.getExternalId())) {
                            String externalId = internalAuthenticationProvider.createCredentials(username, password);
                            userIdentificationDAO.updateExternalId(userIdentification, externalId);
                        } else {
                            if (!StringUtils.isBlank(username))
                                internalAuthenticationProvider.updateUsername(userIdentification.getExternalId(), username);
                            if (!StringUtils.isBlank(password))
                                internalAuthenticationProvider.updatePassword(userIdentification.getExternalId(), password);
                        }
                    }
                }
            }
        }
    }
    if (requestContext.getLoggedUserId().equals(user.getId())) {
        user = staffDAO.findById(user.getId());
        HttpSession session = requestContext.getRequest().getSession(true);
        session.setAttribute("loggedUserName", user.getFullName());
        session.setAttribute("loggedUserRole", Role.valueOf(user.getRole().name()));
    }
    requestContext.setRedirectURL(requestContext.getReferer(true));
}
Also used : PhoneNumberDAO(fi.otavanopisto.pyramus.dao.base.PhoneNumberDAO) Organization(fi.otavanopisto.pyramus.domainmodel.base.Organization) ContactType(fi.otavanopisto.pyramus.domainmodel.base.ContactType) Email(fi.otavanopisto.pyramus.domainmodel.base.Email) Address(fi.otavanopisto.pyramus.domainmodel.base.Address) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember) EmailDAO(fi.otavanopisto.pyramus.dao.base.EmailDAO) PersonDAO(fi.otavanopisto.pyramus.dao.base.PersonDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) UserVariableDAO(fi.otavanopisto.pyramus.dao.users.UserVariableDAO) InternalAuthenticationProvider(fi.otavanopisto.pyramus.plugin.auth.InternalAuthenticationProvider) ContactTypeDAO(fi.otavanopisto.pyramus.dao.base.ContactTypeDAO) InternalAuth(fi.otavanopisto.pyramus.domainmodel.users.InternalAuth) OrganizationDAO(fi.otavanopisto.pyramus.dao.base.OrganizationDAO) AddressDAO(fi.otavanopisto.pyramus.dao.base.AddressDAO) UserIdentificationDAO(fi.otavanopisto.pyramus.dao.users.UserIdentificationDAO) HashSet(java.util.HashSet) TagDAO(fi.otavanopisto.pyramus.dao.base.TagDAO) HttpSession(javax.servlet.http.HttpSession) UserRole(fi.otavanopisto.pyramus.framework.UserRole) Role(fi.otavanopisto.pyramus.domainmodel.users.Role) InternalAuthDAO(fi.otavanopisto.pyramus.dao.users.InternalAuthDAO) PhoneNumber(fi.otavanopisto.pyramus.domainmodel.base.PhoneNumber) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag) UserIdentification(fi.otavanopisto.pyramus.domainmodel.users.UserIdentification)

Example 13 with ContactTypeDAO

use of fi.otavanopisto.pyramus.dao.base.ContactTypeDAO in project pyramus by otavanopisto.

the class BaseService method updateAddress.

public void updateAddress(@WebParam(name = "addressId") Long addressId, @WebParam(name = "defaultAddress") Boolean defaultAddress, @WebParam(name = "contactTypeId") Long contactTypeId, @WebParam(name = "name") String name, @WebParam(name = "streetAddress") String streetAddress, @WebParam(name = "postalCode") String postalCode, @WebParam(name = "city") String city, @WebParam(name = "country") String country) {
    AddressDAO addressDAO = DAOFactory.getInstance().getAddressDAO();
    ContactTypeDAO contactTypeDAO = DAOFactory.getInstance().getContactTypeDAO();
    Address address = addressDAO.findById(addressId);
    ContactType contactType = contactTypeDAO.findById(contactTypeId);
    addressDAO.update(address, defaultAddress, contactType, name, streetAddress, postalCode, city, country);
}
Also used : ContactType(fi.otavanopisto.pyramus.domainmodel.base.ContactType) Address(fi.otavanopisto.pyramus.domainmodel.base.Address) ContactTypeDAO(fi.otavanopisto.pyramus.dao.base.ContactTypeDAO) AddressDAO(fi.otavanopisto.pyramus.dao.base.AddressDAO)

Example 14 with ContactTypeDAO

use of fi.otavanopisto.pyramus.dao.base.ContactTypeDAO in project pyramus by otavanopisto.

the class EditSchoolJSONRequestController method process.

/**
 * Processes the request to create a new grading scale.
 *
 * @param requestContext The JSON request context
 */
public void process(JSONRequestContext requestContext) {
    SchoolDAO schoolDAO = DAOFactory.getInstance().getSchoolDAO();
    SchoolFieldDAO schoolFieldDAO = DAOFactory.getInstance().getSchoolFieldDAO();
    SchoolVariableDAO schoolVariableDAO = DAOFactory.getInstance().getSchoolVariableDAO();
    AddressDAO addressDAO = DAOFactory.getInstance().getAddressDAO();
    EmailDAO emailDAO = DAOFactory.getInstance().getEmailDAO();
    PhoneNumberDAO phoneNumberDAO = DAOFactory.getInstance().getPhoneNumberDAO();
    TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
    ContactTypeDAO contactTypeDAO = DAOFactory.getInstance().getContactTypeDAO();
    BillingDetailsDAO billingDetailsDAO = DAOFactory.getInstance().getBillingDetailsDAO();
    Long schoolId = NumberUtils.createLong(requestContext.getRequest().getParameter("schoolId"));
    School school = schoolDAO.findById(schoolId);
    Long schoolFieldId = requestContext.getLong("schoolFieldId");
    SchoolField schoolField = null;
    if ((schoolFieldId != null) && (schoolFieldId.intValue() >= 0))
        schoolField = schoolFieldDAO.findById(schoolFieldId);
    String schoolCode = requestContext.getString("code");
    String schoolName = requestContext.getString("name");
    String tagsText = requestContext.getString("tags");
    Set<Tag> tagEntities = new HashSet<>();
    if (!StringUtils.isBlank(tagsText)) {
        List<String> tags = Arrays.asList(tagsText.split("[\\ ,]"));
        for (String tag : tags) {
            if (!StringUtils.isBlank(tag)) {
                Tag tagEntity = tagDAO.findByText(tag.trim());
                if (tagEntity == null)
                    tagEntity = tagDAO.create(tag);
                tagEntities.add(tagEntity);
            }
        }
    }
    schoolDAO.update(school, schoolCode, schoolName, schoolField);
    // BillingDetails
    String billingPersonName = requestContext.getString("billingDetailsPersonName");
    String billingCompanyName = requestContext.getString("billingDetailsCompanyName");
    String billingStreetAddress1 = requestContext.getString("billingDetailsStreetAddress1");
    String billingStreetAddress2 = requestContext.getString("billingDetailsStreetAddress2");
    String billingPostalCode = requestContext.getString("billingDetailsPostalCode");
    String billingCity = requestContext.getString("billingDetailsCity");
    String billingRegion = requestContext.getString("billingDetailsRegion");
    String billingCountry = requestContext.getString("billingDetailsCountry");
    String billingPhoneNumber = requestContext.getString("billingDetailsPhoneNumber");
    String billingEmailAddress = requestContext.getString("billingDetailsEmailAddress");
    String billingElectronicBillingAddress = requestContext.getString("billingDetailsElectronicBillingAddress");
    String billingElectronicBillingOperator = requestContext.getString("billingDetailsElectronicBillingOperator");
    String billingCompanyIdentifier = requestContext.getString("billingDetailsCompanyIdentifier");
    String billingReferenceNumber = requestContext.getString("billingDetailsReferenceNumber");
    String billingNotes = requestContext.getString("billingDetailsNotes");
    if (school.getBillingDetails() == null) {
        BillingDetails billingDetails = billingDetailsDAO.create(billingPersonName, billingCompanyName, billingStreetAddress1, billingStreetAddress2, billingPostalCode, billingCity, billingRegion, billingCountry, billingPhoneNumber, billingEmailAddress, billingElectronicBillingAddress, billingElectronicBillingOperator, billingCompanyIdentifier, billingReferenceNumber, billingNotes);
        schoolDAO.updateBillingDetails(school, billingDetails);
    } else {
        BillingDetails billingDetails = school.getBillingDetails();
        billingDetailsDAO.updatePersonName(billingDetails, billingPersonName);
        billingDetailsDAO.updateCompanyName(billingDetails, billingCompanyName);
        billingDetailsDAO.updateStreetAddress1(billingDetails, billingStreetAddress1);
        billingDetailsDAO.updateStreetAddress2(billingDetails, billingStreetAddress2);
        billingDetailsDAO.updatePostalCode(billingDetails, billingPostalCode);
        billingDetailsDAO.updateCity(billingDetails, billingCity);
        billingDetailsDAO.updateRegion(billingDetails, billingRegion);
        billingDetailsDAO.updateCountry(billingDetails, billingCountry);
        billingDetailsDAO.updatePhoneNumber(billingDetails, billingPhoneNumber);
        billingDetailsDAO.updateEmailAddress(billingDetails, billingEmailAddress);
        billingDetailsDAO.updateElectronicBillingAddress(billingDetails, billingElectronicBillingAddress);
        billingDetailsDAO.updateElectronicBillingOperator(billingDetails, billingElectronicBillingOperator);
        billingDetailsDAO.updateCompanyIdentifier(billingDetails, billingCompanyIdentifier);
        billingDetailsDAO.updateReferenceNumber(billingDetails, billingReferenceNumber);
        billingDetailsDAO.updateNotes(billingDetails, billingNotes);
    }
    // Tags
    schoolDAO.updateTags(school, tagEntities);
    // Addresses
    Set<Long> existingAddresses = new HashSet<>();
    int addressCount = requestContext.getInteger("addressTable.rowCount");
    for (int i = 0; i < addressCount; i++) {
        String colPrefix = "addressTable." + i;
        Long addressId = requestContext.getLong(colPrefix + ".addressId");
        Boolean defaultAddress = requestContext.getBoolean(colPrefix + ".defaultAddress");
        ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
        String name = requestContext.getString(colPrefix + ".name");
        String street = requestContext.getString(colPrefix + ".street");
        String postal = requestContext.getString(colPrefix + ".postal");
        String city = requestContext.getString(colPrefix + ".city");
        String country = requestContext.getString(colPrefix + ".country");
        boolean hasAddress = name != null || street != null || postal != null || city != null || country != null;
        if (addressId == -1 && hasAddress) {
            Address address = addressDAO.create(school.getContactInfo(), contactType, name, street, postal, city, country, defaultAddress);
            existingAddresses.add(address.getId());
        } else if (addressId > 0) {
            Address address = addressDAO.findById(addressId);
            if (hasAddress) {
                existingAddresses.add(addressId);
                addressDAO.update(address, defaultAddress, contactType, name, street, postal, city, country);
            }
        }
    }
    List<Address> addresses = school.getContactInfo().getAddresses();
    for (int i = addresses.size() - 1; i >= 0; i--) {
        Address address = addresses.get(i);
        if (!existingAddresses.contains(address.getId())) {
            addressDAO.delete(address);
        }
    }
    // E-mail addresses
    Set<Long> existingEmails = new HashSet<>();
    int emailCount = requestContext.getInteger("emailTable.rowCount");
    for (int i = 0; i < emailCount; i++) {
        String colPrefix = "emailTable." + i;
        Boolean defaultAddress = requestContext.getBoolean(colPrefix + ".defaultAddress");
        ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
        String email = requestContext.getString(colPrefix + ".email");
        // Trim the email address
        email = email != null ? email.trim() : null;
        Long emailId = requestContext.getLong(colPrefix + ".emailId");
        if (emailId == -1 && email != null) {
            emailId = emailDAO.create(school.getContactInfo(), contactType, defaultAddress, email).getId();
            existingEmails.add(emailId);
        } else if (emailId > 0 && email != null) {
            existingEmails.add(emailId);
            emailDAO.update(emailDAO.findById(emailId), contactType, defaultAddress, email);
        }
    }
    List<Email> emails = school.getContactInfo().getEmails();
    for (int i = emails.size() - 1; i >= 0; i--) {
        Email email = emails.get(i);
        if (!existingEmails.contains(email.getId())) {
            emailDAO.delete(email);
        }
    }
    // Phone numbers
    Set<Long> existingPhoneNumbers = new HashSet<>();
    int phoneCount = requestContext.getInteger("phoneTable.rowCount");
    for (int i = 0; i < phoneCount; i++) {
        String colPrefix = "phoneTable." + i;
        Boolean defaultNumber = requestContext.getBoolean(colPrefix + ".defaultNumber");
        ContactType contactType = contactTypeDAO.findById(requestContext.getLong(colPrefix + ".contactTypeId"));
        String number = requestContext.getString(colPrefix + ".phone");
        Long phoneId = requestContext.getLong(colPrefix + ".phoneId");
        if (phoneId == -1 && number != null) {
            phoneId = phoneNumberDAO.create(school.getContactInfo(), contactType, defaultNumber, number).getId();
            existingPhoneNumbers.add(phoneId);
        } else if (phoneId > 0 && number != null) {
            phoneNumberDAO.update(phoneNumberDAO.findById(phoneId), contactType, defaultNumber, number);
            existingPhoneNumbers.add(phoneId);
        }
    }
    List<PhoneNumber> phoneNumbers = school.getContactInfo().getPhoneNumbers();
    for (int i = phoneNumbers.size() - 1; i >= 0; i--) {
        PhoneNumber phoneNumber = phoneNumbers.get(i);
        if (!existingPhoneNumbers.contains(phoneNumber.getId())) {
            phoneNumberDAO.delete(phoneNumber);
        }
    }
    // Variables
    Integer variableCount = requestContext.getInteger("variablesTable.rowCount");
    if (variableCount != null) {
        for (int i = 0; i < variableCount; i++) {
            String colPrefix = "variablesTable." + i;
            String key = requestContext.getRequest().getParameter(colPrefix + ".key");
            String value = requestContext.getRequest().getParameter(colPrefix + ".value");
            schoolVariableDAO.setVariable(school, key, value);
        }
    }
}
Also used : PhoneNumberDAO(fi.otavanopisto.pyramus.dao.base.PhoneNumberDAO) ContactType(fi.otavanopisto.pyramus.domainmodel.base.ContactType) Email(fi.otavanopisto.pyramus.domainmodel.base.Email) Address(fi.otavanopisto.pyramus.domainmodel.base.Address) EmailDAO(fi.otavanopisto.pyramus.dao.base.EmailDAO) School(fi.otavanopisto.pyramus.domainmodel.base.School) BillingDetailsDAO(fi.otavanopisto.pyramus.dao.base.BillingDetailsDAO) SchoolDAO(fi.otavanopisto.pyramus.dao.base.SchoolDAO) ContactTypeDAO(fi.otavanopisto.pyramus.dao.base.ContactTypeDAO) SchoolVariableDAO(fi.otavanopisto.pyramus.dao.base.SchoolVariableDAO) AddressDAO(fi.otavanopisto.pyramus.dao.base.AddressDAO) HashSet(java.util.HashSet) SchoolField(fi.otavanopisto.pyramus.domainmodel.base.SchoolField) TagDAO(fi.otavanopisto.pyramus.dao.base.TagDAO) BillingDetails(fi.otavanopisto.pyramus.domainmodel.base.BillingDetails) SchoolFieldDAO(fi.otavanopisto.pyramus.dao.base.SchoolFieldDAO) PhoneNumber(fi.otavanopisto.pyramus.domainmodel.base.PhoneNumber) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag)

Example 15 with ContactTypeDAO

use of fi.otavanopisto.pyramus.dao.base.ContactTypeDAO in project pyramus by otavanopisto.

the class StaffMemberAPI method addEmail.

public void addEmail(Long staffMemberId, Long contactTypeId, String address, Boolean defaultAddress) throws InvalidScriptException {
    StaffMemberDAO staffMemberDAO = DAOFactory.getInstance().getStaffMemberDAO();
    EmailDAO emailDAO = DAOFactory.getInstance().getEmailDAO();
    PersonDAO personDAO = DAOFactory.getInstance().getPersonDAO();
    ContactTypeDAO contactTypeDAO = DAOFactory.getInstance().getContactTypeDAO();
    address = address != null ? address.trim() : null;
    ContactType contactType = contactTypeDAO.findById(contactTypeId);
    if (contactType == null) {
        throw new InvalidScriptException("ContactType could not be found");
    }
    StaffMember staffMember = staffMemberDAO.findById(staffMemberId);
    if (staffMember == null) {
        throw new InvalidScriptException("StaffMember could not be found");
    }
    Person person = personDAO.findByUniqueEmail(address);
    if (person != null) {
        if (!staffMember.getPerson().getId().equals(person.getId())) {
            throw new InvalidScriptException("Email is already defined for another user");
        }
        StaffMember emailStaffMember = staffMemberDAO.findByUniqueEmail(address);
        if (emailStaffMember != null && emailStaffMember.getId().equals(staffMemberId)) {
            throw new InvalidScriptException("Email is already defined for this staff member");
        }
    }
    emailDAO.create(staffMember.getContactInfo(), contactType, defaultAddress, address);
}
Also used : PersonDAO(fi.otavanopisto.pyramus.dao.base.PersonDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) ContactType(fi.otavanopisto.pyramus.domainmodel.base.ContactType) InvalidScriptException(fi.otavanopisto.pyramus.util.dataimport.scripting.InvalidScriptException) ContactTypeDAO(fi.otavanopisto.pyramus.dao.base.ContactTypeDAO) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember) Person(fi.otavanopisto.pyramus.domainmodel.base.Person) EmailDAO(fi.otavanopisto.pyramus.dao.base.EmailDAO)

Aggregations

ContactTypeDAO (fi.otavanopisto.pyramus.dao.base.ContactTypeDAO)22 ContactType (fi.otavanopisto.pyramus.domainmodel.base.ContactType)21 EmailDAO (fi.otavanopisto.pyramus.dao.base.EmailDAO)11 PersonDAO (fi.otavanopisto.pyramus.dao.base.PersonDAO)10 StaffMemberDAO (fi.otavanopisto.pyramus.dao.users.StaffMemberDAO)10 School (fi.otavanopisto.pyramus.domainmodel.base.School)10 AddressDAO (fi.otavanopisto.pyramus.dao.base.AddressDAO)9 SchoolDAO (fi.otavanopisto.pyramus.dao.base.SchoolDAO)9 StudentDAO (fi.otavanopisto.pyramus.dao.students.StudentDAO)9 Tag (fi.otavanopisto.pyramus.domainmodel.base.Tag)9 Student (fi.otavanopisto.pyramus.domainmodel.students.Student)9 StaffMember (fi.otavanopisto.pyramus.domainmodel.users.StaffMember)9 PhoneNumberDAO (fi.otavanopisto.pyramus.dao.base.PhoneNumberDAO)8 Person (fi.otavanopisto.pyramus.domainmodel.base.Person)8 UserVariableDAO (fi.otavanopisto.pyramus.dao.users.UserVariableDAO)7 ContactURLTypeDAO (fi.otavanopisto.pyramus.dao.base.ContactURLTypeDAO)6 TagDAO (fi.otavanopisto.pyramus.dao.base.TagDAO)6 Address (fi.otavanopisto.pyramus.domainmodel.base.Address)6 ContactURLType (fi.otavanopisto.pyramus.domainmodel.base.ContactURLType)6 Language (fi.otavanopisto.pyramus.domainmodel.base.Language)6