Advertisement
Guest User

ZoneAction.java

a guest
Jan 21st, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.01 KB | None | 0 0
  1. package actions;
  2.  
  3. import admin.dao.service.SharableService;
  4. import admin.dao.service.ZoneService;
  5. import com.opensymphony.xwork2.ActionContext;
  6. import com.opensymphony.xwork2.ActionSupport;
  7. import com.opensymphony.xwork2.ModelDriven;
  8. import com.opensymphony.xwork2.Preparable;
  9. import com.opensymphony.xwork2.ValidationAware;
  10. import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
  11. import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
  12. import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
  13. import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
  14. import com.opensymphony.xwork2.validator.annotations.Validations;
  15. import com.opensymphony.xwork2.validator.annotations.ValidatorType;
  16. import java.io.Serializable;
  17. import java.math.BigDecimal;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import model.Transporter;
  21. import model.ZoneTable;
  22. import org.apache.struts2.convention.annotation.Action;
  23. import org.apache.struts2.convention.annotation.InterceptorRef;
  24. import org.apache.struts2.convention.annotation.Namespace;
  25. import org.apache.struts2.convention.annotation.ParentPackage;
  26. import org.apache.struts2.convention.annotation.Result;
  27. import org.apache.struts2.convention.annotation.ResultPath;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import pagination.Paging;
  30.  
  31. @Namespace("/admin_side")
  32. @ResultPath("/WEB-INF/content")
  33. @ParentPackage(value="default")
  34. public final class ZoneAction extends ActionSupport implements Serializable, ValidationAware, Preparable, ModelDriven<ZoneTable>
  35. {
  36.     @Autowired
  37.     private final transient ZoneService zoneService=null;
  38.     @Autowired
  39.     private final transient SharableService sharableService=null;
  40.     private static final long serialVersionUID = 1L;
  41.     private int pageSize=5;
  42.  
  43.     private Long id;
  44.     private Boolean deleteOneRow;
  45.     private Boolean deleteMultipleRows;
  46.     private ZoneTable entity=new ZoneTable();
  47.     private List<ZoneTable>zones=new ArrayList<ZoneTable>();
  48.     private String message;
  49.     private List<Long>chk;
  50.     private Long deleteId;
  51.     private Long transporterId;
  52.     private List<Transporter> transporters;
  53.     private Transporter transporter;
  54.  
  55.     private Long begin;
  56.     private Long end;
  57.     private Long currentPage=1L;
  58.     private Long rowCount;
  59.     private Long totalPages;
  60.     private Integer status;
  61.  
  62.     public int getPageSize() {
  63.         return pageSize;
  64.     }
  65.  
  66.     public void setPageSize(int pageSize) {
  67.         this.pageSize = pageSize;
  68.     }
  69.  
  70.     public Long getId() {
  71.         return id;
  72.     }
  73.  
  74.     public void setId(Long id) {
  75.         this.id = id;
  76.     }
  77.  
  78.     public Boolean getDeleteOneRow() {
  79.         return deleteOneRow;
  80.     }
  81.  
  82.     public void setDeleteOneRow(Boolean deleteOneRow) {
  83.         this.deleteOneRow = deleteOneRow;
  84.     }
  85.  
  86.     public Boolean getDeleteMultipleRows() {
  87.         return deleteMultipleRows;
  88.     }
  89.  
  90.     @RequiredFieldValidator(type= ValidatorType.FIELD, key="delete.multiple.confirm")
  91.     public void setDeleteMultipleRows(Boolean deleteMultipleRows) {
  92.         this.deleteMultipleRows = deleteMultipleRows;
  93.     }
  94.  
  95.     public String getMessage() {
  96.         return message;
  97.     }
  98.  
  99.     public void setMessage(String message) {
  100.         this.message = message;
  101.     }
  102.  
  103.     public List<Long> getChk() {
  104.         return chk;
  105.     }
  106.  
  107.     public void setChk(List<Long> chk) {
  108.         this.chk = chk;
  109.     }
  110.  
  111.     public Long getDeleteId() {
  112.         return deleteId;
  113.     }
  114.  
  115.     public void setDeleteId(Long deleteId) {
  116.         this.deleteId = deleteId;
  117.     }
  118.  
  119.     public Transporter getTransporter() {
  120.         return transporter;
  121.     }
  122.  
  123.     public void setTransporter(Transporter transporter) {
  124.         this.transporter = transporter;
  125.     }
  126.  
  127.     public Long getBegin() {
  128.         return begin;
  129.     }
  130.  
  131.     public void setBegin(Long begin) {
  132.         this.begin = begin;
  133.     }
  134.  
  135.     public Long getEnd() {
  136.         return end;
  137.     }
  138.  
  139.     public List<Transporter> getTransporters() {
  140.         transporters=sharableService.getTransporterList();
  141.         return transporters;
  142.     }
  143.  
  144.     /*public void setTransporters(List<Transporter> transporters) {
  145.         this.transporters = transporters;
  146.     }*/
  147.  
  148.     public void setEnd(Long end) {
  149.         this.end = end;
  150.     }
  151.  
  152.     public Long getCurrentPage() {
  153.         return currentPage;
  154.     }
  155.  
  156.     public void setCurrentPage(Long currentPage) {
  157.         this.currentPage = currentPage;
  158.     }
  159.  
  160.     public Long getRowCount() {
  161.         return rowCount;
  162.     }
  163.  
  164.     public void setRowCount(Long rowCount) {
  165.         this.rowCount = rowCount;
  166.     }
  167.  
  168.     public Long getTotalPages() {
  169.         return totalPages;
  170.     }
  171.  
  172.     public void setTotalPages(Long totalPages) {
  173.         this.totalPages = totalPages;
  174.     }
  175.  
  176.     public Integer getStatus() {
  177.         return status;
  178.     }
  179.  
  180.     public void setStatus(Integer status) {
  181.         this.status = status;
  182.     }
  183.  
  184.     public Long getTransporterId() {
  185.         return transporterId;
  186.     }
  187.  
  188.     public void setTransporterId(Long transporterId) {
  189.         this.transporterId = transporterId;
  190.     }
  191.  
  192.     private void paginate()
  193.     {
  194.         rowCount=zoneService.rowCount();
  195.         Paging paging=new Paging(rowCount, pageSize, currentPage);
  196.         totalPages=Paging.getTotalPages(rowCount, pageSize);
  197.  
  198.         if(currentPage>totalPages||currentPage<=0)
  199.         {
  200.             currentPage=1L;begin=1L;
  201.             end=totalPages<10?paging.getEnd():10;
  202.         }
  203.         else
  204.         {
  205.             begin=paging.getBegin();
  206.             end=paging.getEnd();
  207.         }
  208.     }
  209.  
  210.     @Action(value = "Zone",
  211.             results = {
  212.                 @Result(name=ActionSupport.SUCCESS, location="Zone.jsp"),
  213.                 @Result(name = ActionSupport.INPUT, location = "Zone.jsp")},
  214.             interceptorRefs={@InterceptorRef(value="modelParamsPrepareParamsStack", params={"params.acceptParamNames", "id, currentPage, rowCount, totalPages, message, status", "validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
  215.     public String load() throws Exception
  216.     {
  217.         return ActionSupport.SUCCESS;
  218.     }
  219.  
  220.     @Action(value = "ZonePage",
  221.         results = {@Result(name=ActionSupport.SUCCESS, location="Zone.jsp", params={"namespace", "/admin_side", "actionName", "Zone", "currentPage", "${currentPage}"}),
  222.         @Result(name = ActionSupport.INPUT, location = "Zone.jsp")},
  223.         interceptorRefs={@InterceptorRef(value="modelParamsPrepareParamsStack", params={"params.acceptParamNames", "currentPage", "validation.validateAnnotatedMethodOnly", "true"})})
  224.     public String page()
  225.     {
  226.         return ActionSupport.SUCCESS;
  227.     }
  228.  
  229.     @Validations(
  230.             requiredFields={@RequiredFieldValidator(fieldName="selectedItem", type= ValidatorType.FIELD, key = "transporter.required")},
  231.             requiredStrings={
  232.                 @RequiredStringValidator(fieldName="zoneName", type= ValidatorType.FIELD, key = "zone.name.required")},
  233.             stringLengthFields={
  234.                 @StringLengthFieldValidator(fieldName="zoneName", type= ValidatorType.FIELD, minLength="2", maxLength="45", key="transporter.name.length", messageParams={"2", "45"})})
  235.     @Action(value = "AddZone",
  236.         results = {@Result(name=ActionSupport.SUCCESS, type="redirectAction", location="Zone.jsp", params={"namespace", "/admin_side", "actionName", "Zone", "currentPage", "${currentPage}", "message", "${message}", "id", "${id}", "status", "${status}"}),
  237.         @Result(name = ActionSupport.INPUT, location = "Zone.jsp")},
  238.         interceptorRefs={@InterceptorRef(value="modelParamsPrepareParamsStack", params={"params.acceptParamNames", "id, zoneId, zoneName, (\\[1\\]\\.)*transporterId, currentPage, rowCount, totalPages, status", "validation.validateAnnotatedMethodOnly", "true"})})
  239.     public String insert()
  240.     {
  241.         System.out.println(transporter.getTransporterId()+" : "+transporter.getTransporterName()+" : "+transporter.getTransporterWebsite());
  242.         if(entity.getTransporterId()==null)
  243.         {
  244.             setId(null);
  245.             setStatus(1);
  246.             setCurrentPage(1L);
  247.             /*if(zoneService.insert(entity.getZoneName(), entity.getTransporterWebsite()))
  248.             {
  249.                 setStatus(1);
  250.                 setMessage(getText("insert.success"));
  251.             }
  252.             else
  253.             {
  254.                 setStatus(-1);
  255.                 setMessage(getText("insert.failed"));
  256.             }*/
  257.         }
  258.         else
  259.         {
  260.             if(zoneService.update(entity))
  261.             {
  262.                 setStatus(1);
  263.                 setMessage(getText("update.success", new String[]{String.valueOf(entity.getTransporterId())}));
  264.             }
  265.             else
  266.             {
  267.                 setStatus(-1);
  268.                 setMessage(getText("update.failed", new String[]{String.valueOf(entity.getTransporterId())}));
  269.             }
  270.         }
  271.         return ActionSupport.SUCCESS;
  272.     }
  273.  
  274.     @Validations(
  275.             requiredFields={
  276.                 @RequiredFieldValidator(fieldName="zoneId", type= ValidatorType.FIELD, key = "edit.warning"),
  277.                 @RequiredFieldValidator(fieldName="selectedItem", type= ValidatorType.FIELD, key = "dropbox.required"),
  278.                 @RequiredFieldValidator(fieldName="id", type= ValidatorType.FIELD, key = "delete.id.missing")})
  279.     @Action(value = "EditZone",
  280.             results = {
  281.                 @Result(name=ActionSupport.SUCCESS, location="Zone.jsp"),
  282.                 @Result(name = ActionSupport.INPUT, location = "Zone.jsp")},
  283.             interceptorRefs={
  284.                 @InterceptorRef(value="modelParamsPrepareParamsStack", params={"params.acceptParamNames", "id, selectedItem, zoneId, zoneName, currentPage", "validation.validateAnnotatedMethodOnly", "true"})})
  285.     public String edit()
  286.     {
  287.         return ActionSupport.SUCCESS;
  288.     }
  289.  
  290.     @Validations(
  291.             requiredFields={
  292.                 @RequiredFieldValidator(fieldName="deleteOneRow", type= ValidatorType.FIELD, shortCircuit=true, key="delete.row.reject"),
  293.                 @RequiredFieldValidator(fieldName="deleteId", type= ValidatorType.FIELD, shortCircuit=true, key="delete.id.missing")},
  294.             fieldExpressions={@FieldExpressionValidator(fieldName="deleteOneRow", expression="deleteOneRow==true", shortCircuit=true, key="delete.row.reject")})
  295.     @Action(value = "DeleteTransporter",
  296.             results = {
  297.                 @Result(name=ActionSupport.SUCCESS, type="redirectAction", location="Transporter.action", params={"currentPage", "${currentPage}", "message", "${message}", "status", "${status}"}),
  298.                 @Result(name = ActionSupport.INPUT, location = "Zone.jsp")},
  299.             interceptorRefs={
  300.                 @InterceptorRef(value="modelParamsPrepareParamsStack", params={"params.acceptParamNames", "deleteId, deleteOneRow, currentPage, status", "validation.validateAnnotatedMethodOnly", "true"})})
  301.     public String deleteSingleRow()
  302.     {
  303.         if(zoneService.delete(deleteId))
  304.         {
  305.             rowCount--;
  306.             currentPage=Paging.delete(rowCount, pageSize, currentPage);
  307.             setMessage(getText("delete.success", new String[]{String.valueOf(deleteId)}));
  308.             setStatus(1);
  309.         }
  310.         else
  311.         {
  312.             setStatus(-1);
  313.             setMessage(getText("delete.failed", new String[]{String.valueOf(deleteId)}));
  314.         }
  315.         return ActionSupport.SUCCESS;
  316.     }
  317.  
  318.     @Validations(
  319.             requiredFields={@RequiredFieldValidator(type= ValidatorType.FIELD, fieldName="chk", key="delete.multiple.alert"),
  320.             @RequiredFieldValidator(type= ValidatorType.FIELD, fieldName="deleteMultipleRows", key="delete.row.reject")},
  321.             fieldExpressions={
  322.                 @FieldExpressionValidator(fieldName="deleteMultipleRows", expression="deleteMultipleRows==true", shortCircuit=true, key="delete.row.confirm")})
  323.     @Action(value = "DeleteZones",
  324.             results = {
  325.                 @Result(name=ActionSupport.SUCCESS, type="redirectAction", location="Zone.jsp", params={"namespace", "/admin_side", "actionName", "Transporter", "currentPage", "${currentPage}", "message", "${message}", "status", "${status}"}),
  326.                 @Result(name = ActionSupport.INPUT, location = "Zone.jsp")},
  327.             interceptorRefs={
  328.                 @InterceptorRef(value="modelParamsPrepareParamsStack", params={"params.acceptParamNames", "chk, currentPage, rowCount, totalPages", "validation.validateAnnotatedMethodOnly", "true"})})
  329.     public String deleteMultipleRows()
  330.     {
  331.         if(zoneService.delete(chk))
  332.         {
  333.             int size=chk.size();
  334.             rowCount-=size;
  335.             currentPage=Paging.delete(rowCount, pageSize, currentPage);
  336.             setMessage(getText("delete.multiple.success", new String[]{String.valueOf(size)}));
  337.             setStatus(1);
  338.         }
  339.         else
  340.         {
  341.             setMessage(getText("delete.multiple.failed"));
  342.             setStatus(-1);
  343.         }
  344.         return ActionSupport.SUCCESS;
  345.     }
  346.  
  347.     @Override
  348.     public ZoneTable getModel()
  349.     {
  350.         return entity;
  351.     }
  352.  
  353.     public List<ZoneTable> getZones()
  354.     {
  355.         return zones;
  356.     }
  357.  
  358.     @Override
  359.     public void validate()
  360.     {
  361.         if(hasErrors())
  362.         {
  363.             if(entity.getTransporterId()==null&&id!=null&&zoneService.checkId(id))
  364.             {
  365.                 long currentRow=zoneService.getCurrentRow(id);
  366.                 long page=currentRow%pageSize;
  367.                 page=page==0?currentRow-pageSize:currentRow-currentRow%pageSize;
  368.                 zones= zoneService.getList((int)page, pageSize, null, null);
  369.                 currentPage=new BigDecimal(currentRow).divide(new BigDecimal(pageSize), 0, BigDecimal.ROUND_CEILING).longValue();
  370.             }
  371.             else
  372.             {
  373.                 zones= zoneService.getList((int)(currentPage-1)*pageSize, pageSize, null, null);
  374.             }
  375.             paginate();
  376.             //transporters=sharableService.getTransporterList();
  377.         }
  378.     }
  379.  
  380.     @Override
  381.     public void prepare() throws Exception
  382.     {
  383.         String actionName = ActionContext.getContext().getName();
  384.         if(actionName.equals("Zone")||actionName.equals("EditZone")||actionName.equals("ZonePage"))
  385.         {
  386.             if(entity.getTransporterId()==null&&id!=null&&zoneService.checkId(id))
  387.             {
  388.                 long currentRow=zoneService.getCurrentRow(id);
  389.                 long page=currentRow%pageSize;
  390.                 page=page==0?currentRow-pageSize:currentRow-currentRow%pageSize;
  391.                 zones= zoneService.getList((int)page, pageSize, null, null);
  392.                 currentPage=new BigDecimal(currentRow).divide(new BigDecimal(pageSize), 0, BigDecimal.ROUND_CEILING).longValue();
  393.             }
  394.             else
  395.             {
  396.                 zones= zoneService.getList((int)(currentPage-1)*pageSize, pageSize, null, null);
  397.             }
  398.             paginate();
  399.             //transporters=sharableService.getTransporterList();
  400.         }
  401.     }
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement