Search in sources :

Example 1 with SchoolVariableKey

use of fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey in project pyramus by otavanopisto.

the class EditSchoolViewController method process.

/**
 * Processes the page request by including the corresponding JSP page to the response.
 *
 * @param pageRequestContext Page request context
 */
public void process(PageRequestContext pageRequestContext) {
    SchoolDAO schoolDAO = DAOFactory.getInstance().getSchoolDAO();
    SchoolFieldDAO schoolFieldDAO = DAOFactory.getInstance().getSchoolFieldDAO();
    SchoolVariableDAO schoolVariableDAO = DAOFactory.getInstance().getSchoolVariableDAO();
    SchoolVariableKeyDAO schoolVariableKeyDAO = DAOFactory.getInstance().getSchoolVariableKeyDAO();
    ContactTypeDAO contactTypeDAO = DAOFactory.getInstance().getContactTypeDAO();
    ContactURLTypeDAO contactURLTypeDAO = DAOFactory.getInstance().getContactURLTypeDAO();
    Long schoolId = NumberUtils.createLong(pageRequestContext.getRequest().getParameter("school"));
    School school = schoolDAO.findById(schoolId);
    StringBuilder tagsBuilder = new StringBuilder();
    Iterator<Tag> tagIterator = school.getTags().iterator();
    while (tagIterator.hasNext()) {
        Tag tag = tagIterator.next();
        tagsBuilder.append(tag.getText());
        if (tagIterator.hasNext())
            tagsBuilder.append(' ');
    }
    List<ContactURLType> contactURLTypes = contactURLTypeDAO.listUnarchived();
    Collections.sort(contactURLTypes, new StringAttributeComparator("getName"));
    List<SchoolVariableKey> schoolUserEditableVariableKeys = schoolVariableKeyDAO.listUserEditableVariableKeys();
    Collections.sort(schoolUserEditableVariableKeys, new StringAttributeComparator("getVariableName"));
    List<ContactType> contactTypes = contactTypeDAO.listUnarchived();
    Collections.sort(contactTypes, new StringAttributeComparator("getName"));
    String jsonContactTypes = new JSONArrayExtractor("id", "name").extractString(contactTypes);
    List<Address> addresses = school.getContactInfo().getAddresses();
    JSONArray jsonAddresses = new JSONArrayExtractor("id", "defaultAddress", "name", "streetAddress", "postalCode", "city", "country").extract(addresses);
    for (int i = 0; i < jsonAddresses.size(); i++) {
        JSONObject jsonAddress = jsonAddresses.getJSONObject(i);
        if (addresses.get(i).getContactType() != null) {
            jsonAddress.put("contactTypeId", addresses.get(i).getContactType().getId());
        }
    }
    List<Email> emails = school.getContactInfo().getEmails();
    JSONArray jsonEmails = new JSONArrayExtractor("id", "defaultAddress", "address").extract(emails);
    for (int i = 0; i < jsonEmails.size(); i++) {
        JSONObject jsonEmail = jsonEmails.getJSONObject(i);
        if (emails.get(i).getContactType() != null) {
            jsonEmail.put("contactTypeId", emails.get(i).getContactType().getId());
        }
    }
    List<PhoneNumber> phoneNumbers = school.getContactInfo().getPhoneNumbers();
    JSONArray jsonPhoneNumbers = new JSONArrayExtractor("id", "defaultNumber", "number").extract(phoneNumbers);
    for (int i = 0; i < jsonPhoneNumbers.size(); i++) {
        JSONObject jsonPhoneNumber = jsonPhoneNumbers.getJSONObject(i);
        if (phoneNumbers.get(i).getContactType() != null) {
            jsonPhoneNumber.put("contactTypeId", phoneNumbers.get(i).getContactType().getId());
        }
    }
    JSONArray jsonVariableKeys = new JSONArrayExtractor("variableKey", "variableName", "variableType").extract(schoolUserEditableVariableKeys);
    for (int i = 0; i < jsonVariableKeys.size(); i++) {
        JSONObject jsonVariableKey = jsonVariableKeys.getJSONObject(i);
        String key = jsonVariableKey.getString("variableKey");
        String value = schoolVariableDAO.findValueBySchoolAndKey(school, key);
        if (value != null)
            jsonVariableKey.put("variableValue", value);
    }
    this.setJsDataVariable(pageRequestContext, "addresses", jsonAddresses.toString());
    this.setJsDataVariable(pageRequestContext, "emails", jsonEmails.toString());
    this.setJsDataVariable(pageRequestContext, "phoneNumbers", jsonPhoneNumbers.toString());
    this.setJsDataVariable(pageRequestContext, "contactTypes", jsonContactTypes);
    this.setJsDataVariable(pageRequestContext, "variableKeys", jsonVariableKeys.toString());
    pageRequestContext.getRequest().setAttribute("tags", tagsBuilder.toString());
    pageRequestContext.getRequest().setAttribute("school", school);
    pageRequestContext.getRequest().setAttribute("variableKeys", schoolUserEditableVariableKeys);
    pageRequestContext.getRequest().setAttribute("schoolFields", schoolFieldDAO.listUnarchived());
    pageRequestContext.setIncludeJSP("/templates/settings/editschool.jsp");
}
Also used : ContactType(fi.otavanopisto.pyramus.domainmodel.base.ContactType) Email(fi.otavanopisto.pyramus.domainmodel.base.Email) Address(fi.otavanopisto.pyramus.domainmodel.base.Address) SchoolVariableKeyDAO(fi.otavanopisto.pyramus.dao.base.SchoolVariableKeyDAO) StringAttributeComparator(fi.otavanopisto.pyramus.util.StringAttributeComparator) JSONArrayExtractor(fi.otavanopisto.pyramus.util.JSONArrayExtractor) ContactURLTypeDAO(fi.otavanopisto.pyramus.dao.base.ContactURLTypeDAO) School(fi.otavanopisto.pyramus.domainmodel.base.School) SchoolDAO(fi.otavanopisto.pyramus.dao.base.SchoolDAO) ContactTypeDAO(fi.otavanopisto.pyramus.dao.base.ContactTypeDAO) SchoolVariableDAO(fi.otavanopisto.pyramus.dao.base.SchoolVariableDAO) JSONArray(net.sf.json.JSONArray) SchoolVariableKey(fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey) ContactURLType(fi.otavanopisto.pyramus.domainmodel.base.ContactURLType) JSONObject(net.sf.json.JSONObject) SchoolFieldDAO(fi.otavanopisto.pyramus.dao.base.SchoolFieldDAO) PhoneNumber(fi.otavanopisto.pyramus.domainmodel.base.PhoneNumber) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag)

Example 2 with SchoolVariableKey

use of fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey in project pyramus by otavanopisto.

the class SchoolRESTService method createVariable.

@Path("/variables")
@POST
@RESTPermit(SchoolPermissions.CREATE_SCHOOLVARIABLEKEY)
public Response createVariable(fi.otavanopisto.pyramus.rest.model.VariableKey entity) {
    if (entity == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (StringUtils.isBlank(entity.getKey()) || StringUtils.isBlank(entity.getName()) || entity.getType() == null || entity.getUserEditable() == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    VariableType variableType = null;
    switch(entity.getType()) {
        case BOOLEAN:
            variableType = VariableType.BOOLEAN;
            break;
        case DATE:
            variableType = VariableType.DATE;
            break;
        case NUMBER:
            variableType = VariableType.NUMBER;
            break;
        case TEXT:
            variableType = VariableType.TEXT;
            break;
    }
    SchoolVariableKey schoolVariableKey = schoolController.createSchoolVariableKey(entity.getKey(), entity.getName(), variableType, entity.getUserEditable());
    return Response.ok(objectFactory.createModel(schoolVariableKey)).build();
}
Also used : VariableType(fi.otavanopisto.pyramus.domainmodel.base.VariableType) SchoolVariableKey(fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) POST(javax.ws.rs.POST)

Example 3 with SchoolVariableKey

use of fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey in project pyramus by otavanopisto.

the class SchoolRESTService method deleteVariable.

@Path("/variables/{KEY}")
@DELETE
@RESTPermit(SchoolPermissions.DELETE_SCHOOLVARIABLEKEY)
public Response deleteVariable(@PathParam("KEY") String key) {
    SchoolVariableKey schoolVariableKey = schoolController.findSchoolVariableKeyByVariableKey(key);
    if (schoolVariableKey == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    schoolController.deleteSchoolVariableKey(schoolVariableKey);
    return Response.noContent().build();
}
Also used : SchoolVariableKey(fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit)

Example 4 with SchoolVariableKey

use of fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey in project pyramus by otavanopisto.

the class SchoolRESTService method createSchool.

@Path("/schools")
@POST
@RESTPermit(SchoolPermissions.CREATE_SCHOOL)
public Response createSchool(fi.otavanopisto.pyramus.rest.model.School entity) {
    if (entity == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (entity.getFieldId() == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    String code = entity.getCode();
    String name = entity.getName();
    if (StringUtils.isBlank(code) || StringUtils.isBlank(name)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    SchoolField schoolField = schoolController.findSchoolFieldById(entity.getFieldId());
    if (schoolField == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    BillingDetails billingDetails = null;
    School school = schoolController.createSchool(code, name, schoolField, billingDetails);
    if (entity.getTags() != null) {
        schoolController.updateSchoolTags(school, entity.getTags());
    }
    if (entity.getVariables() != null) {
        Set<String> variableKeys = entity.getVariables().keySet();
        for (String variableKey : variableKeys) {
            SchoolVariableKey schoolVariableKey = schoolController.findSchoolVariableKeyByVariableKey(variableKey);
            if (schoolVariableKey == null) {
                return Response.status(Status.BAD_REQUEST).build();
            }
            schoolController.createSchoolVariable(school, schoolVariableKey, entity.getVariables().get(variableKey));
        }
    }
    schoolController.updateSchoolAdditionalContactInfo(school, entity.getAdditionalContactInfo());
    return Response.ok(objectFactory.createModel(school)).build();
}
Also used : SchoolField(fi.otavanopisto.pyramus.domainmodel.base.SchoolField) School(fi.otavanopisto.pyramus.domainmodel.base.School) BillingDetails(fi.otavanopisto.pyramus.domainmodel.base.BillingDetails) SchoolVariableKey(fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) POST(javax.ws.rs.POST)

Example 5 with SchoolVariableKey

use of fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey in project pyramus by otavanopisto.

the class SchoolVariableDAO method findValueBySchoolAndKey.

public String findValueBySchoolAndKey(School school, String key) {
    SchoolVariableKeyDAO schoolVariableKeyDAO = DAOFactory.getInstance().getSchoolVariableKeyDAO();
    SchoolVariableKey schoolVariableKey = schoolVariableKeyDAO.findVariableKey(key);
    if (schoolVariableKey != null) {
        SchoolVariable schoolVariable = findBySchoolAndVariableKey(school, schoolVariableKey);
        return schoolVariable == null ? null : schoolVariable.getValue();
    } else {
        throw new PersistenceException("Unknown VariableKey");
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) SchoolVariable(fi.otavanopisto.pyramus.domainmodel.base.SchoolVariable) SchoolVariableKey(fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey)

Aggregations

SchoolVariableKey (fi.otavanopisto.pyramus.domainmodel.base.SchoolVariableKey)15 SchoolVariable (fi.otavanopisto.pyramus.domainmodel.base.SchoolVariable)6 School (fi.otavanopisto.pyramus.domainmodel.base.School)5 SchoolVariableKeyDAO (fi.otavanopisto.pyramus.dao.base.SchoolVariableKeyDAO)3 Address (fi.otavanopisto.pyramus.domainmodel.base.Address)3 ContactType (fi.otavanopisto.pyramus.domainmodel.base.ContactType)3 ContactURLType (fi.otavanopisto.pyramus.domainmodel.base.ContactURLType)3 Email (fi.otavanopisto.pyramus.domainmodel.base.Email)3 PhoneNumber (fi.otavanopisto.pyramus.domainmodel.base.PhoneNumber)3 Tag (fi.otavanopisto.pyramus.domainmodel.base.Tag)3 RESTPermit (fi.otavanopisto.pyramus.rest.annotation.RESTPermit)3 JSONArrayExtractor (fi.otavanopisto.pyramus.util.JSONArrayExtractor)3 StringAttributeComparator (fi.otavanopisto.pyramus.util.StringAttributeComparator)3 Path (javax.ws.rs.Path)3 ContactTypeDAO (fi.otavanopisto.pyramus.dao.base.ContactTypeDAO)2 ContactURLTypeDAO (fi.otavanopisto.pyramus.dao.base.ContactURLTypeDAO)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 BillingDetails (fi.otavanopisto.pyramus.domainmodel.base.BillingDetails)2