com.rr.wabshs.ui.hierarchy.hierarchyController.java Source code

Java tutorial

Introduction

Here is the source code for com.rr.wabshs.ui.hierarchy.hierarchyController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rr.wabshs.ui.hierarchy;

import com.registryKit.hierarchy.hierarchyManager;
import com.registryKit.hierarchy.programHierarchyDetails;
import com.registryKit.hierarchy.programOrgHierarchy;
import com.registryKit.user.User;
import com.rr.wabshs.ui.security.encryptObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author chadmccue
 */
@Controller
@RequestMapping("/hierarchy")
public class hierarchyController {

    @Autowired
    private hierarchyManager hierarchymanager;

    @Value("${programId}")
    private Integer programId;

    @Value("${topSecret}")
    private String topSecret;

    /**
     * The 'getSecondTierList.do' GET request will return the list of tier 2 available to the user.
     * @param session
     * @param surveyId
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "getSecondTierList.do", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView getSecondTierList(HttpSession session,
            @RequestParam(value = "surveyId", required = false) Integer surveyId) throws Exception {

        ModelAndView mav = new ModelAndView();
        mav.setViewName("/survey/tier2Select");

        User userDetails = (User) session.getAttribute("userDetails");

        programOrgHierarchy topLevel = hierarchymanager.getProgramOrgHierarchyBydspPos(1, programId);

        /* Get a list of top level entities */
        Integer userId = 0;
        if (userDetails.getRoleId() == 3) {
            userId = userDetails.getId();
        }
        List<programHierarchyDetails> topLevelEntities = hierarchymanager.getProgramHierarchyItems(topLevel.getId(),
                userId);

        List<firstTierEntities> tier1EntityList = new ArrayList<firstTierEntities>();

        encryptObject encrypt = new encryptObject();
        Map<String, String> map;

        for (programHierarchyDetails topentity : topLevelEntities) {

            firstTierEntities newfirstTierEntity = new firstTierEntities();
            newfirstTierEntity.setId(topentity.getId());
            newfirstTierEntity.setName(topentity.getName());

            List secondLevelEntities = hierarchymanager.getProgramOrgHierarchyItems(programId, 2, topentity.getId(),
                    userId);

            List<secondTierEntities> tier2EntityList = new ArrayList<secondTierEntities>();

            if (!secondLevelEntities.isEmpty() && secondLevelEntities.size() > 0) {

                for (ListIterator iter = secondLevelEntities.listIterator(); iter.hasNext();) {

                    Object[] row = (Object[]) iter.next();

                    secondTierEntities newSecondTierEntity = new secondTierEntities();
                    newSecondTierEntity.setId(Integer.parseInt(row[0].toString()));
                    newSecondTierEntity.setName(row[1].toString());
                    newSecondTierEntity.setLastSurveyTaken("");
                    newSecondTierEntity.setLastSurveyTakenOn(null);

                    //Encrypt the use id to pass in the url
                    map = new HashMap<String, String>();
                    map.put("id", Integer.toString(Integer.parseInt(row[0].toString())));
                    map.put("topSecret", topSecret);

                    String[] encrypted = encrypt.encryptObject(map);

                    newSecondTierEntity.setEncryptedId(encrypted[0]);
                    newSecondTierEntity.setEncryptedSecret(encrypted[1]);

                    tier2EntityList.add(newSecondTierEntity);

                }

                newfirstTierEntity.setTier2EntityList(tier2EntityList);
            }

            tier1EntityList.add(newfirstTierEntity);
        }

        mav.addObject("tier1EntityList", tier1EntityList);

        //Encrypt the use id to pass in the url
        mav.addObject("surveyId", surveyId);

        return mav;

    }

}