Search in sources :

Example 1 with WorkResourceDAO

use of fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO in project pyramus by otavanopisto.

the class CreateWorkResourceJSONRequestController method process.

public void process(JSONRequestContext jsonRequestContext) {
    ResourceDAO resourceDAO = DAOFactory.getInstance().getResourceDAO();
    ResourceCategoryDAO resourceCategoryDAO = DAOFactory.getInstance().getResourceCategoryDAO();
    WorkResourceDAO workResourceDAO = DAOFactory.getInstance().getWorkResourceDAO();
    TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
    String name = jsonRequestContext.getRequest().getParameter("name");
    Double hourlyCost = NumberUtils.createDouble(jsonRequestContext.getRequest().getParameter("hourlyCost"));
    Double costPerUse = NumberUtils.createDouble(jsonRequestContext.getRequest().getParameter("costPerUse"));
    ResourceCategory resourceCategory = resourceCategoryDAO.findById(NumberUtils.createLong(jsonRequestContext.getRequest().getParameter("category")));
    String tagsText = jsonRequestContext.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);
            }
        }
    }
    WorkResource workResource = workResourceDAO.create(name, resourceCategory, costPerUse, hourlyCost);
    resourceDAO.setResourceTags(workResource, tagEntities);
    String redirectURL = jsonRequestContext.getRequest().getContextPath() + "/resources/editworkresource.page?resource=" + workResource.getId();
    String refererAnchor = jsonRequestContext.getRefererAnchor();
    if (!StringUtils.isBlank(refererAnchor))
        redirectURL += "#" + refererAnchor;
    jsonRequestContext.setRedirectURL(redirectURL);
}
Also used : WorkResourceDAO(fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO) WorkResource(fi.otavanopisto.pyramus.domainmodel.resources.WorkResource) TagDAO(fi.otavanopisto.pyramus.dao.base.TagDAO) ResourceCategoryDAO(fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO) ResourceDAO(fi.otavanopisto.pyramus.dao.resources.ResourceDAO) WorkResourceDAO(fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO) ResourceCategory(fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag) HashSet(java.util.HashSet)

Example 2 with WorkResourceDAO

use of fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO in project pyramus by otavanopisto.

the class EditWorkResourceViewController method process.

public void process(PageRequestContext pageRequestContext) {
    ResourceDAO resourceDAO = DAOFactory.getInstance().getResourceDAO();
    WorkResourceDAO workResourceDAO = DAOFactory.getInstance().getWorkResourceDAO();
    ResourceCategoryDAO resourceCategoryDAO = DAOFactory.getInstance().getResourceCategoryDAO();
    Long resourceId = NumberUtils.createLong(pageRequestContext.getRequest().getParameter("resource"));
    Resource resource = resourceDAO.findById(resourceId);
    StringBuilder tagsBuilder = new StringBuilder();
    Iterator<Tag> tagIterator = resource.getTags().iterator();
    while (tagIterator.hasNext()) {
        Tag tag = tagIterator.next();
        tagsBuilder.append(tag.getText());
        if (tagIterator.hasNext())
            tagsBuilder.append(' ');
    }
    List<ResourceCategory> resourceCategories = resourceCategoryDAO.listUnarchived();
    Collections.sort(resourceCategories, new StringAttributeComparator("getName"));
    pageRequestContext.getRequest().setAttribute("tags", tagsBuilder.toString());
    pageRequestContext.getRequest().setAttribute("categories", resourceCategories);
    pageRequestContext.getRequest().setAttribute("resource", workResourceDAO.findById(resource.getId()));
    pageRequestContext.setIncludeJSP("/templates/resources/editworkresource.jsp");
}
Also used : WorkResourceDAO(fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO) ResourceCategoryDAO(fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO) Resource(fi.otavanopisto.pyramus.domainmodel.resources.Resource) StringAttributeComparator(fi.otavanopisto.pyramus.util.StringAttributeComparator) ResourceDAO(fi.otavanopisto.pyramus.dao.resources.ResourceDAO) WorkResourceDAO(fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag) ResourceCategory(fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory)

Example 3 with WorkResourceDAO

use of fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO in project pyramus by otavanopisto.

the class EditWorkResourceJSONRequestController method process.

public void process(JSONRequestContext jsonRequestContext) {
    ResourceDAO resourceDAO = DAOFactory.getInstance().getResourceDAO();
    ResourceCategoryDAO resourceCategoryDAO = DAOFactory.getInstance().getResourceCategoryDAO();
    WorkResourceDAO workResourceDAO = DAOFactory.getInstance().getWorkResourceDAO();
    TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
    String name = jsonRequestContext.getRequest().getParameter("name");
    Long resourceId = NumberUtils.createLong(jsonRequestContext.getRequest().getParameter("resource"));
    Double hourlyCost = NumberUtils.createDouble(jsonRequestContext.getRequest().getParameter("hourlyCost"));
    Double costPerUse = NumberUtils.createDouble(jsonRequestContext.getRequest().getParameter("costPerUse"));
    Long version = NumberUtils.createLong(jsonRequestContext.getRequest().getParameter("version"));
    String tagsText = jsonRequestContext.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);
            }
        }
    }
    WorkResource workResource = workResourceDAO.findById(resourceId);
    if (!version.equals(workResource.getVersion()))
        throw new SmvcRuntimeException(PyramusStatusCode.CONCURRENT_MODIFICATION, Messages.getInstance().getText(jsonRequestContext.getRequest().getLocale(), "generic.errors.concurrentModification"));
    ResourceCategory resourceCategory = resourceCategoryDAO.findById(NumberUtils.createLong(jsonRequestContext.getRequest().getParameter("category")));
    workResourceDAO.update(workResource, name, resourceCategory, costPerUse, hourlyCost);
    resourceDAO.setResourceTags(workResource, tagEntities);
    jsonRequestContext.setRedirectURL(jsonRequestContext.getReferer(true));
}
Also used : WorkResourceDAO(fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO) WorkResource(fi.otavanopisto.pyramus.domainmodel.resources.WorkResource) TagDAO(fi.otavanopisto.pyramus.dao.base.TagDAO) ResourceCategoryDAO(fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) ResourceCategory(fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory) ResourceDAO(fi.otavanopisto.pyramus.dao.resources.ResourceDAO) WorkResourceDAO(fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag) HashSet(java.util.HashSet)

Aggregations

ResourceCategoryDAO (fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO)3 ResourceDAO (fi.otavanopisto.pyramus.dao.resources.ResourceDAO)3 WorkResourceDAO (fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO)3 Tag (fi.otavanopisto.pyramus.domainmodel.base.Tag)3 ResourceCategory (fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory)3 TagDAO (fi.otavanopisto.pyramus.dao.base.TagDAO)2 WorkResource (fi.otavanopisto.pyramus.domainmodel.resources.WorkResource)2 HashSet (java.util.HashSet)2 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)1 Resource (fi.otavanopisto.pyramus.domainmodel.resources.Resource)1 StringAttributeComparator (fi.otavanopisto.pyramus.util.StringAttributeComparator)1