1 package org.campware.cream.modules.actions; 2 3 42 43 import java.util.Date ; 44 import org.apache.velocity.context.Context; 45 46 import org.apache.turbine.util.RunData; 47 import org.apache.torque.util.Criteria; 48 import org.apache.torque.util.Transaction; 49 import java.sql.Connection ; 50 51 import org.campware.cream.om.OnlineSubscription; 52 import org.campware.cream.om.OnlineSubscriptionPeer; 53 import org.apache.turbine.util.velocity.VelocityHtmlEmail; 54 import org.apache.turbine.Turbine; 55 56 import org.campware.cream.om.Notification; 57 import org.campware.cream.om.NotificationPeer; 58 59 63 public class OnlineSubscriptionSQL extends CreamAction 64 { 65 protected void initScreen() 66 { 67 setModuleType(DOCUMENT); 68 setModuleName("ONLINE_SUBSCRIPTION"); 69 } 70 71 81 public void doInsert(RunData data, Context context) 82 throws Exception 83 { 84 OnlineSubscription entry = new OnlineSubscription(); 85 data.getParameters().setProperties(entry); 86 int myStatus= data.getParameters().getInt("status"); 87 int notify= data.getParameters().getInt("notify", 10); 88 89 entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate"))); 90 entry.setClosedDate(parseDate(data.getParameters().getString("closeddate"))); 91 entry.setStartDate(parseDate(data.getParameters().getString("startdate"))); 92 entry.setEndDate(parseDate(data.getParameters().getString("enddate"))); 93 entry.setCreatedBy(data.getUser().getName()); 94 entry.setCreated(new Date ()); 95 entry.setModifiedBy(data.getUser().getName()); 96 entry.setModified(new Date ()); 97 98 99 boolean bSave=true; 100 101 if (myStatus!=10 && notify==20){ 102 bSave= sendEmail(data, context, entry); 103 } 104 105 if (bSave){ 106 entry.setOnlineSubsCode(getTempCode()); 107 108 109 Connection conn = Transaction.begin(OnlineSubscriptionPeer.DATABASE_NAME); 110 boolean success = false; 111 try { 112 entry.save(conn); 113 entry.setOnlineSubsCode(getRowCode("OS", entry.getOnlineSubsId())); 114 entry.save(conn); 115 Transaction.commit(conn); 116 success = true; 117 118 } finally { 119 if (!success) Transaction.safeRollback(conn); 120 } 121 } 122 } 123 124 132 public void doUpdate(RunData data, Context context) 133 throws Exception 134 { 135 OnlineSubscription entry = new OnlineSubscription(); 136 data.getParameters().setProperties(entry); 137 138 int myStatus= data.getParameters().getInt("status"); 139 int notify= data.getParameters().getInt("notify", 10); 140 141 entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate"))); 142 entry.setClosedDate(parseDate(data.getParameters().getString("closeddate"))); 143 entry.setStartDate(parseDate(data.getParameters().getString("startdate"))); 144 entry.setEndDate(parseDate(data.getParameters().getString("enddate"))); 145 entry.setCreated(parseDateTime(data.getParameters().getString("created"))); 146 entry.setModifiedBy(data.getUser().getName()); 147 entry.setModified(new Date ()); 148 149 boolean bSave=true; 150 151 if (myStatus!=10 && notify==20){ 152 bSave= sendEmail(data, context, entry); 153 } 154 155 if (bSave){ 156 entry.setModified(true); 157 entry.setNew(false); 158 entry.save(); 159 } 160 } 161 162 165 private boolean sendEmail(RunData data, Context context, OnlineSubscription emailEntry) 166 throws Exception 167 { 168 int notifid; 169 int status= emailEntry.getStatus(); 170 171 if (status==30){ 172 notifid=1001; 173 }else{ 174 notifid=1003; 175 } 176 177 Criteria criteria = new Criteria(); 178 criteria.add(NotificationPeer.NOTIFICATION_ID, new Integer (notifid), Criteria.EQUAL); 179 Notification myNotif = (Notification) NotificationPeer.doSelect(criteria).get(0); 180 VelocityTool velTool= new VelocityTool(context); 181 182 String sEmailAddress=emailEntry.getCustomerRelatedByCustomerId().getEmail(); 183 184 if (sEmailAddress.length()>1){ 185 VelocityHtmlEmail ve = new VelocityHtmlEmail(data); 186 ve.setCharset("UTF-8"); 187 ve.addTo( sEmailAddress, ""); 188 ve.setFrom(Turbine.getConfiguration().getString("mail.smtp.from"), Turbine.getConfiguration().getString("mail.smtp.from.name")); 189 ve.setSubject(myNotif.getSubject()); 190 context.put("name", emailEntry.getCustomerRelatedByCustomerId().getCustomerName1()); 191 context.put("display", emailEntry.getCustomerRelatedByCustomerId().getCustomerDisplay()); 192 context.put("dear", emailEntry.getCustomerRelatedByCustomerId().getDear()); 193 context.put("email", emailEntry.getCustomerRelatedByCustomerId().getEmail()); 194 context.put("custom1", emailEntry.getCustomerRelatedByCustomerId().getCustom1()); 195 context.put("custom2", emailEntry.getCustomerRelatedByCustomerId().getCustom2()); 196 context.put("custom3", emailEntry.getCustomerRelatedByCustomerId().getCustom3()); 197 context.put("custom4", emailEntry.getCustomerRelatedByCustomerId().getCustom4()); 198 context.put("custom5", emailEntry.getCustomerRelatedByCustomerId().getCustom5()); 199 context.put("custom6", emailEntry.getCustomerRelatedByCustomerId().getCustom6()); 200 context.put("productdisplay", emailEntry.getProduct().getProductDisplay()); 201 context.put("productdescription", emailEntry.getProduct().getProductDescription()); 202 context.put("startdate", formatDate(emailEntry.getStartDate())); 203 context.put("enddate", formatDate(emailEntry.getEndDate())); 204 205 context.put("emailbody", velTool.evaluate(myNotif.getBody())); 206 ve.setTextTemplate("screens/SendEmail.vm"); 207 ve.send(); 208 209 return true; 210 211 }else{ 212 data.setMessage("Sorry, this customer has empty email address!"); 213 data.setScreenTemplate("CreamError.vm"); 214 return false; 215 } 216 } 217 218 219 220 224 public void doDelete(RunData data, Context context) 225 throws Exception 226 { 227 Criteria criteria = new Criteria(); 228 criteria.add(OnlineSubscriptionPeer.ONLINE_SUBS_ID, data.getParameters().getInt("onlinesubsid")); 229 OnlineSubscriptionPeer.doDelete(criteria); 230 } 231 232 236 public void doDeleteselected(RunData data, Context context) 237 throws Exception 238 { 239 int[] delIds= data.getParameters().getInts("rowid"); 240 Criteria criteria = new Criteria(); 241 criteria.addIn(OnlineSubscriptionPeer.ONLINE_SUBS_ID, delIds); 242 OnlineSubscriptionPeer.doDelete(criteria); 243 } 244 245 } 246 | Popular Tags |