Search in sources :

Example 1 with BillingDetailsDAO

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

the class CreateSchoolJSONRequestController 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();
    String schoolCode = requestContext.getString("code");
    String schoolName = requestContext.getString("name");
    String tagsText = requestContext.getString("tags");
    Long schoolFieldId = requestContext.getLong("schoolFieldId");
    SchoolField schoolField = null;
    if (schoolFieldId != null && schoolFieldId.intValue() >= 0)
        schoolField = schoolFieldDAO.findById(schoolFieldId);
    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);
            }
        }
    }
    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");
    BillingDetails billingDetails = billingDetailsDAO.create(billingPersonName, billingCompanyName, billingStreetAddress1, billingStreetAddress2, billingPostalCode, billingCity, billingRegion, billingCountry, billingPhoneNumber, billingEmailAddress, billingElectronicBillingAddress, billingElectronicBillingOperator, billingCompanyIdentifier, billingReferenceNumber, billingNotes);
    School school = schoolDAO.create(schoolCode, schoolName, schoolField, billingDetails);
    // Tags
    schoolDAO.updateTags(school, 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(school.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 = requestContext.getString(colPrefix + ".email");
        // Trim the email address
        email = email != null ? email.trim() : null;
        if (email != null) {
            emailDAO.create(school.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(school.getContactInfo(), contactType, defaultNumber, number);
        }
    }
    // 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);
        }
    }
    String redirectURL = requestContext.getRequest().getContextPath() + "/settings/editschool.page?school=" + school.getId();
    String refererAnchor = requestContext.getRefererAnchor();
    if (!StringUtils.isBlank(refererAnchor))
        redirectURL += "#" + refererAnchor;
    requestContext.setRedirectURL(redirectURL);
}
Also used : SchoolField(fi.otavanopisto.pyramus.domainmodel.base.SchoolField) PhoneNumberDAO(fi.otavanopisto.pyramus.dao.base.PhoneNumberDAO) ContactType(fi.otavanopisto.pyramus.domainmodel.base.ContactType) TagDAO(fi.otavanopisto.pyramus.dao.base.TagDAO) BillingDetails(fi.otavanopisto.pyramus.domainmodel.base.BillingDetails) EmailDAO(fi.otavanopisto.pyramus.dao.base.EmailDAO) School(fi.otavanopisto.pyramus.domainmodel.base.School) SchoolFieldDAO(fi.otavanopisto.pyramus.dao.base.SchoolFieldDAO) BillingDetailsDAO(fi.otavanopisto.pyramus.dao.base.BillingDetailsDAO) SchoolDAO(fi.otavanopisto.pyramus.dao.base.SchoolDAO) ContactTypeDAO(fi.otavanopisto.pyramus.dao.base.ContactTypeDAO) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag) SchoolVariableDAO(fi.otavanopisto.pyramus.dao.base.SchoolVariableDAO) AddressDAO(fi.otavanopisto.pyramus.dao.base.AddressDAO) HashSet(java.util.HashSet)

Example 2 with BillingDetailsDAO

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

the class CourseStudentDetailsDialogViewController method getExistingBillingDetails.

private List<BillingDetails> getExistingBillingDetails(CourseStudent courseStudent) {
    Map<Integer, BillingDetails> result = new HashMap<>();
    BillingDetailsDAO billingDetailsDAO = DAOFactory.getInstance().getBillingDetailsDAO();
    List<BillingDetails> billingDetails = billingDetailsDAO.listByStudent(courseStudent.getStudent());
    for (BillingDetails studentBillingDetails : billingDetails) {
        result.put(getBillingDetailsHash(studentBillingDetails), studentBillingDetails);
    }
    if (courseStudent.getBillingDetails() != null) {
        result.remove(getBillingDetailsHash(courseStudent.getBillingDetails()));
    }
    return new ArrayList<>(result.values());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BillingDetailsDAO(fi.otavanopisto.pyramus.dao.base.BillingDetailsDAO) BillingDetails(fi.otavanopisto.pyramus.domainmodel.base.BillingDetails)

Example 3 with BillingDetailsDAO

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

the class SaveStudentDetailsJSONRequestController method process.

public void process(JSONRequestContext requestContext) {
    CourseStudentDAO courseStudentDAO = DAOFactory.getInstance().getCourseStudentDAO();
    RoomDAO roomDAO = DAOFactory.getInstance().getRoomDAO();
    BillingDetailsDAO billingDetailsDAO = DAOFactory.getInstance().getBillingDetailsDAO();
    Long courseStudentId = requestContext.getLong("courseStudentId");
    String organization = requestContext.getString("organization");
    String additionalInfo = requestContext.getString("additionalInfo");
    Long roomId = requestContext.getLong("roomId");
    String roomAdditionalInfo = requestContext.getString("roomAdditionalInfo");
    BigDecimal lodgingFee = requestContext.getBigDecimal("lodgingFee");
    Currency lodgingFeeCurrency = requestContext.getCurrency("lodgingFeeCurrency");
    BigDecimal reservationFee = requestContext.getBigDecimal("reservationFee");
    Currency reservationFeeCurrency = requestContext.getCurrency("reservationFeeCurrency");
    String billingDetailsPersonName = requestContext.getString("billingDetailsPersonName");
    String billingDetailsCompanyName = requestContext.getString("billingDetailsCompanyName");
    String billingDetailsStreetAddress1 = requestContext.getString("billingDetailsStreetAddress1");
    String billingDetailsStreetAddress2 = requestContext.getString("billingDetailsStreetAddress2");
    String billingDetailsPostalCode = requestContext.getString("billingDetailsPostalCode");
    String billingDetailsCity = requestContext.getString("billingDetailsCity");
    String billingDetailsRegion = requestContext.getString("billingDetailsRegion");
    String billingDetailsCountry = requestContext.getString("billingDetailsCountry");
    String billingDetailsPhoneNumber = requestContext.getString("billingDetailsPhoneNumber");
    String billingDetailsEmailAddress = requestContext.getString("billingDetailsEmailAddress");
    String billingDetailsCompanyIdentifier = requestContext.getString("billingDetailsCompanyIdentifier");
    String billingDetailsReferenceNumber = requestContext.getString("billingDetailsReferenceNumber");
    String billingDetailsElectronicBillingAddress = requestContext.getString("billingDetailsElectronicBillingAddress");
    String billingDetailsElectronicBillingOperator = requestContext.getString("billingDetailsElectronicBillingOperator");
    String billingDetailsNotes = requestContext.getString("billingDetailsNotes");
    Long billingDetailsId = requestContext.getLong("billingDetailsId");
    if (billingDetailsId != null) {
        BillingDetails billingDetails = billingDetailsDAO.findById(billingDetailsId);
        if (billingDetails != null) {
            billingDetailsPersonName = billingDetails.getPersonName();
            billingDetailsCompanyName = billingDetails.getCompanyName();
            billingDetailsStreetAddress1 = billingDetails.getStreetAddress1();
            billingDetailsStreetAddress2 = billingDetails.getStreetAddress2();
            billingDetailsPostalCode = billingDetails.getPostalCode();
            billingDetailsCity = billingDetails.getCity();
            billingDetailsRegion = billingDetails.getRegion();
            billingDetailsCountry = billingDetails.getCountry();
            billingDetailsPhoneNumber = billingDetails.getPhoneNumber();
            billingDetailsEmailAddress = billingDetails.getEmailAddress();
            billingDetailsCompanyIdentifier = billingDetails.getCompanyIdentifier();
            billingDetailsReferenceNumber = billingDetails.getReferenceNumber();
            billingDetailsElectronicBillingAddress = billingDetails.getElectronicBillingAddress();
            billingDetailsElectronicBillingOperator = billingDetails.getElectronicBillingOperator();
            billingDetailsNotes = billingDetails.getNotes();
        }
    }
    boolean billingDetailsSet = !allNull(billingDetailsPersonName, billingDetailsCompanyName, billingDetailsStreetAddress1, billingDetailsStreetAddress2, billingDetailsPostalCode, billingDetailsCity, billingDetailsRegion, billingDetailsCountry, billingDetailsPhoneNumber, billingDetailsEmailAddress, billingDetailsCompanyIdentifier, billingDetailsReferenceNumber, billingDetailsElectronicBillingAddress, billingDetailsElectronicBillingOperator, billingDetailsNotes);
    if (courseStudentId == null) {
        throw new SmvcRuntimeException(PyramusStatusCode.UNDEFINED, "Missing courseStudentId parameter");
    }
    CourseStudent courseStudent = courseStudentDAO.findById(courseStudentId);
    if (courseStudent == null) {
        throw new SmvcRuntimeException(PyramusStatusCode.UNDEFINED, "Invalid courseStudentId parameter");
    }
    Room room = null;
    if (roomId != null) {
        room = roomDAO.findById(roomId);
        if (room == null) {
            throw new SmvcRuntimeException(PyramusStatusCode.UNDEFINED, "Invalid roomId");
        }
    }
    BillingDetails billingDetails = courseStudent.getBillingDetails();
    if (billingDetailsSet) {
        if (billingDetails != null) {
            billingDetailsDAO.updatePersonName(billingDetails, billingDetailsPersonName);
            billingDetailsDAO.updateCompanyName(billingDetails, billingDetailsCompanyName);
            billingDetailsDAO.updateStreetAddress1(billingDetails, billingDetailsStreetAddress1);
            billingDetailsDAO.updateStreetAddress2(billingDetails, billingDetailsStreetAddress2);
            billingDetailsDAO.updatePostalCode(billingDetails, billingDetailsPostalCode);
            billingDetailsDAO.updateCity(billingDetails, billingDetailsCity);
            billingDetailsDAO.updateRegion(billingDetails, billingDetailsRegion);
            billingDetailsDAO.updateCountry(billingDetails, billingDetailsCountry);
            billingDetailsDAO.updatePhoneNumber(billingDetails, billingDetailsPhoneNumber);
            billingDetailsDAO.updateEmailAddress(billingDetails, billingDetailsEmailAddress);
            billingDetailsDAO.updateCompanyIdentifier(billingDetails, billingDetailsCompanyIdentifier);
            billingDetailsDAO.updateReferenceNumber(billingDetails, billingDetailsReferenceNumber);
            billingDetailsDAO.updateNotes(billingDetails, billingDetailsNotes);
            billingDetailsDAO.updateElectronicBillingAddress(billingDetails, billingDetailsElectronicBillingAddress);
            billingDetailsDAO.updateElectronicBillingOperator(billingDetails, billingDetailsElectronicBillingOperator);
        } else {
            courseStudentDAO.updateBillingDetails(courseStudent, billingDetailsDAO.create(billingDetailsPersonName, billingDetailsCompanyName, billingDetailsStreetAddress1, billingDetailsStreetAddress2, billingDetailsPostalCode, billingDetailsCity, billingDetailsRegion, billingDetailsCountry, billingDetailsPhoneNumber, billingDetailsEmailAddress, billingDetailsElectronicBillingAddress, billingDetailsElectronicBillingOperator, billingDetailsCompanyIdentifier, billingDetailsReferenceNumber, billingDetailsNotes));
        }
    } else {
        if (billingDetails != null) {
            courseStudentDAO.updateBillingDetails(courseStudent, null);
            billingDetailsDAO.delete(billingDetails);
        }
    }
    courseStudentDAO.updateOrganization(courseStudent, organization);
    courseStudentDAO.updateAdditionalInfo(courseStudent, additionalInfo);
    courseStudentDAO.updateRoom(courseStudent, room);
    courseStudentDAO.updateRoomAdditionalInfo(courseStudent, roomAdditionalInfo);
    courseStudentDAO.updateLodgingFee(courseStudent, lodgingFee, lodgingFeeCurrency);
    courseStudentDAO.updateReservationFee(courseStudent, reservationFee, reservationFeeCurrency);
}
Also used : RoomDAO(fi.otavanopisto.pyramus.dao.accommodation.RoomDAO) CourseStudentDAO(fi.otavanopisto.pyramus.dao.courses.CourseStudentDAO) Currency(java.util.Currency) CourseStudent(fi.otavanopisto.pyramus.domainmodel.courses.CourseStudent) BillingDetailsDAO(fi.otavanopisto.pyramus.dao.base.BillingDetailsDAO) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) BillingDetails(fi.otavanopisto.pyramus.domainmodel.base.BillingDetails) Room(fi.otavanopisto.pyramus.domainmodel.accommodation.Room) BigDecimal(java.math.BigDecimal)

Example 4 with BillingDetailsDAO

use of fi.otavanopisto.pyramus.dao.base.BillingDetailsDAO 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)

Aggregations

BillingDetailsDAO (fi.otavanopisto.pyramus.dao.base.BillingDetailsDAO)4 BillingDetails (fi.otavanopisto.pyramus.domainmodel.base.BillingDetails)4 AddressDAO (fi.otavanopisto.pyramus.dao.base.AddressDAO)2 ContactTypeDAO (fi.otavanopisto.pyramus.dao.base.ContactTypeDAO)2 EmailDAO (fi.otavanopisto.pyramus.dao.base.EmailDAO)2 PhoneNumberDAO (fi.otavanopisto.pyramus.dao.base.PhoneNumberDAO)2 SchoolDAO (fi.otavanopisto.pyramus.dao.base.SchoolDAO)2 SchoolFieldDAO (fi.otavanopisto.pyramus.dao.base.SchoolFieldDAO)2 SchoolVariableDAO (fi.otavanopisto.pyramus.dao.base.SchoolVariableDAO)2 TagDAO (fi.otavanopisto.pyramus.dao.base.TagDAO)2 ContactType (fi.otavanopisto.pyramus.domainmodel.base.ContactType)2 School (fi.otavanopisto.pyramus.domainmodel.base.School)2 SchoolField (fi.otavanopisto.pyramus.domainmodel.base.SchoolField)2 Tag (fi.otavanopisto.pyramus.domainmodel.base.Tag)2 HashSet (java.util.HashSet)2 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)1 RoomDAO (fi.otavanopisto.pyramus.dao.accommodation.RoomDAO)1 CourseStudentDAO (fi.otavanopisto.pyramus.dao.courses.CourseStudentDAO)1 Room (fi.otavanopisto.pyramus.domainmodel.accommodation.Room)1 Address (fi.otavanopisto.pyramus.domainmodel.base.Address)1