An example Struts2 Action class


package com.sutisoft.sutigrc.assetMgmt;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.Hibernate;

import com.opensymphony.xwork2.ActionSupport;
import com.sutisoft.sutigrc.businessData.BusinessLocationsLogic;
import com.sutisoft.sutigrc.businessData.BusinessUnitsLogic;
import com.sutisoft.sutigrc.commons.DateFormatter;
import com.sutisoft.sutigrc.commons.MasterLabel;
import com.sutisoft.sutigrc.commons.SelectComboData;
import com.sutisoft.sutigrc.hibernate.AssetAttachments;
import com.sutisoft.sutigrc.hibernate.AssetDetails;
import com.sutisoft.sutigrc.hibernate.AssetService;
import com.sutisoft.sutigrc.hibernate.AssetServiceAttachments;
import com.sutisoft.sutigrc.hibernate.AssetServiceAttachmentsDAO;
import com.sutisoft.sutigrc.hibernate.AssetTypes;
import com.sutisoft.sutigrc.hibernate.IdentityDetails;
import com.sutisoft.sutigrc.identityMgmt.IdentityLogic;
import com.sutisoft.sutigrc.vendorMgmt.VendorListLogic;

public class AssetAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(AssetAction.class.getName());
private static final String IDENTITYDETAILS = "identityDetails";
private static final String LISTPAGE = "listPage";
private static final String DEFINEPAGE = "assetDefine";
private static final String DATEFORMATVALUE = "dateFormatValue";
private static final String ATTACHMENTDETAILS = "attachmentDetails";
private static final String FILESIZE = "fileSize";
private static final String FORWARD = "globalLogin";
private static final String SORTLIST = "sortList";
private static final String HISTORY = "assetHistory";
private static final String SERVICE = "assetService";

private String assetName;
private Integer assetType;
private Integer location;
private Integer assetStatus;
private Integer businessUnit;
private Integer condition;
private String manufacturer;
private String brand;
private String serialNo;
private String model;
private String description;
private File assetImage;
private File documents;
private String contentType;
private String fileName;
private String purchaseDate;
private String inserviceDate;
private Integer vendorName;
private String warrantyExpires;
private String poNumber;
private String purchasePrice;
private String currentValue;
private String scrapValue;

private Integer assetEditId;
private String fromDate;
private String toDate;

private Integer[] searchAssetIds;
private Integer[] searchAssetTypes;
private String[] searchAssetNames;
private Integer[] searchAssetStatus;
private Integer[] searchAssetLocation;
private Integer[] searchBusinessUnit;
private Integer[] searchVendors;
private String[] searchManufacturer;
private Integer[] searchAssetCondition;
private String[] searchMonitorStatus;

private String tdId;
private String sortId;
private String sortType;
private String searchId;
private String reportStatus;

private Integer quickSearchNameId;

private Integer attachmentID;

AssetAttachmentForm attachForm = new AssetAttachmentForm();

public AssetAttachmentForm attachForm() {
return attachForm;
}

@SkipValidation
public String displayAssetsList() {
logger.info("in displayAssetList() of AssetAction class");

HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
AssetLogic logic = new AssetLogic();

Map<Integer, String> assetIDsMap = new LinkedHashMap<Integer, String>();
Map<Integer, String> assetTypesMap = new LinkedHashMap<Integer, String>();
Map<String, String> assetNamesMap = new LinkedHashMap<String, String>();
Map<String, String> manufacturersMap = new LinkedHashMap<String, String>();
Map<String, String> monitorStatusMap = new LinkedHashMap<String, String>();
Map assetStatusMap = new LinkedHashMap();
Map assetConditionMap = new LinkedHashMap();
Map locationMap = new LinkedHashMap();
Map businessUnitMap = new LinkedHashMap();
Map vendorsMap = new LinkedHashMap();
List assetsList;
try {
IdentityDetails identityDetails = (IdentityDetails) session.getAttribute(IDENTITYDETAILS);

if (reportStatus != null) {
assetsList = (ArrayList) logic.searchForReports(this, identityDetails, reportStatus);
} else {
assetsList = logic.getAssetsList(identityDetails);
}
logger.info("assets list size:" + assetsList.size());
session.setAttribute("assetsList", assetsList);

session.setAttribute("assetIDsMap", assetIDsMap);
session.setAttribute("assetNamesMap", assetNamesMap);
session.setAttribute("manufacturersMap", manufacturersMap);
session.setAttribute("assetStatusMap", assetStatusMap);
session.setAttribute("assetConditionMap", assetConditionMap);
session.setAttribute("assetTypesMap", assetTypesMap);
session.setAttribute("businessUnitMap", businessUnitMap);
session.setAttribute("locationMap", locationMap);
session.setAttribute("vendorsMap", vendorsMap);
session.setAttribute("monitorStatusMap", monitorStatusMap);

if (reportStatus != null) {
this.setSearchId("Yes");
this.displaySearchFields();
}

} catch (Exception e) {
logger.error(e);
}
return LISTPAGE;
}

@SuppressWarnings("unchecked")
@SkipValidation
public String displaySearchFields() {
logger.info("in displaySearchFields() of AssetAction");

SelectComboData selectComboData = new SelectComboData();
BusinessUnitsLogic businessUnitsLogic = new BusinessUnitsLogic();
BusinessLocationsLogic businessLocationsLogic = new BusinessLocationsLogic();
VendorListLogic vendorLogic = new VendorListLogic();
AssetTypesLogic typesLogic = new AssetTypesLogic();

HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
try {
IdentityDetails identityDetails = (IdentityDetails) session.getAttribute(IDENTITYDETAILS);
Integer companyId = identityDetails.getCompanyDetails().getCompanyId();

List assetsList = (List) session.getAttribute("assetsList");
Map assetIDsMap = (LinkedHashMap) session.getAttribute("assetIDsMap");
Map assetNamesMap = (LinkedHashMap) session.getAttribute("assetNamesMap");
Map manufacturersMap = (LinkedHashMap) session.getAttribute("manufacturersMap");
Map assetTypesMap = (LinkedHashMap) session.getAttribute("assetTypesMap");
Map monitorStatusMap = (LinkedHashMap) session.getAttribute("monitorStatusMap");

Iterator assetIdsIter = assetsList.iterator();
while (assetIdsIter.hasNext()) {
AssetDetails bean = (AssetDetails) assetIdsIter.next();
assetIDsMap.put(bean.getAssetId(), bean.getAssetReference());
assetNamesMap.put(bean.getAssetName(), bean.getAssetName());
if (!bean.getManufacturer().equals(""))
manufacturersMap.put(bean.getManufacturer(), bean.getManufacturer());
}
session.setAttribute("assetIDsMap", assetIDsMap);
session.setAttribute("assetNamesMap", assetNamesMap);
session.setAttribute("manufacturersMap", manufacturersMap);

List list = typesLogic.getAssetTypesList(identityDetails);
Iterator iter = list.iterator();
while (iter.hasNext()) {
AssetTypes bean = (AssetTypes) iter.next();
assetTypesMap.put(bean.getTypeId(), bean.getTypeName());
}
session.setAttribute("assetTypesMap", assetTypesMap);

Map assetStatusMap = selectComboData.getLabelValues(MasterLabel.ASSETSTATUS, identityDetails);
session.setAttribute("assetStatusMap", assetStatusMap);

Map assetConditionMap = selectComboData.getLabelValues(MasterLabel.ASSETCONDITION, identityDetails);
session.setAttribute("assetConditionMap", assetConditionMap);

Map locationMap = businessLocationsLogic.getBusinessLocationsList(companyId);
session.setAttribute("locationMap", locationMap);

Map businessUnitMap = businessUnitsLogic.getBusinessUnitsList(companyId);
session.setAttribute("businessUnitMap", businessUnitMap);

Map vendorsMap = vendorLogic.getActiveVendors(identityDetails);
session.setAttribute("vendorsMap", vendorsMap);

monitorStatusMap.put("CHECKED-IN", "CHECKED-IN");
monitorStatusMap.put("CHECKED-OUT", "CHECKED-OUT");
session.setAttribute("monitorStatusMap", monitorStatusMap);

} catch (Exception e) {
logger.error(e);
}
return SUCCESS;
}

@SkipValidation
public String definePage() {
logger.info("in definePage() of AssetAction class");

HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
AssetTypesLogic typesLogic = new AssetTypesLogic();
SelectComboData selectComboData = new SelectComboData();
BusinessUnitsLogic businessUnitsLogic = new BusinessUnitsLogic();
BusinessLocationsLogic businessLocationsLogic = new BusinessLocationsLogic();
VendorListLogic vendorLogic = new VendorListLogic();
IdentityLogic idLogic = new IdentityLogic();

Map<Integer, String> assetTypesMap;
Map locationsMap;
Map assetStatusMap;
Map businessUnitMap;
Map conditionMap;
Map ownersMap;
Map assignedToMap;
Map vendorsMap;
try {

IdentityDetails identityDetails = (IdentityDetails) session.getAttribute(IDENTITYDETAILS);
Integer companyId = identityDetails.getCompanyDetails().getCompanyId();

locationsMap = new LinkedHashMap();
assetStatusMap = new LinkedHashMap();
businessUnitMap = new LinkedHashMap();
conditionMap = new LinkedHashMap();
ownersMap = new LinkedHashMap();
assignedToMap = new LinkedHashMap();
vendorsMap = new LinkedHashMap();

assetTypesMap = new LinkedHashMap<Integer, String>();
List list = typesLogic.getAssetTypesList(identityDetails);
Iterator iter = list.iterator();
while (iter.hasNext()) {
AssetTypes assetTypes = (AssetTypes) iter.next();
Integer optionKey = assetTypes.getTypeId();
String optionValue = assetTypes.getTypeName();
assetTypesMap.put(optionKey, optionValue);
}
session.setAttribute("assetTypesMap", assetTypesMap);

assetStatusMap = selectComboData.getLabelValues(MasterLabel.ASSETSTATUS, identityDetails);
session.setAttribute("assetStatusMap", assetStatusMap);

businessUnitMap = businessUnitsLogic.getBusinessUnitsList(companyId);
session.setAttribute("businessUnitMap", businessUnitMap);

conditionMap = selectComboData.getLabelValues(MasterLabel.ASSETCONDITION, identityDetails);
session.setAttribute("conditionMap", conditionMap);

locationsMap = businessLocationsLogic.getBusinessLocationsList(companyId);
session.setAttribute("locationsMap", locationsMap);

vendorsMap = vendorLogic.getActiveVendors(identityDetails);
session.setAttribute("vendorsMap", vendorsMap);

ownersMap = idLogic.getEmployeeNamesWithOutSel(companyId);
session.setAttribute("ownersMap", ownersMap);

assignedToMap = idLogic.getEmployeeNamesWithOutSel(companyId);
session.setAttribute("assignedToMap", assignedToMap);

if (assetEditId == null) {
session.removeAttribute(ATTACHMENTDETAILS);
this.setAssetName(null);
this.setAssetType(null);
this.setAssetStatus(null);
this.setCondition(null);
this.setBusinessUnit(null);
this.setLocation(null);
this.setManufacturer(null);
this.setBrand(null);
this.setSerialNo(null);
this.setModel(null);
this.setDescription(null);
this.setPurchaseDate(null);
this.setInserviceDate(null);
this.setVendorName(null);
this.setWarrantyExpires(null);
this.setPoNumber(null);
this.setPurchasePrice(null);
this.setCurrentValue(null);
this.setScrapValue(null);
}
session.removeAttribute("assetImagePath");

} catch (Exception e) {
logger.error(e);
}
return DEFINEPAGE;
}

public static String getExtension(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}

@SkipValidation
public String uploadAssetImage() {
logger.info("in uploadAssetImage of AssetAction");
logger.info("asset image :" + assetImage);
logger.info("asset file name :" + fileName);
ServletContext context = ServletActionContext.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
try {
Integer userId = (Integer) session.getAttribute("userId");
String absolutePath = context.getRealPath("uploaded");
logger.info("absolute Path is:" + absolutePath);
String relativePath = absolutePath + "/" + "AssetImages" + "/" + userId;
logger.info("relative path is:" + relativePath);
File destDir = new File(relativePath);
if (!destDir.exists()) {
FileUtils.forceMkdir(destDir);
}
Date date = new Date();
Long currentTime = date.getTime();
String imageFileName = currentTime + getExtension(fileName);
relativePath = relativePath + "/" + imageFileName;
destDir = new File(relativePath);
FileResource.copy(assetImage, destDir);
relativePath = "uploaded" + "/" + "AssetImages" + "/" + userId + "/" + imageFileName;
logger.info("------->" + relativePath);
session.setAttribute("assetImagePath", relativePath);
} catch (Exception e) {
logger.error(e);
}
return DEFINEPAGE;
}

@SkipValidation
public String removeAssetImage() {
logger.info("in removeAssetImage() of Asset Action class");

HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
session.removeAttribute("assetImagePath");

return DEFINEPAGE;
}

public String saveAsset() {
logger.info("in saveAsset() of AssetAction class");
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
AssetLogic logic = new AssetLogic();
ArrayList attachedDocuments = null;
Integer result = 0;
try {
IdentityDetails identityDetails = (IdentityDetails) session.getAttribute(IDENTITYDETAILS);
String dateFormatValue = (String) session.getAttribute(DATEFORMATVALUE);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormatValue, Locale.ENGLISH);

Date purchaseDate1 = null;
Date inserviceDate1 = null;
Date warrantyExpires1 = null;
if (!purchaseDate.equals(""))
purchaseDate1 = sdf.parse(purchaseDate);
if (!inserviceDate.equals(""))
inserviceDate1 = sdf.parse(inserviceDate);
if (!warrantyExpires.equals(""))
warrantyExpires1 = sdf.parse(warrantyExpires);

if (session.getAttribute(ATTACHMENTDETAILS) != null) {
attachedDocuments = (ArrayList) session.getAttribute(ATTACHMENTDETAILS);
}
String assetImagePath = (String) session.getAttribute("assetImagePath");
result = logic.saveAsset(this, identityDetails, purchaseDate1, inserviceDate1, warrantyExpires1, attachedDocuments, assetImagePath);
logger.info("result is:" + result);
if (result == 1) {
this.addActionMessage(getText("asset.define.saveSuccess"));
this.displayAssetsList();
} else {
this.addActionError(getText("asset.define.saveFailed"));
return DEFINEPAGE;
}
} catch (Exception e) {
logger.error(e);
}
return LISTPAGE;
}

@SkipValidation
public String editAsset() {
logger.info("in editAsset() of AssetAction class");
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
AssetLogic logic = new AssetLogic();
logger.info("edit change id is:" + assetEditId);
try {
String dateFormatValue = (String) session.getAttribute("dateFormatValue");
definePage();
logic.editAsset(this, assetEditId, dateFormatValue);
List attachmentList = logic.editAttachments(assetEditId);
List<AssetAttachmentForm> attachmentDetails = new ArrayList<AssetAttachmentForm>();
float fileSize = 0;
for (int i = 0; i < attachmentList.size(); i++) {
AssetAttachments assetAttachments = (AssetAttachments) attachmentList.get(i);
AssetAttachmentForm attachForm = new AssetAttachmentForm();
attachForm.setFileName(assetAttachments.getAttachmentName());
attachForm.setAttachmentContent(assetAttachments.getAttachment());
attachmentDetails.add(attachForm);
Blob fileBlob = assetAttachments.getAttachment();
fileSize += fileBlob.length();
fileSize = (float) (fileSize / 1024) / 1024;
}
session.setAttribute(ATTACHMENTDETAILS, attachmentDetails);
session.setAttribute(FILESIZE, fileSize);
if (getFileName() != null)
session.setAttribute("assetImagePath", getFileName());

} catch (Exception e) {
logger.error(e);
}
return DEFINEPAGE;
}

public String updateAsset() {
logger.info("in updateAsset() of AssetAction class");

logger.info("in saveAsset()");
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
AssetLogic logic = new AssetLogic();
Integer result = 0;
ArrayList attachedDocuments = null;
try {
IdentityDetails identityDetails = (IdentityDetails) session.getAttribute(IDENTITYDETAILS);
String dateFormatValue = (String) session.getAttribute(DATEFORMATVALUE);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormatValue, Locale.ENGLISH);

Date purchaseDate1 = null;
Date inserviceDate1 = null;
Date warrantyExpires1 = null;
if (!purchaseDate.equals(""))
purchaseDate1 = sdf.parse(purchaseDate);
if (!inserviceDate.equals(""))
inserviceDate1 = sdf.parse(inserviceDate);
if (!warrantyExpires.equals(""))
warrantyExpires1 = sdf.parse(warrantyExpires);

if (session.getAttribute(ATTACHMENTDETAILS) != null) {
attachedDocuments = (ArrayList) session.getAttribute(ATTACHMENTDETAILS);
}
String assetImagePath = (String) session.getAttribute("assetImagePath");
result = logic.updateAsset(this, identityDetails, purchaseDate1, inserviceDate1, warrantyExpires1, assetEditId, attachedDocuments, assetImagePath);
logger.info("result is:" + result);
if (result == 1) {
this.addActionMessage(getText("asset.define.updateSuccess"));
this.displayAssetsList();
this.setAssetEditId(null);
} else {
this.addActionError(getText("asset.define.updateFailed"));
return DEFINEPAGE;
}
} catch (Exception e) {
logger.error(e);
}
return LISTPAGE;
}

@SkipValidation
public String searchAssets() {
logger.info("in searchAssets() of AssetAction class");

HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
DateFormatter dateFormatter = new DateFormatter();
AssetLogic assetLogic = new AssetLogic();
List assetsList = new ArrayList();
searchId = "Yes";
try {
Integer companyId = (Integer) session.getAttribute("companyId");
String dateFormatValue = (String) session.getAttribute(DATEFORMATVALUE);
Date fromDate_ = null;
Date toDate_ = null;
if (!fromDate.equals("") && fromDate != null) {
fromDate_ = dateFormatter.convertToDatabaseDate(fromDate, dateFormatValue, "fromDate");
}
if (!toDate.equals("") && toDate != null) {
toDate_ = dateFormatter.convertToDatabaseDate(toDate, dateFormatValue, "toDate");
}
if (!fromDate.equals("") && !toDate.equals("")) {
if (toDate_.compareTo(fromDate_) < 0) {
this.addActionError(getText("Invaliddate"));
return LISTPAGE;
}
}
assetsList = assetLogic.searchAssets(this, companyId, fromDate_, toDate_);
logger.info("assets list size:" + assetsList.size());
session.setAttribute("assetsList", assetsList);
} catch (Exception e) {
logger.error(e);
return FORWARD;
}
return LISTPAGE;
}

@SkipValidation
public String sortingElements() {
logger.info("in sortingElements() of AssetAction class");
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
IdentityDetails identityDetails = (IdentityDetails) session.getAttribute("identityDetails");
try {
String type = "asc";
if (getSortType() != null)
if (this.getSortType().equalsIgnoreCase("asc"))
type = "desc";
String[] values = new String[2];
values[0] = this.getSortId();
values[1] = type;
List assetsList = new AssetLogic().getAssetsListForSorting(identityDetails, values);
session.setAttribute("assetsList", assetsList);
setSortId(sortId);
this.setSortType(type);
this.setTdId(this.getTdId());
} catch (Exception e) {
logger.error(e);
return FORWARD;
}
return SORTLIST;
}

@SuppressWarnings("unchecked")
@SkipValidation
public String attachUploadDocuments() {
logger.info("in attachUploadDocuments() of AssetAction class");
float fileSize = 0;
ArrayList<AssetAttachmentForm> attachmentDetails = null;
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
logger.info("attached document:--" + getDocuments());
logger.info("attached document name:--" + getFileName());
logger.info("attached content type:--" + getContentType());
String filename = getFileName();
ArrayList<Object> extention = new ArrayList<Object>();
StringTokenizer attachfile = new StringTokenizer(filename, ".");
while (attachfile.hasMoreTokens()) {
extention.add(attachfile.nextToken());
}
String attachfiletype = extention.get(extention.size() - 1).toString().trim();
ArrayList<String> fileextention = new ArrayList<String>();
fileextention.add("doc");
fileextention.add("ppt");
fileextention.add("xls");
fileextention.add("xlsx");
fileextention.add("txt");
fileextention.add("pdf");
fileextention.add("odp");
fileextention.add("ods");
fileextention.add("odt");
fileextention.add("rtf");
fileextention.add("docx");
fileextention.add("csv");
fileextention.add("jpg");
fileextention.add("jpeg");
fileextention.add("png");
fileextention.add("bmp");
fileextention.add("gif");
try {
if (!getDocuments().canRead()) {
addActionError(getText("issue.emptyfileuploadfailed"));
} else if (!fileextention.contains(attachfiletype.toLowerCase())) {
addActionError(getText("issue.uploadtransactionFailed"));
} else {
if (session.getAttribute(ATTACHMENTDETAILS) == null) {
attachmentDetails = new ArrayList<AssetAttachmentForm>();
} else {
List attachmentList = (List) session.getAttribute(ATTACHMENTDETAILS);
attachmentDetails = (ArrayList<AssetAttachmentForm>) attachmentList;
fileSize = (Float) session.getAttribute(FILESIZE);
}
File file = getDocuments();
FileInputStream fis = new FileInputStream(file);
Blob fileBlob = Hibernate.createBlob(fis);
long size = fileBlob.length();
float fileSize1 = (float) (size / 1024) / 1024;
fileSize = fileSize + fileSize1;

if (fileSize > 10.0) {
addActionError(getText("issue.attachmentSizeisexceed"));
return SUCCESS;
}
attachForm().setAttachmentContent(fileBlob);
attachForm().setFileName(getFileName());
attachmentDetails.add(attachForm());
session.setAttribute(ATTACHMENTDETAILS, attachmentDetails);
session.setAttribute(FILESIZE, fileSize);
}
} catch (Exception e) {
logger.error(e);
return FORWARD;
}
return SUCCESS;
}

@SkipValidation
public String removeUploadDocuments() {
logger.info("in removeUploadDocuments() of AssetAction class");
try {
logger.info("asset AttachmentList:--" + attachForm().getAttachmentdelete());
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();

ArrayList attachmentDetails = (ArrayList) session.getAttribute(ATTACHMENTDETAILS);
logger.info("AttachmentID:--" + getAttachmentID());
int attachemtID = getAttachmentID();
if (attachmentDetails.size() > 0) {
attachmentDetails.remove(attachemtID);
}
session.setAttribute(ATTACHMENTDETAILS, attachmentDetails);
} catch (Exception e) {
logger.error(e);
return FORWARD;
}
return SUCCESS;
}

@SkipValidation
public String downloadAssetDocument() {
logger.info("in downloadAssetDocument() of Asset Action");
String forward = null;
try {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ArrayList attachedDocuments = (ArrayList) session.getAttribute(ATTACHMENTDETAILS);
HttpServletResponse response = ServletActionContext.getResponse();
ArrayList<Object> extention = new ArrayList<Object>();
int attachemtID = getAttachmentID();
logger.info("attachment id is:--" + attachmentID);
logger.info("attached documents:" + attachedDocuments);

AssetAttachments beans = (AssetAttachments) attachedDocuments.get(attachemtID);
Blob data = beans.getAttachment();
String filename = beans.getAttachmentName();

logger.info("fileName:--" + filename);

StringTokenizer attachfile = new StringTokenizer(filename, ".");
while (attachfile.hasMoreTokens()) {
extention.add(attachfile.nextToken());
}
String attachfiletype = extention.get(extention.size() - 1).toString().trim();
response.setContentType(attachfiletype);
response.setHeader("Content-disposition", "attachment; filename=" + filename);
response.setHeader("Cache-Control", "max-age=0");

OutputStream outStream = response.getOutputStream();
InputStream inputStream = data.getBinaryStream();

byte[] buffer = new byte[inputStream.available()];
int temp = 0;
while ((temp = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, temp);
}
outStream.flush();
inputStream.close();
outStream.close();

} catch (Exception e) {
logger.error(e);
}
return forward;
}

@SkipValidation
public String downloadAssetServiceDocument() {
logger.info("in downloadAssetServiceDocument() of Asset Action");
String forward = null;
try {
HttpServletResponse response = ServletActionContext.getResponse();
ArrayList<Object> extention = new ArrayList<Object>();
logger.info("attachment id is:--" + attachmentID);

AssetServiceAttachments bean = new AssetServiceAttachmentsDAO().findById(attachmentID);
Blob data = bean.getAttachment();
String filename = bean.getAttachmentName();
logger.info("fileName:--" + filename);

StringTokenizer attachfile = new StringTokenizer(filename, ".");
while (attachfile.hasMoreTokens()) {
extention.add(attachfile.nextToken());
}
String attachfiletype = extention.get(extention.size() - 1).toString().trim();
response.setContentType(attachfiletype);
response.setHeader("Content-disposition", "attachment; filename=" + filename);
response.setHeader("Cache-Control", "max-age=0");

OutputStream outStream = response.getOutputStream();
InputStream inputStream = data.getBinaryStream();

byte[] buffer = new byte[inputStream.available()];
int temp = 0;
while ((temp = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, temp);
}
outStream.flush();
inputStream.close();
outStream.close();

} catch (Exception e) {
logger.error(e);
}
return forward;
}

@SkipValidation
public String displayAssetHistory() {
logger.info("in displayAssetHistory() of AssetAction");
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
AssetLogic logic = new AssetLogic();
AssetManageLogic logic2 = new AssetManageLogic();
List assetDetailsList = null;
List attachmentList = null;
List assetManageDetailsList = null;
try {
Integer assetId = Integer.parseInt((String) request.getParameter("assetId"));
logger.info("asset id is:" + assetId);

assetDetailsList = logic.getAssetHistory(assetId);
logger.info("list size:" + assetDetailsList.size());
session.setAttribute("assetHistoryList", assetDetailsList);

attachmentList = logic.editAttachments(assetId);
session.setAttribute("attachmentDetails", attachmentList);

assetManageDetailsList = logic.getAssetManageHistory(assetId);
session.setAttribute("assetManageDetailsList", assetManageDetailsList);

List listOfCheckLists = logic2.getCheckListsForAssetHistory(assetManageDetailsList);
session.setAttribute("listOfCheckLists", listOfCheckLists);

} catch (Exception e) {
logger.error(e);
}
return HISTORY;
}

@SkipValidation
public String displayAssetService() {
logger.info("in displayAssetService() of AssetAction");
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
AssetManageLogic logic2 = new AssetManageLogic();
List assetServiceDetailsList = null;
List<List> serviceAttachmentsList = new ArrayList<List>();
try {
Integer assetId = Integer.parseInt((String) request.getParameter("assetId"));
logger.info("asset id is:" + assetId);

String assetReference = (String) request.getParameter("assetRef");
logger.info("asset reference is:" + assetReference);
session.setAttribute("assetReference", assetReference);

assetServiceDetailsList = logic2.getAssetServiceList(assetId);
logger.info("service details list size:" + assetServiceDetailsList.size());
session.setAttribute("assetServiceList", assetServiceDetailsList);

if (assetServiceDetailsList.size() > 0) {
Iterator iter = assetServiceDetailsList.iterator();
while (iter.hasNext()) {
AssetService bean = (AssetService) iter.next();
List serviceAttachments = new AssetServiceAttachmentsDAO().findByProperty("assetService.serviceId", bean.getServiceId());
logger.info("service attachment individual size:" + serviceAttachments.size());
serviceAttachmentsList.add(serviceAttachments);
}
}
logger.info("service attachment list size:" + serviceAttachmentsList.size());
session.setAttribute("serviceAttachmentsList", serviceAttachmentsList);

} catch (Exception e) {
logger.error(e);
}
return SERVICE;
}

@SkipValidation
public String getQuickAssetDetails() {
ArrayList<AssetDetails> assetList = new ArrayList<AssetDetails>();
this.setSearchId("Yes");
try {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
AssetLogic assetLogic = new AssetLogic();
AssetDetails assetDetails = (AssetDetails) assetLogic.getQuickAssetDetails(getQuickSearchNameId());
String searchedName = assetDetails.getAssetName();
displayAssetsList();
displaySearchFields();
String[] name = new String[1];
name[0] = searchedName;
this.setSearchAssetNames(name);
assetList.add(assetDetails);
session.setAttribute("assetsList", assetList);
} catch (Exception e) {
logger.error(e);
}
return "listPage";

}

public String getAssetName() {
return assetName;
}

public void setAssetName(String assetName) {
this.assetName = assetName;
}

public Integer getAssetType() {
return assetType;
}

public void setAssetType(Integer assetType) {
this.assetType = assetType;
}

public Integer getLocation() {
return location;
}

public void setLocation(Integer location) {
this.location = location;
}

public Integer getAssetStatus() {
return assetStatus;
}

public void setAssetStatus(Integer assetStatus) {
this.assetStatus = assetStatus;
}

public Integer[] getSearchAssetCondition() {
return searchAssetCondition;
}

public void setSearchAssetCondition(Integer[] searchAssetCondition) {
this.searchAssetCondition = searchAssetCondition;
}

public Integer getBusinessUnit() {
return businessUnit;
}

public void setBusinessUnit(Integer businessUnit) {
this.businessUnit = businessUnit;
}

public Integer getCondition() {
return condition;
}

public void setCondition(Integer condition) {
this.condition = condition;
}

public String getManufacturer() {
return manufacturer;
}

public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

public String getSerialNo() {
return serialNo;
}

public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getPurchaseDate() {
return purchaseDate;
}

public void setPurchaseDate(String purchaseDate) {
this.purchaseDate = purchaseDate;
}

public String getInserviceDate() {
return inserviceDate;
}

public void setInserviceDate(String inserviceDate) {
this.inserviceDate = inserviceDate;
}

public Integer getVendorName() {
return vendorName;
}

public void setVendorName(Integer vendorName) {
this.vendorName = vendorName;
}

public String getPoNumber() {
return poNumber;
}

public void setPoNumber(String poNumber) {
this.poNumber = poNumber;
}

public String getPurchasePrice() {
return purchasePrice;
}

public void setPurchasePrice(String purchasePrice) {
this.purchasePrice = purchasePrice;
}

public String getCurrentValue() {
return currentValue;
}

public void setCurrentValue(String currentValue) {
this.currentValue = currentValue;
}

public String getScrapValue() {
return scrapValue;
}

public void setScrapValue(String scrapValue) {
this.scrapValue = scrapValue;
}

public File getAssetImage() {
return assetImage;
}

public void setAssetImage(File assetImage) {
this.assetImage = assetImage;
}

public File getDocuments() {
return documents;
}

public void setDocuments(File documents) {
this.documents = documents;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public void setDocumentsContentType(String contentType) {
this.contentType = contentType;
}

public String getFileName() {
return fileName;
}

public void setDocumentsFileName(String fileName) {
this.fileName = fileName;
}

public void setAssetImageFileName(String fileName) {
this.fileName = fileName;
}

public Integer getAssetEditId() {
return assetEditId;
}

public void setAssetEditId(Integer assetEditId) {
this.assetEditId = assetEditId;
}

public String getFromDate() {
return fromDate;
}

public void setFromDate(String fromDate) {
this.fromDate = fromDate;
}

public String getToDate() {
return toDate;
}

public void setToDate(String toDate) {
this.toDate = toDate;
}

public Integer[] getSearchAssetIds() {
return searchAssetIds;
}

public void setSearchAssetIds(Integer[] searchAssetIds) {
this.searchAssetIds = searchAssetIds;
}

public Integer[] getSearchAssetTypes() {
return searchAssetTypes;
}

public void setSearchAssetTypes(Integer[] searchAssetTypes) {
this.searchAssetTypes = searchAssetTypes;
}

public String[] getSearchAssetNames() {
return searchAssetNames;
}

public void setSearchAssetNames(String[] searchAssetNames) {
this.searchAssetNames = searchAssetNames;
}

public Integer[] getSearchAssetStatus() {
return searchAssetStatus;
}

public void setSearchAssetStatus(Integer[] searchAssetStatus) {
this.searchAssetStatus = searchAssetStatus;
}

public Integer[] getSearchAssetLocation() {
return searchAssetLocation;
}

public void setSearchAssetLocation(Integer[] searchAssetLocation) {
this.searchAssetLocation = searchAssetLocation;
}

public Integer[] getSearchBusinessUnit() {
return searchBusinessUnit;
}

public void setSearchBusinessUnit(Integer[] searchBusinessUnit) {
this.searchBusinessUnit = searchBusinessUnit;
}

public Integer[] getSearchVendors() {
return searchVendors;
}

public void setSearchVendors(Integer[] searchVendors) {
this.searchVendors = searchVendors;
}

public String[] getSearchManufacturer() {
return searchManufacturer;
}

public void setSearchManufacturer(String[] searchManufacturer) {
this.searchManufacturer = searchManufacturer;
}

public String getTdId() {
return tdId;
}

public void setTdId(String tdId) {
this.tdId = tdId;
}

public String getSortId() {
return sortId;
}

public void setSortId(String sortId) {
this.sortId = sortId;
}

public String getSortType() {
return sortType;
}

public void setSortType(String sortType) {
this.sortType = sortType;
}

public Integer getAttachmentID() {
return attachmentID;
}

public void setAttachmentID(Integer attachmentID) {
this.attachmentID = attachmentID;
}

public String getSearchId() {
return searchId;
}

public void setSearchId(String searchId) {
this.searchId = searchId;
}

public String getReportStatus() {
return reportStatus;
}

public void setReportStatus(String reportStatus) {
this.reportStatus = reportStatus;
}

public Integer getQuickSearchNameId() {
return quickSearchNameId;
}

public void setQuickSearchNameId(Integer quickSearchNameId) {
this.quickSearchNameId = quickSearchNameId;
}

public String getWarrantyExpires() {
return warrantyExpires;
}

public void setWarrantyExpires(String warrantyExpires) {
this.warrantyExpires = warrantyExpires;
}

public String[] getSearchMonitorStatus() {
return searchMonitorStatus;
}

public void setSearchMonitorStatus(String[] searchMonitorStatus) {
this.searchMonitorStatus = searchMonitorStatus;
}

}