edu.byu.softwareDistribution.web.controller.shopper.ShoppingProductController.java Source code

Java tutorial

Introduction

Here is the source code for edu.byu.softwareDistribution.web.controller.shopper.ShoppingProductController.java

Source

package edu.byu.softwareDistribution.web.controller.shopper;

import edu.byu.security.userdetails.IdentityDetails;
import edu.byu.softwareDist.dao.*;
import edu.byu.softwareDist.domain.*;
import edu.byu.softwareDist.manager.LicenseManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.beans.PropertyEditorSupport;
import java.util.List;

@Controller
public class ShoppingProductController {
    private static final Logger LOG = Logger.getLogger(ShoppingController.class);

    @Autowired
    private ProductDao productDao;
    @Autowired
    private FileSetPlatformDao fileSetPlatformDao;
    @Autowired
    private FileSetDao fileSetDao;
    @Autowired
    private SoftwareContractDao softwareContractDao;
    @Autowired
    private PeriodDao periodDao;
    @Autowired
    private LicenseManager licenseManager;

    @RequestMapping(value = "/shop/product/{productId}")
    public String displayPage(Model model, @PathVariable("productId") Integer productId) {
        LOG.debug("productId: " + productId);
        Product product = productDao.findById(productId);
        if (product == null) {
            return "shop/product";
        }
        Period period = new Period();
        SoftwareContract sc = new SoftwareContract();
        if (product.getMaintenance() != null) {
            period = periodDao.findByMaintenanceId(product.getMaintenance().getMaintenanceId()); //NOT UNIQUE QUERY PURCHASE UNTIL DATE CAN HAVE MULTI GREATER
            if (period != null) {
                sc = softwareContractDao.findByMaintenanceIdAndPeriodId(product.getMaintenance().getMaintenanceId(),
                        period.getPeriodId());
            }
        }
        model.addAttribute("product", product);
        model.addAttribute("productReturnable", licenseManager.isReturnable(product.getProductId()));
        List<FileSetPlatform> setPlatforms = fileSetPlatformDao.findByProductId(productId);
        final List<FileSet> fileSets = fileSetDao.findAllByProduct(product);
        model.addAttribute("fileSets", fileSets);
        model.addAttribute("platforms", setPlatforms);
        model.addAttribute("period", period);
        model.addAttribute("signedInName", IdentityDetails.getCurrentIdentityDetails().getName());
        model.addAttribute("sc", sc);
        return "shop/product";
    }

    @InitBinder
    public void addProductEditor(ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Product.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                setValue(productDao.findById(Integer.parseInt(text)));
            }
        });
    }
}