多个画面使用一个类来管理,subrequest变量使用

public static String className = ManageAssetFacesBean.class.getName();

public static Log logger = LogFactory.getLog(ManageAssetFacesBean.class);


public static final String TYPE_CREATE = "create";
public static final String TYPE_COPY   = "copy";
public static final String TYPE_UPDATE = "update";

private String operationType = TYPE_CREATE;

private Asset asset = new Asset();
private AssetPlan assetPlan;

//When do 'edit' asset operation, this asset will stores the original value retrieved from database,
//and "asset" attribute stores the updated data which will be pushed back to the database
private Asset assetCopy = new Asset();

private AssociateCUPlanBean associateBean = new AssociateCUPlanBean();

private boolean isSubmitOutputAssetDocNumber = false;
   
    // the tip text when assoicate asset to asset plan ;
    private String assoicateTipText;
   
private int subRequestCount = 0;

private int lastAssetType;
//onload method

public void onload(FacesContext context) throws SQLException{
// System.out.println("enter asset page onload..., subRequestCount is " + subRequestCount);

if (hasError()) {
return;
}
       
if(subRequestCount > 0){
subRequestCount--;
return;
}

HttpServletRequest request = getRequest();
operationType = request.getParameter("type");
if(operationType == null){
operationType = TYPE_CREATE;
}

asset = new Asset();

//This statement is just for create new asset type
asset.setStatus(Asset.STATUS_DRAFT);

String assetID = request.getParameter("assetID");
populateAsset(assetID);


//clear output asset doc number config info
if(TYPE_CREATE.equals(operationType)){
asset.getOutputAssetDocNumList().clear();
asset.setQuantity(1);
}

//populate available assetTypes
//List assetTypes    = ManageAssetService.$.retrieveSuperModuleAssetTypes();
List assetTypes    = ManageAssetService.$.retrieveAssetTypes(true);
List assetTypeItems = E2EUIUtility.composeSelectItemList(assetTypes);
asset.setAvaiAssetTypes(assetTypeItems);
}

/**
* When the doc number config page is loaded, this method will be called
* @param context
*/
public void onload_docNumConfig(FacesContext context) throws SQLException{
// System.out.println("enter doc number on load...");
if(this.isSubmitOutputAssetDocNumber == true){
this.isSubmitOutputAssetDocNumber = false;
}else{
    int contentType = asset.getTypeID();
int oldType = assetCopy.getTypeID();
if(contentType == oldType){
    asset.setOutputAssetDocNumList(assetCopy.getOutputAssetDocNumList());
}
else if(contentType !=lastAssetType){
    lastAssetType = contentType;
List outputAssetTypeList = ManageAssetService.$.retrieveOutputAssetTypes(contentType);
List docNumList =  new ArrayList();
if(outputAssetTypeList.size() > 0){ //is super module
// System.out.println("is output asset");
int childNum = outputAssetTypeList.size();
for(int i = 0; i < childNum; i++){
OutputAsset outputAsset = new OutputAsset();
outputAsset.setName(((AssetType)outputAssetTypeList.get(i)).getName());
docNumList.add(outputAsset);
}
}
asset.setOutputAssetDocNumList(docNumList);
}
}
}

/**
* setup output asset doc number
*
*/
public void setupOutputAssetDocNumber(){
this.isSubmitOutputAssetDocNumber = true;
}

//action methods

/**
* Load the default average page
*/
public void loadDefaultPageNum() throws SQLException {
        this.subRequestCount++;
        HttpServletRequest request = getRequest();
        String assetTypeID = request.getParameter("assetTypeID");
        AssetType type = ManageAssetService.$.retrieveAssetTypeByID(Integer.parseInt(assetTypeID));
        int avgPage = type.getAvgPage();       
        asset.setAvgPages(avgPage);
    }

/**
* Load the default average words per page
*
*/
public void loadDefaultWordNum() throws SQLException{
this.subRequestCount++;
HttpServletRequest request = getRequest();
String assetTypeID = request.getParameter("assetTypeID");
AssetType type = ManageAssetService.$.retrieveAssetTypeByID(Integer.parseInt(assetTypeID));
int avgWord = type.getAvgWordPerPage();
asset.setAvgWordsPerPage(avgWord);
}

/**
* Set asset content type before
*/
public void setAssetType(){
HttpServletRequest request = getRequest();
String assetTypeID = request.getParameter("assetTypeID");
asset.setTypeID(Integer.parseInt(assetTypeID));
}

/**
* Create Consumablue Unit-- The type could be
* Product CU or Asset CU
* @throws Exception
*/
public String createAsset()  throws Exception{

asset.setId(0);
if (!asset.getStatus().equalsIgnoreCase(Asset.STATUS_DRAFT)) {
asset.setStatus(Asset.STATUS_DRAFT);
}
//We need to get the correct data before validation
ManageAssetService.$.getPreparedForDBOperation(asset);

if (!validateAsset(asset)) {
return "";
}
ManageAssetService.$.createAsset(asset);
        this.operationType = TYPE_UPDATE;
return "createSuccess";
}

public boolean validateAsset(Asset tempAsset) {
boolean result = true;

if ("Yes".equals(tempAsset.getLink_to_ann())
&& (tempAsset.getAnnouncement() == null || tempAsset
.getAnnouncement().length() == 0)) {
FacesContext.getCurrentInstance().addMessage(
"form1:Ann",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"The announcement could not be empty.",
"The announcement could not be empty."));
result = false;
}

if (tempAsset.getTranslationStartDate().after(
tempAsset.getPublishDate())) {
FacesContext
.getCurrentInstance()
.addMessage(
"form1:translationStartDate",
new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"The translation start date should not be after the publish date .",
"The translation start date should not be after the publish date ."));
result = false;

}
try {
if (ManageAssetService.$.isNameUsed(tempAsset.getName(), tempAsset
.getBuCode(), tempAsset.getId())) {
FacesContext.getCurrentInstance().addMessage(
"form1:assetName",
new FacesMessage("Please check the name.",
"The name has been used in \""
+ tempAsset.getBuCode() + "\"."));
result = false;

}
String message = checkBudget(tempAsset);
       if(message.length()>0){
FacesContext.getCurrentInstance().addMessage(
"form1:translationCost",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
                 message,
                 message + "Please add budget first."));
result = false;

       }
      
} catch (SQLException e) {
e.printStackTrace();
result = false;
}

return result;
}


private String checkBudget(Asset asset) throws SQLException {
        if (asset.getStatus() == Asset.STATUS_DRAFT || !"GTS".equals(asset.getBuCode())) {
            return "";
        }
        List lAssets = asset.getTargetLanguages();
        BudgetPlan bp = null;
        String message = "";
        String language = "";
        for (Iterator it = lAssets.iterator(); it.hasNext();) {
            LAsset lAsset = (LAsset) it.next();
            if (lAsset.getPrioritized().equals(LAsset.PRIORITIZE_YES)) {
                if (assetPlan == null) {
                    assetPlan = ManageAssetPlanService.$.
                    retrieveAssetPlan(Integer.valueOf(asset.getAssetPlanIDs()).intValue());
                }
                LBudgetPlan tempLBudgetPlan = null;
                if (bp == null) {
                    bp = ManageBudgetPlanService.$.retrieveBudgetPlan(assetPlan.getBudgetPlanId());
                    if (bp == null) {
                        return "";
                    }
                    Map lbpMap = bp.getLanguageBudgets();
                    tempLBudgetPlan = (LBudgetPlan) lbpMap.get(lAsset.getLanguageID());
                }
               
                if (tempLBudgetPlan == null) {
                    return ("The selected language " + lAsset.getLanguageName()
                            + " isn't included in budget plan " + bp.getName() + ". ");                   
                }
                double costEstimation = lAsset.getEstCost() + lAsset.getEstProCost();
                double actualCost = lAsset.getActualProCost() + lAsset.getActualTransCost();
                double remaining = tempLBudgetPlan.getRemainingAmount() + actualCost - costEstimation;               
                if (remaining < 0) {
                     language += "," + tempLBudgetPlan.getLanguageName();                   
                }
            }
        }

        if (!StringUtils.isBlank(language)) {
            message = "There is no more budget for " + language + ".";           
        }
        return message;
    }
/**
* Update Consumable Unit -- The type could be Product CU or Asset CU
*
* @return
* @throws SQLException
*/
public String updateAsset() throws Exception{
   
        //We need to get the correct data before validation
ManageAssetService.$.getPreparedForDBOperation(asset);
        if(!validateAsset(asset)){
            return "";
        }

ManageAssetService.$.updateAsset(asset,assetPlan);
return "updateSuccess";
}


/**
* Refine the asset plan records, only active asset plan can be associated to
* @return
* @throws SQLException
*/
private String getBUCodebyAsset(String assetIDs) throws SQLException {
assoicateTipText = "";
System.out.println("assetIDs:"+assetIDs);

List BUs = ManageAssetService.$.retrieveAssetBU(assetIDs);
if (BUs != null && BUs.size() == 1) {
associateBean.setSelectedCUIDs(assetIDs);
return (String) BUs.get(0);
} else {
assoicateTipText = "The assets selected must belong to one business unit.";
}

return null;
}

private void retrieveLinkablePlans(String bu) throws SQLException {
assoicateTipText = "";
AssetPlanSearchCriteria criteria = new AssetPlanSearchCriteria();
criteria.setBuCode(bu);
// List statusList = new ArrayList();
// statusList.add(AssetPlan.STATUS_DRAFT);
// statusList.add(AssetPlan.STATUS_PUBLISH);
// statusList.add(AssetPlan.STATUS_PRIORIRIE_REQUESTED);
// statusList.add(AssetPlan.STATUS_PRIORIRIE_RETURNED);
// statusList.add(AssetPlan.STATUS_IN_TRANSLATION);
// statusList.add(AssetPlan.STATUS_IN_VALIDATION);
// statusList.add(AssetPlan.STATUS_COMPLETE);
// criteria.setStatusList(statusList);

List plans = ManageAssetPlanService.$.retrieveAssetPlanBySC(criteria);
if (plans == null || plans.size() == 0) {
assoicateTipText = "There is no Product/Program that can be assoicate to.";
} else {
this.associateBean.setAssociatedPlans(plans);
}
// System.out.println("assoicateTipText: " + assoicateTipText);

}

public void onload_associatePlanPage(FacesContext context)
throws SQLException {
String assetIDs = getRequest().getParameter("assetIDs");

System.out.println("assetIDs:"+assetIDs);

associateBean = new AssociateCUPlanBean();

if (assetIDs != null && assetIDs.length() > 0 && !"0".equals(assetIDs)) {

AssetPlan tempAssetPlan;
Integer assetID;
List assetIDList = Arrays.asList(assetIDs.split(","));

// check whether the linked asset plan is in translation status;
for (Iterator it = assetIDList.iterator(); it.hasNext();) {
assetID = Integer.valueOf((String) it.next());


tempAssetPlan = ManageAssetPlanService.$
.retrieveAssetPlanByAssetID(assetID.intValue());

if (tempAssetPlan != null) {

if (AssetPlan.STATUS_IN_TRANSLATION
.equalsIgnoreCase(tempAssetPlan.getStatus())
|| AssetPlan.STATUS_IN_VALIDATION
.equalsIgnoreCase(tempAssetPlan.getStatus())
|| AssetPlan.STATUS_COMPLETE
.equalsIgnoreCase(tempAssetPlan.getStatus())) {
// give some notification
assoicateTipText = "The asset has been linked to Product/Program \""
+ tempAssetPlan.getName()
+ "\" which is already in \""
+ tempAssetPlan.getStatus()
+ "\" status, change the link is not allowed.";
return;
} else {

associateBean.setSelectedPlanIDs(String.valueOf(tempAssetPlan.getId()));
}
} else {
associateBean.setSelectedPlanIDs("");
}
}

String bu = getBUCodebyAsset(assetIDs);
if (bu != null)
retrieveLinkablePlans(bu);
} else {
String bu = getRequest().getParameter("bu");
retrieveLinkablePlans(bu);
}
}

/**
* Associate Assets to Plans (currently it's asset to asset plans, in the
* future, we may support cu&idplan)
*
*/
public String associateAssetToAssetPlan() throws Exception {
String result = "";
List assetIDs = Arrays.asList(associateBean.getSelectedCUIDs().split(
","));
List assetPlanIDs = Arrays.asList(associateBean.getSelectedPlanIDs()
.split(","));

int assetId = Integer.valueOf((String) assetIDs.get(0)).intValue();
int assetPlanId = Integer.valueOf((String) assetPlanIDs.get(0))
.intValue();

if (assetId > 0 && assetPlanId > 0) {
Asset asset = ManageAssetService.$.retrieveAsset(assetId);
AssetPlan ap = ManageAssetPlanService.$
.retrieveAssetPlan(assetPlanId);
try {
BaseDao.startTransaction();
ManageAssetService.$.deleteAssetToAssetPlanLinksByAsset(asset
.getId());
ManageAssetService.$.associateAssetToAssetPlan(asset, ap);
BaseDao.commitTransaction();
} finally {
BaseDao.endTransaction();
}
result = "success";
} else {

}

return result;
}


/**
* Estimate budget for Asset CU
*
* @throws SQLException
*/
public void showBudget() throws SQLException {
        HttpServletRequest request = getRequest();
        Asset tempAsset = ManageAssetService.$.composeCostEstimatorAssetBean(request);
              
        if (asset != null) {
            tempAsset.setTargetLanguages(asset.getTargetLanguages());
        }
        //Compose target languages
        ManageAssetService.$.composeLAssetList(tempAsset);
        //Estimate the cost
        CostEstimation costEstimation = CostEstimationService.$.calculateCost(tempAsset);

        asset.setEN_COST(costEstimation.getTranslationAndCDLayoutCharges());
        asset.setNLV_COST(costEstimation.getStiboProductionTranslationCost());
    }

public void estimateBudget() throws SQLException {
this.subRequestCount++;
      showBudget();
    }

/**
     * calculte the estimate budget for Asset CU By Sophia
     *
     * @throws SQLException
     */
    public void editBudgetOk() throws SQLException{

        HttpServletRequest request = getRequest();
//      retrieve parameters
        String hidEstProCosts = request.getParameter("hidEstProCosts");
        String hidEstTransCosts = request.getParameter("hidEstTransCosts");
        String hidActualHandlingCosts = request.getParameter("hidActualHandlingCosts");
        String hidActualTransCosts = request.getParameter("hidactualTransCosts");
       
        String[] estProCosts = new String[0];
        String[] estTransCosts = new String[0];
        String[] actualHandlingCosts = new String[0];
        String[] actualTransCosts = new String[0];
       
        if (hidEstProCosts != null) {
        estProCosts = hidEstProCosts.split("#");
        }
        if (hidEstTransCosts != null) {
        estTransCosts = hidEstTransCosts.split("#");
        }
        if (hidActualHandlingCosts != null) {
        actualHandlingCosts = hidActualHandlingCosts.split("#");
        }
        if (hidActualTransCosts != null) {
        actualTransCosts = hidActualTransCosts.split("#");
        }
       
        BigDecimal estProTotal = new BigDecimal(0.0);
        BigDecimal estTransTotal = new BigDecimal(0.0);
       
        //Set estimateProCost, estimateTransCost, actualProCost, actualTranCost to Asset
        List tarLanguages = asset.getTargetLanguages();
        for(int i=0;i<tarLanguages.size();i++){
            LAsset lasset =  (LAsset)tarLanguages.get(i);
            double estProCost = Double.parseDouble(estProCosts[i]);
            double estTransCost = Double.parseDouble(estTransCosts[i]);
            lasset.setEstProCost(estProCost);
            lasset.setEstCost(estTransCost);
            lasset.setActualProCost(Double.parseDouble(actualHandlingCosts[i]));
            lasset.setActualTransCost(Double.parseDouble(actualTransCosts[i]));
           
            estProTotal = estProTotal.add(new BigDecimal(lasset.getEstProCost()));
            estTransTotal = estTransTotal.add(new BigDecimal(lasset.getEstCost()));
        }
        asset.setEN_COST(estTransTotal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        asset.setNLV_COST(estProTotal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    }

/**
* Populate the Asset bean based on the assetid
* @param assetID
*/
private void populateAsset(String assetID) throws SQLException{

//ujjal added - retireve BUs based on user login  ************
Role role = SessionAccess.getSessionUserProfile().getCurrentRole();
TreeSet buSet = role.getBuSet();
List buList = new ArrayList();
for (Iterator iter = buSet.iterator(); iter.hasNext();)
{
com.ibm.g11n.e2e.ui.bean.BusinessUnit bu = (com.ibm.g11n.e2e.ui.bean.BusinessUnit) iter.next();
BusinessUnit newBU = new BusinessUnit();
newBU.setCode(bu.getBuid());
newBU.setName(bu.getBuid());
buList.add(newBU);
}

// System.out.println("buList size: " + buList.size());

if(assetID == null){
String buID = null;
if(buList.size()>0)
buID = ((BusinessUnit)buList.get(0)).getCode();
// System.out.println("buID: " + buID);
List majors = MajorService.$.retrieveMajorsByBU(buID);
asset.setAvaiMajors(E2EUIUtility.composeSelectItemList(majors));
if(majors != null && majors.size() > 0){
int majorID = asset.getMajorID();
if(majorID ==0)
majorID = ((Major)majors.get(0)).getId();
List minors = MinorService.$.retrieveMinors(majorID);
asset.setAvaiMinors(E2EUIUtility.composeSelectItemList(minors));
}
}else{
asset = ManageAssetService.$.retrieveAsset(Integer.valueOf(assetID).intValue());

if(operationType.equals(TYPE_COPY)){
asset = ManageAssetService.$.retrieveAsset(Integer.valueOf(assetID).intValue());
asset.setStatus(Asset.STATUS_DRAFT);
asset.setCreatedDate(new Date());
asset.setTargetLanguages(new ArrayList());
asset.setAssetPlanIDList(new ArrayList());
asset.setAssetPlanIDs(null);
asset.setAssetPlanNameList(new ArrayList());
asset.setAssetPlanNames(null);


}

//stores an copy of the asset into attribute "assetCopy"
// assetCopy = ManageAssetService.$.retrieveAsset(Integer.valueOf(assetID).intValue());

List majors = MajorService.$.retrieveMajorsByBU(asset.getBuCode()); //need
asset.setAvaiMajors(E2EUIUtility.composeSelectItemList(majors));
if(majors != null && majors.size() > 0){
List minors = MinorService.$.retrieveMinors(asset.getMajorID());
asset.setAvaiMinors(E2EUIUtility.composeSelectItemList(minors));
}
associateBean.setSelectedPlanIDs(asset.getAssetPlanIDs());


}

//set available BU List
asset.setAvaiBUs(buList);

}

/**
* Populate brand and minor list after user change the selected BU
*
*/
public void populateMajorList(){
this.subRequestCount++;
HttpServletRequest request = getRequest();
String buID = request.getParameter("buID");
List majors = MajorService.$.retrieveMajorsByBU(buID);
asset.setAvaiMajors(E2EUIUtility.composeSelectItemList(majors));
}

/**
*Popuate(update) minor List after user change the selected brand
*/
public void populateMinorList(){
this.subRequestCount++;
HttpServletRequest request = getRequest();
String majorID = request.getParameter("majorID");
if(majorID != null){
List minors = MinorService.$.retrieveMinors(Integer.valueOf(majorID).intValue());
asset.setAvaiMinors(E2EUIUtility.composeSelectItemList(minors));
}else{
// System.out.println("retrieve minor...");
String buID = request.getParameter("buID");
// System.out.println("bu id: " + buID);
List majors = MajorService.$.retrieveMajorsByBU(buID);
this.asset.setMajor(null); // set the selected brand to null
if(majors != null && majors.size() > 0){
// System.out.println("major id: " + ((Major)majors.get(0)).getId());
List minors = MinorService.$.retrieveMinors(((Major)majors.get(0)).getId());
// System.out.println("minor size: " + minors.size());
asset.setAvaiMinors(E2EUIUtility.composeSelectItemList(minors));
}
}

}

/**
*Estimate translation begin date based on publish date
*/
public void estimateTrDate(){
this.subRequestCount++;
String publishDateStr = getRequest().getParameter("publishDate");
// System.out.println("publish date: " + publishDateStr);

try {
Calendar publishDate = E2EUtility.getCalendar(publishDateStr);
publishDate.add(Calendar.DAY_OF_MONTH, -15);
Date translationDate = publishDate.getTime();
asset.setTranslationStartDate(translationDate);
// System.out.println("translation date: " + translationDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
}

// getter and setter method

public String getOperationType() {
return operationType;
}

public void setOperationType(String operationType) {
this.operationType = operationType;
}

public AssociateCUPlanBean getAssociateBean() {
return associateBean;
}

public void setAssociateBean(AssociateCUPlanBean associateBean) {
this.associateBean = associateBean;
}

public Asset getAsset() {
return asset;
}

public void setAsset(Asset asset) {
this.asset = asset;
}

    public String getAssoicateTipText() {
        return assoicateTipText;
    }

    public void setAssoicateTipText(String assoicateTipText) {
        this.assoicateTipText = assoicateTipText;
    }

    public int getSubRequestCount() {
        return subRequestCount;
    }

    public void setSubRequestCount(int subRequestCount) {
        this.subRequestCount = subRequestCount;
    }

// public Asset getAssetCopy() {
// return assetCopy;
// }
//
// public void setAssetCopy(Asset assetCopy) {
// this.assetCopy = assetCopy;
// }

public String getAssetPlanStatus() {

String status = AssetPlan.STATUS_DRAFT;
if(operationType.equalsIgnoreCase(TYPE_UPDATE)){
try {
status = ManageAssetPlanService.$
.selectAssetPlanStatusByAsset(asset.getId());
} catch (SQLException e) {
e.printStackTrace();
}
}

return status;
}

public void setAssetPlanStatus(String status) {

}

你可能感兴趣的:(request)