1 package org.demo; 2 import java.util.Collection ; 3 import javax.annotation.Resource; 4 import javax.faces.application.FacesMessage; 5 import javax.faces.context.FacesContext; 6 import javax.persistence.EntityManager; 7 import javax.persistence.EntityManagerFactory; 8 import javax.persistence.PersistenceUnit; 9 import javax.persistence.Query; 10 import javax.transaction.UserTransaction ; 11 12 public class DiscountCodeController { 13 14 private DiscountCode discountCode; 15 16 @Resource 17 private UserTransaction utx; 18 19 @PersistenceUnit(unitName = "SjsasJSFTestPU") 20 private EntityManagerFactory emf; 21 22 private EntityManager getEntityManager() { 23 return emf.createEntityManager(); 24 } 25 26 private int batchSize = 20; 27 28 private int firstItem = 0; 29 30 public String createSetup() { 31 this.discountCode = new DiscountCode(); 32 return "discountCode_create"; 33 } 34 35 public String create() { 36 EntityManager em = getEntityManager(); 37 try { 38 utx.begin(); 39 em.persist(discountCode); 40 utx.commit(); 41 addSuccessMessage("DiscountCode was successfully created."); 42 } catch (Exception ex) { 43 try { 44 addErrorMessage(ex.getLocalizedMessage()); 45 utx.rollback(); 46 } catch (Exception e) { 47 addErrorMessage(e.getLocalizedMessage()); 48 } 49 } 50 em.close(); 51 return "discountCode_list"; 52 } 53 54 public String detailSetup() { 55 setDiscountCodeFromRequestParam(); 56 return "discountCode_detail"; 57 } 58 59 public String editSetup() { 60 setDiscountCodeFromRequestParam(); 61 return "discountCode_edit"; 62 } 63 64 public String edit() { 65 EntityManager em = getEntityManager(); 66 try { 67 utx.begin(); 68 discountCode = em.merge(discountCode); 69 utx.commit(); 70 addSuccessMessage("DiscountCode was successfully updated."); 71 } catch (Exception ex) { 72 try { 73 addErrorMessage(ex.getLocalizedMessage()); 74 utx.rollback(); 75 } catch (Exception e) { 76 addErrorMessage(e.getLocalizedMessage()); 77 } 78 } 79 em.close(); 80 return "discountCode_list"; 81 } 82 83 public String destroy() { 84 EntityManager em = getEntityManager(); 85 try { 86 utx.begin(); 87 DiscountCode discountCode = getDiscountCodeFromRequestParam(); 88 discountCode = em.merge(discountCode); 89 em.remove(discountCode); 90 utx.commit(); 91 addSuccessMessage("DiscountCode was successfully deleted."); 92 } catch (Exception ex) { 93 try { 94 addErrorMessage(ex.getLocalizedMessage()); 95 utx.rollback(); 96 } catch (Exception e) { 97 addErrorMessage(e.getLocalizedMessage()); 98 } 99 } 100 em.close(); 101 return "discountCode_list"; 102 } 103 104 public DiscountCode getDiscountCodeFromRequestParam() { 105 EntityManager em = getEntityManager(); 106 String param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("discountCode"); 107 String id = param; 108 DiscountCode o = em.find(DiscountCode.class, id); 109 em.close(); 110 return o; 111 } 112 113 public void setDiscountCodeFromRequestParam() { 114 DiscountCode discountCode = getDiscountCodeFromRequestParam(); 115 setDiscountCode(discountCode); 116 } 117 118 public Collection getDiscountCodes() { 119 EntityManager em = getEntityManager(); 120 Query q = em.createQuery("select object(o) from DiscountCode as o"); 121 q.setMaxResults(batchSize); 122 q.setFirstResult(firstItem); 123 Collection c = q.getResultList(); 124 em.close(); 125 return c; 126 } 127 128 public DiscountCode getDiscountCode() { 129 return discountCode; 130 } 131 132 public void setDiscountCode(DiscountCode discountCode) { 133 this.discountCode = discountCode; 134 } 135 136 public static void addErrorMessage(String msg) { 137 FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg); 138 FacesContext fc = FacesContext.getCurrentInstance(); 139 fc.addMessage(null, facesMsg); 140 } 141 142 public static void addSuccessMessage(String msg) { 143 FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg); 144 FacesContext fc = FacesContext.getCurrentInstance(); 145 fc.addMessage("successInfo", facesMsg); 146 } 147 148 public DiscountCode findDiscountCode(String id) { 149 EntityManager em = getEntityManager(); 150 DiscountCode o = (DiscountCode) em.find(DiscountCode.class, id); 151 em.close(); 152 return o; 153 } 154 155 public int getItemCount() { 156 EntityManager em = getEntityManager(); 157 int count = ((Long ) em.createQuery("select count(o) from DiscountCode as o").getSingleResult()).intValue();em.close(); 158 return count; 159 } 160 161 public int getFirstItem() { 162 return firstItem; 163 } 164 165 public int getLastItem() { 166 int size = getItemCount(); 167 return firstItem + batchSize > size ? size : firstItem + batchSize; 168 } 169 170 public int getBatchSize() { 171 return batchSize; 172 } 173 174 public String next() { 175 if (firstItem + batchSize < getItemCount()) { 176 firstItem += batchSize; 177 } 178 return "discountCode_list"; 179 } 180 181 public String prev() { 182 firstItem -= batchSize; 183 if (firstItem < 0) { 184 firstItem = 0; 185 } 186 return "discountCode_list"; 187 } 188 } 189 | Popular Tags |