Search in sources :

Example 1 with WorkResource

use of fi.otavanopisto.pyramus.domainmodel.resources.WorkResource 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 WorkResource

use of fi.otavanopisto.pyramus.domainmodel.resources.WorkResource in project pyramus by otavanopisto.

the class WorkResourceDAO method create.

public WorkResource create(String name, ResourceCategory category, Double costPerUse, Double hourlyCost) {
    EntityManager entityManager = getEntityManager();
    WorkResource workResource = new WorkResource();
    workResource.setName(name);
    if (costPerUse != null) {
        MonetaryAmount monetaryAmount = new MonetaryAmount();
        monetaryAmount.setAmount(costPerUse);
        workResource.setCostPerUse(monetaryAmount);
    }
    if (hourlyCost != null) {
        MonetaryAmount monetaryAmount = new MonetaryAmount();
        monetaryAmount.setAmount(hourlyCost);
        workResource.setHourlyCost(monetaryAmount);
    }
    workResource.setCategory(category);
    entityManager.persist(workResource);
    return workResource;
}
Also used : EntityManager(javax.persistence.EntityManager) WorkResource(fi.otavanopisto.pyramus.domainmodel.resources.WorkResource) MonetaryAmount(fi.otavanopisto.pyramus.persistence.usertypes.MonetaryAmount)

Example 3 with WorkResource

use of fi.otavanopisto.pyramus.domainmodel.resources.WorkResource 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)

Example 4 with WorkResource

use of fi.otavanopisto.pyramus.domainmodel.resources.WorkResource in project pyramus by otavanopisto.

the class SearchResourcesJSONRequestController method process.

public void process(JSONRequestContext requestContext) {
    ResourceDAO resourceDAO = DAOFactory.getInstance().getResourceDAO();
    ResourceCategoryDAO resourceCategoryDAO = DAOFactory.getInstance().getResourceCategoryDAO();
    Integer resultsPerPage = NumberUtils.createInteger(requestContext.getRequest().getParameter("maxResults"));
    if (resultsPerPage == null) {
        resultsPerPage = 10;
    }
    Integer page = NumberUtils.createInteger(requestContext.getRequest().getParameter("page"));
    if (page == null) {
        page = 0;
    }
    SearchResult<Resource> searchResult;
    if ("advanced".equals(requestContext.getString("activeTab"))) {
        String name = requestContext.getRequest().getParameter("name");
        String tags = requestContext.getRequest().getParameter("tags");
        ResourceType resourceType = null;
        String resourceTypeParam = requestContext.getRequest().getParameter("resourceType");
        if (!StringUtils.isBlank(resourceTypeParam)) {
            resourceType = Enum.valueOf(ResourceType.class, resourceTypeParam);
        }
        ResourceCategory resourceCategory = null;
        String resourceCategoryParam = requestContext.getRequest().getParameter("resourceCategory");
        if (!StringUtils.isBlank(resourceCategoryParam)) {
            resourceCategory = resourceCategoryDAO.findById(NumberUtils.createLong(resourceCategoryParam));
        }
        searchResult = resourceDAO.searchResources(resultsPerPage, page, name, tags, resourceType, resourceCategory, true);
    } else {
        String simpleQuery = requestContext.getString("simpleQuery");
        searchResult = resourceDAO.searchResourcesBasic(resultsPerPage, page, simpleQuery);
    }
    List<Map<String, Object>> results = new ArrayList<>();
    List<Resource> resources = searchResult.getResults();
    for (Resource resource : resources) {
        Double unitCost = 0.0;
        if (resource instanceof MaterialResource) {
            unitCost = ((MaterialResource) resource).getUnitCost().getAmount();
        } else if (resource instanceof WorkResource) {
            unitCost = ((WorkResource) resource).getCostPerUse().getAmount();
        }
        Double hourlyCost = 0.0;
        if (resource instanceof WorkResource) {
            hourlyCost = ((WorkResource) resource).getHourlyCost().getAmount();
        }
        Map<String, Object> resourceInfo = new HashMap<>();
        resourceInfo.put("id", resource.getId());
        resourceInfo.put("name", resource.getName());
        resourceInfo.put("resourceType", resource.getResourceType());
        resourceInfo.put("resourceCategoryId", resource.getCategory().getId());
        resourceInfo.put("resourceCategoryName", resource.getCategory().getName());
        resourceInfo.put("unitCost", unitCost);
        resourceInfo.put("hourlyCost", hourlyCost);
        results.add(resourceInfo);
    }
    String statusMessage;
    Locale locale = requestContext.getRequest().getLocale();
    if (searchResult.getTotalHitCount() > 0) {
        statusMessage = Messages.getInstance().getText(locale, "resources.searchResources.searchStatus", new Object[] { searchResult.getFirstResult() + 1, searchResult.getLastResult() + 1, searchResult.getTotalHitCount() });
    } else {
        statusMessage = Messages.getInstance().getText(locale, "resources.searchResources.searchStatusNoMatches");
    }
    requestContext.addResponseParameter("results", results);
    requestContext.addResponseParameter("statusMessage", statusMessage);
    requestContext.addResponseParameter("pages", searchResult.getPages());
    requestContext.addResponseParameter("page", searchResult.getPage());
}
Also used : MaterialResource(fi.otavanopisto.pyramus.domainmodel.resources.MaterialResource) Locale(java.util.Locale) WorkResource(fi.otavanopisto.pyramus.domainmodel.resources.WorkResource) HashMap(java.util.HashMap) ResourceCategoryDAO(fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO) Resource(fi.otavanopisto.pyramus.domainmodel.resources.Resource) MaterialResource(fi.otavanopisto.pyramus.domainmodel.resources.MaterialResource) WorkResource(fi.otavanopisto.pyramus.domainmodel.resources.WorkResource) ArrayList(java.util.ArrayList) ResourceType(fi.otavanopisto.pyramus.domainmodel.resources.ResourceType) ResourceCategory(fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory) ResourceDAO(fi.otavanopisto.pyramus.dao.resources.ResourceDAO) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

WorkResource (fi.otavanopisto.pyramus.domainmodel.resources.WorkResource)4 ResourceCategoryDAO (fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO)3 ResourceDAO (fi.otavanopisto.pyramus.dao.resources.ResourceDAO)3 ResourceCategory (fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory)3 TagDAO (fi.otavanopisto.pyramus.dao.base.TagDAO)2 WorkResourceDAO (fi.otavanopisto.pyramus.dao.resources.WorkResourceDAO)2 Tag (fi.otavanopisto.pyramus.domainmodel.base.Tag)2 HashSet (java.util.HashSet)2 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)1 MaterialResource (fi.otavanopisto.pyramus.domainmodel.resources.MaterialResource)1 Resource (fi.otavanopisto.pyramus.domainmodel.resources.Resource)1 ResourceType (fi.otavanopisto.pyramus.domainmodel.resources.ResourceType)1 MonetaryAmount (fi.otavanopisto.pyramus.persistence.usertypes.MonetaryAmount)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1 EntityManager (javax.persistence.EntityManager)1