use of fi.otavanopisto.pyramus.domainmodel.resources.MaterialResource in project pyramus by otavanopisto.
the class CreateMaterialResourceJSONRequestController method process.
public void process(JSONRequestContext jsonRequestContext) {
ResourceDAO resourceDAO = DAOFactory.getInstance().getResourceDAO();
ResourceCategoryDAO resourceCategoryDAO = DAOFactory.getInstance().getResourceCategoryDAO();
MaterialResourceDAO materialResourceDAO = DAOFactory.getInstance().getMaterialResourceDAO();
TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
String name = jsonRequestContext.getRequest().getParameter("name");
Double unitCost = NumberUtils.createDouble(jsonRequestContext.getRequest().getParameter("unitCost"));
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);
}
}
}
MaterialResource materialResource = materialResourceDAO.create(name, resourceCategory, unitCost);
resourceDAO.setResourceTags(materialResource, tagEntities);
String redirectURL = jsonRequestContext.getRequest().getContextPath() + "/resources/editmaterialresource.page?resource=" + materialResource.getId();
String refererAnchor = jsonRequestContext.getRefererAnchor();
if (!StringUtils.isBlank(refererAnchor))
redirectURL += "#" + refererAnchor;
jsonRequestContext.setRedirectURL(redirectURL);
}
use of fi.otavanopisto.pyramus.domainmodel.resources.MaterialResource in project pyramus by otavanopisto.
the class MaterialResourceDAO method create.
public MaterialResource create(String name, ResourceCategory category, Double unitCost) {
EntityManager entityManager = getEntityManager();
MaterialResource materialResource = new MaterialResource();
materialResource.setName(name);
if (unitCost != null) {
MonetaryAmount monetaryAmount = new MonetaryAmount();
monetaryAmount.setAmount(unitCost);
materialResource.setUnitCost(monetaryAmount);
}
materialResource.setCategory(category);
entityManager.persist(materialResource);
return materialResource;
}
use of fi.otavanopisto.pyramus.domainmodel.resources.MaterialResource in project pyramus by otavanopisto.
the class EditMaterialResourceJSONRequestController method process.
public void process(JSONRequestContext jsonRequestContext) {
ResourceDAO resourceDAO = DAOFactory.getInstance().getResourceDAO();
ResourceCategoryDAO resourceCategoryDAO = DAOFactory.getInstance().getResourceCategoryDAO();
MaterialResourceDAO materialResourceDAO = DAOFactory.getInstance().getMaterialResourceDAO();
TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
String name = jsonRequestContext.getRequest().getParameter("name");
Long resourceId = NumberUtils.createLong(jsonRequestContext.getRequest().getParameter("resource"));
Double unitCost = NumberUtils.createDouble(jsonRequestContext.getRequest().getParameter("unitCost"));
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);
}
}
}
MaterialResource materialResource = materialResourceDAO.findById(resourceId);
if (!version.equals(materialResource.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")));
materialResourceDAO.update(materialResource, name, resourceCategory, unitCost);
resourceDAO.setResourceTags(materialResource, tagEntities);
jsonRequestContext.setRedirectURL(jsonRequestContext.getReferer(true));
}
use of fi.otavanopisto.pyramus.domainmodel.resources.MaterialResource 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());
}
Aggregations