1 16 17 18 41 package com.opensourcestrategies.crmsfa.util; 42 43 import java.util.Map ; 44 import java.util.List ; 45 import java.util.Locale ; 46 import java.util.Calendar ; 47 import java.util.Iterator ; 48 import java.sql.Timestamp ; 49 import java.text.DateFormat ; 50 import java.text.SimpleDateFormat ; 51 import java.text.ParseException ; 52 import javax.servlet.http.HttpServletRequest ; 53 import javax.servlet.http.HttpServletResponse ; 54 import javax.servlet.http.HttpSession ; 55 56 import javax.servlet.http.HttpServletRequest ; 57 import javax.servlet.http.HttpServletResponse ; 58 59 import org.ofbiz.base.util.Debug; 60 import org.ofbiz.base.util.UtilFormatOut; 61 import org.ofbiz.base.util.UtilValidate; 62 import org.ofbiz.base.util.UtilHttp; 63 import org.ofbiz.base.util.UtilMisc; 64 import org.ofbiz.base.util.UtilNumber; 65 import org.ofbiz.base.util.UtilDateTime; 66 import org.ofbiz.base.util.UtilProperties; 67 import org.ofbiz.base.util.collections.ResourceBundleMapWrapper; 68 69 import org.ofbiz.entity.GenericDelegator; 70 import org.ofbiz.entity.GenericEntityException; 71 import org.ofbiz.entity.GenericValue; 72 import org.ofbiz.service.DispatchContext; 73 import org.ofbiz.service.GenericServiceException; 74 import org.ofbiz.service.LocalDispatcher; 75 import org.ofbiz.service.ServiceUtil; 76 import org.ofbiz.service.ModelService; 77 import org.ofbiz.security.Security; 78 79 import org.ofbiz.securityext.login.LoginServices; 80 import org.ofbiz.party.contact.ContactHelper; 81 82 88 public class UtilCommon { 89 90 public static final String module = UtilCommon.class.getName(); 91 92 93 94 95 96 97 98 101 public static Map createAndLogServiceError(String uiLabel, Locale locale, String _module) { 102 ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); 103 String errorMsg = (String ) uiLabelMap.get(uiLabel); 104 Debug.logError(null, errorMsg, _module); 105 return ServiceUtil.returnError(errorMsg); 106 } 107 108 111 public static Map createAndLogServiceError(String message, String uiLabel, Locale locale, String _module) { 112 ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); 113 String errorMsg = (String ) uiLabelMap.get(uiLabel) + " " + message; 114 Debug.logError(null, errorMsg, _module); 115 return ServiceUtil.returnError(errorMsg); 116 } 117 118 121 public static Map createAndLogServiceError(Map serviceResult, String uiLabel, Locale locale, String _module) { 122 ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); 123 String errorMsg = (String ) uiLabelMap.get(uiLabel) + " " + ServiceUtil.getErrorMessage(serviceResult); 124 Debug.logError(null, errorMsg, _module); 125 return ServiceUtil.returnError(errorMsg); 126 } 127 128 131 public static Map createAndLogServiceError(Exception e, String uiLabel, Locale locale, String _module) { 132 ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); 133 String errorMsg = (String ) uiLabelMap.get(uiLabel) + " " + e.getMessage(); 134 Debug.logError(e, errorMsg, _module); 135 return ServiceUtil.returnError(errorMsg); 136 } 137 138 139 140 141 142 143 144 152 public static Timestamp getEndTimestamp(Timestamp start, String duration, Locale locale) throws IllegalArgumentException { 153 154 if (duration == null || duration.length() == 0) return start; 156 157 Calendar cal = Calendar.getInstance(); 158 159 DateFormat df = new SimpleDateFormat ("HH:mm", locale); 161 try { 162 cal.setTime(df.parse(duration)); 163 } catch (ParseException e) { 164 throw new IllegalArgumentException ("Duration input must be in HH:mm format."); 165 } 166 167 int days = cal.get(Calendar.DAY_OF_YEAR) - 1; 169 int hours = cal.get(Calendar.HOUR_OF_DAY); 170 int minutes = cal.get(Calendar.MINUTE); 171 172 cal.setTime(start); 174 cal.set(Calendar.DAY_OF_YEAR, days + cal.get(Calendar.DAY_OF_YEAR)); 175 cal.set(Calendar.HOUR_OF_DAY, hours + cal.get(Calendar.HOUR_OF_DAY)); 176 cal.set(Calendar.MINUTE, minutes + cal.get(Calendar.MINUTE)); 177 178 Timestamp end = new Timestamp (cal.getTimeInMillis()); 180 181 if (end.before(start)) throw new IllegalArgumentException ("Cannot set a negative duration."); 183 184 return end; 186 } 187 188 191 public static String getDuration(Timestamp start, Timestamp end) { 192 Calendar cal = Calendar.getInstance(); 193 194 cal.setTime(UtilDateTime.getDayStart(start)); 196 197 long duration = end.getTime() - start.getTime(); 199 200 int hours = (int) (duration / 1000 / 60 / 60); 202 203 int minutes = (int) ((duration / 1000 / 60) % 60); 205 206 return hours + ":" + (minutes < 10 ? "0" : "") + minutes; 207 } 208 209 214 public static boolean isTimePeriodOpen(String customTimePeriodId, GenericDelegator delegator) throws GenericEntityException { 215 216 GenericValue timePeriod = delegator.findByPrimaryKeyCache("CustomTimePeriod", UtilMisc.toMap("customTimePeriodId", customTimePeriodId)); 218 if (timePeriod == null) { 219 return false; 220 } 221 if (timePeriod.getString("isClosed") == null) { 222 Debug.logWarning("Time period [" + customTimePeriodId + "] has no isClosed flag set--please set it", module); 223 return false; } else if (timePeriod.getString("isClosed").equals("Y")) { 225 return false; 226 } 227 if (timePeriod.getDate("thruDate").before(UtilDateTime.toDate(UtilDateTime.toDateTimeString(UtilDateTime.nowTimestamp())))) { 228 Debug.logInfo(timePeriod.getDate("thruDate") + " is before " + UtilDateTime.nowTimestamp() + " so time period [" + customTimePeriodId + "] is not open", module ); 229 return false; 230 } 231 232 return true; 233 } 234 235 236 237 238 239 240 241 248 public static String donePageRequestHelper(HttpServletRequest request, HttpServletResponse response) { 249 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 250 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 251 252 String donePage = (String ) request.getParameter("donePage"); 253 if (donePage == null) { 254 donePage = "error"; 255 } 256 return donePage; 257 } 258 259 263 private static String sendFrom = "ofbiztest@yahoo.com"; 264 private static String subject = "CRM password reminder for user "; 265 public static String forgotPassword(HttpServletRequest request, HttpServletResponse response) { 266 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 267 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 268 269 String resource = "SecurityextUiLabels"; 270 String errMsg = null; 271 boolean useEncryption = "true".equals(UtilProperties.getPropertyValue("security.properties", "password.encrypt")); 272 273 String userLoginId = request.getParameter("USERNAME"); 275 if ((userLoginId != null) && ("true".equals(UtilProperties.getPropertyValue("security.properties", "username.lowercase")))) { 276 userLoginId = userLoginId.toLowerCase(); 277 } 278 if (!UtilValidate.isNotEmpty(userLoginId)) { 279 errMsg = UtilProperties.getMessage(resource, "loginevents.username_was_empty_reenter", UtilHttp.getLocale(request)); 281 request.setAttribute("_ERROR_MESSAGE_", errMsg); 282 return "error"; 283 } 284 285 GenericValue supposedUserLogin = null; 287 String passwordToSend = null; 288 try { 289 supposedUserLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", userLoginId)); 290 if (supposedUserLogin == null) { 291 errMsg = UtilProperties.getMessage(resource, "loginevents.username_not_found_reenter", UtilHttp.getLocale(request)); 293 request.setAttribute("_ERROR_MESSAGE_", errMsg); 294 return "error"; 295 } 296 if (useEncryption) { 297 double randNum = Math.random(); 299 300 passwordToSend = "auto" + ((long) (randNum * 100000)); 302 supposedUserLogin.set("currentPassword", LoginServices.getPasswordHash(passwordToSend)); 303 supposedUserLogin.set("passwordHint", "Auto-Generated Password"); 304 } else { 305 passwordToSend = supposedUserLogin.getString("currentPassword"); 306 } 307 } catch (GenericEntityException e) { 308 Debug.logWarning(e, "", module); 309 Map messageMap = UtilMisc.toMap("errorMessage", e.toString()); 310 errMsg = UtilProperties.getMessage(resource, "loginevents.error_accessing_password", messageMap, UtilHttp.getLocale(request)); 311 request.setAttribute("_ERROR_MESSAGE_", errMsg); 312 return "error"; 313 } 314 if (supposedUserLogin == null) { 315 Map messageMap = UtilMisc.toMap("userLoginId", userLoginId); 317 errMsg = UtilProperties.getMessage(resource, "loginevents.user_with_the_username_not_found", messageMap, UtilHttp.getLocale(request)); 318 request.setAttribute("_ERROR_MESSAGE_", errMsg); 319 return "error"; 320 } 321 322 GenericValue party = null; 324 StringBuffer emails = new StringBuffer (); 325 try { 326 party = supposedUserLogin.getRelatedOne("Party"); 327 } catch (GenericEntityException e) { 328 Debug.logWarning(e, "", module); 329 party = null; 330 } 331 if (party != null) { 332 Iterator emailIter = UtilMisc.toIterator(ContactHelper.getContactMechByPurpose(party, "PRIMARY_EMAIL", false)); 333 while (emailIter != null && emailIter.hasNext()) { 334 GenericValue email = (GenericValue) emailIter.next(); 335 emails.append(emails.length() > 0 ? "," : "").append(email.getString("infoString")); 336 } 337 } 338 if (!UtilValidate.isNotEmpty(emails.toString())) { 339 errMsg = UtilProperties.getMessage(resource, "loginevents.no_primary_email_address_set_contact_customer_service", UtilHttp.getLocale(request)); 341 request.setAttribute("_ERROR_MESSAGE_", errMsg); 342 return "error"; 343 } 344 345 StringBuffer body = new StringBuffer (subject + userLoginId + "\n\n"); 347 if (useEncryption) { 348 body.append("A new password was generated for you: ").append(passwordToSend).append("\n\n"); 349 } else { 350 body.append("Your password is: ").append(passwordToSend).append("\n\n"); 351 } 352 body.append("When you log in, please change your password because this email is not secure.\n"); 353 354 Map input = UtilMisc.toMap("subject", subject + userLoginId, "sendFrom", sendFrom, "contentType", "text/plain"); 356 input.put("sendTo", emails.toString()); 357 input.put("body", body.toString()); 358 359 try { 360 Map result = dispatcher.runSync("sendMail", input); 361 362 if (ServiceUtil.isError(result)) { 363 Map messageMap = UtilMisc.toMap("errorMessage", result.get(ModelService.ERROR_MESSAGE)); 364 errMsg = UtilProperties.getMessage(resource, "loginevents.error_unable_email_password_contact_customer_service_errorwas", messageMap, UtilHttp.getLocale(request)); 365 request.setAttribute("_ERROR_MESSAGE_", errMsg); 366 return "error"; 367 } 368 } catch (GenericServiceException e) { 369 Debug.logWarning(e, "", module); 370 errMsg = UtilProperties.getMessage(resource, "loginevents.error_unable_email_password_contact_customer_service", UtilHttp.getLocale(request)); 371 request.setAttribute("_ERROR_MESSAGE_", errMsg); 372 return "error"; 373 } 374 375 if (useEncryption) { 377 try { 378 supposedUserLogin.store(); 379 } catch (GenericEntityException e) { 380 Debug.logWarning(e, "", module); 381 Map messageMap = UtilMisc.toMap("errorMessage", e.toString()); 382 errMsg = UtilProperties.getMessage(resource, "loginevents.error_saving_new_password_email_not_correct_password", messageMap, UtilHttp.getLocale(request)); 383 request.setAttribute("_ERROR_MESSAGE_", errMsg); 384 return "error"; 385 } 386 } 387 388 if (useEncryption) { 389 errMsg = UtilProperties.getMessage(resource, "loginevents.new_password_createdandsent_check_email", UtilHttp.getLocale(request)); 390 request.setAttribute("_EVENT_MESSAGE_", errMsg); 391 } else { 392 errMsg = UtilProperties.getMessage(resource, "loginevents.new_password_sent_check_email", UtilHttp.getLocale(request)); 393 request.setAttribute("_EVENT_MESSAGE_", errMsg); 394 } 395 return "success"; 396 } 397 398 402 public static String toPercent(Number number) { 403 return UtilNumber.toPercentString(number, 404 com.opensourcestrategies.crmsfa.forecasts.UtilForecast.BD_FORECAST_PERCENT_DECIMALS - 2, 405 com.opensourcestrategies.crmsfa.forecasts.UtilForecast.BD_FORECAST_PERCENT_ROUNDING); 406 } 407 } 408 | Popular Tags |