📄 utilcommon.java
字号:
return df.format(new Timestamp(cal.getTimeInMillis())).toString(); } /** * Returns a true if the time period has passed or is closed * @param customTimePeriodId * @return */ public static boolean isTimePeriodOpen(String customTimePeriodId, GenericDelegator delegator) throws GenericEntityException { // first check time period -- is closed or passed? if so, cannot create a forecast GenericValue timePeriod = delegator.findByPrimaryKeyCache("CustomTimePeriod", UtilMisc.toMap("customTimePeriodId", customTimePeriodId)); if (timePeriod == null) { return false; } if (timePeriod.getString("isClosed") == null) { Debug.logWarning("Time period [" + customTimePeriodId + "] has no isClosed flag set--please set it", module); return false; // really sholdn't have this case, so let's return a false } else if (timePeriod.getString("isClosed").equals("Y")) { return false; } if (timePeriod.getDate("thruDate").before(UtilDateTime.toDate(UtilDateTime.toDateTimeString(UtilDateTime.nowTimestamp())))) { Debug.logInfo(timePeriod.getDate("thruDate") + " is before " + UtilDateTime.nowTimestamp() + " so time period [" + customTimePeriodId + "] is not open", module ); return false; } return true; } /************************************************************************/ /** Miscellaneous Methods **/ /************************************************************************/ /** * This method will read the donePage parameter and return it to the controller as the result. * It is called from controller.xml using <event type="java" path="com.opensourcestrategies.crmsfa.util.UtilCommon" invoke="donePageRequestHelper"/> * Then it can be used with <response name="returnValue" .../> to determine what to do next. * * @return The donePage parameter if it exists, otherwise "error" */ public static String donePageRequestHelper(HttpServletRequest request, HttpServletResponse response) { GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); String donePage = (String) request.getParameter("donePage"); if (donePage == null) { donePage = "error"; } return donePage; } /** * Custom forgotPassword service because the OFBiz method is reather complicated. * TODO: config properties for sendFrom, subject, and maybe make ftl for body */ private static String sendFrom = "ofbiztest@yahoo.com"; private static String subject = "CRM password reminder for user "; public static String forgotPassword(HttpServletRequest request, HttpServletResponse response) { GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); String resource = "SecurityextUiLabels"; String errMsg = null; boolean useEncryption = "true".equals(UtilProperties.getPropertyValue("security.properties", "password.encrypt")); // validate the userloginId input String userLoginId = request.getParameter("USERNAME"); if ((userLoginId != null) && ("true".equals(UtilProperties.getPropertyValue("security.properties", "username.lowercase")))) { userLoginId = userLoginId.toLowerCase(); } if (!UtilValidate.isNotEmpty(userLoginId)) { // the password was incomplete errMsg = UtilProperties.getMessage(resource, "loginevents.username_was_empty_reenter", UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } // get the password of the userlogin or generate a new one if there is encryption GenericValue supposedUserLogin = null; String passwordToSend = null; try { supposedUserLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", userLoginId)); if (supposedUserLogin == null) { // the Username was not found errMsg = UtilProperties.getMessage(resource, "loginevents.username_not_found_reenter", UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } if (useEncryption) { // password encrypted, can't send, generate new password and email to user double randNum = Math.random(); // multiply by 100,000 to usually make a 5 digit number passwordToSend = "auto" + ((long) (randNum * 100000)); supposedUserLogin.set("currentPassword", LoginServices.getPasswordHash(passwordToSend)); supposedUserLogin.set("passwordHint", "Auto-Generated Password"); } else { passwordToSend = supposedUserLogin.getString("currentPassword"); } } catch (GenericEntityException e) { Debug.logWarning(e, "", module); Map messageMap = UtilMisc.toMap("errorMessage", e.toString()); errMsg = UtilProperties.getMessage(resource, "loginevents.error_accessing_password", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } if (supposedUserLogin == null) { // the Username was not found Map messageMap = UtilMisc.toMap("userLoginId", userLoginId); errMsg = UtilProperties.getMessage(resource, "loginevents.user_with_the_username_not_found", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } // now find the party and the party's primary email GenericValue party = null; StringBuffer emails = new StringBuffer(); try { party = supposedUserLogin.getRelatedOne("Party"); } catch (GenericEntityException e) { Debug.logWarning(e, "", module); party = null; } if (party != null) { Iterator emailIter = UtilMisc.toIterator(ContactHelper.getContactMechByPurpose(party, "PRIMARY_EMAIL", false)); while (emailIter != null && emailIter.hasNext()) { GenericValue email = (GenericValue) emailIter.next(); emails.append(emails.length() > 0 ? "," : "").append(email.getString("infoString")); } } if (!UtilValidate.isNotEmpty(emails.toString())) { // the Username was not found errMsg = UtilProperties.getMessage(resource, "loginevents.no_primary_email_address_set_contact_customer_service", UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } // construct the body StringBuffer body = new StringBuffer(subject + userLoginId + "\n\n"); if (useEncryption) { body.append("A new password was generated for you: ").append(passwordToSend).append("\n\n"); } else { body.append("Your password is: ").append(passwordToSend).append("\n\n"); } body.append("When you log in, please change your password because this email is not secure.\n"); // send the email Map input = UtilMisc.toMap("subject", subject + userLoginId, "sendFrom", sendFrom, "contentType", "text/plain"); input.put("sendTo", emails.toString()); input.put("body", body.toString()); try { Map result = dispatcher.runSync("sendMail", input); if (ServiceUtil.isError(result)) { Map messageMap = UtilMisc.toMap("errorMessage", result.get(ModelService.ERROR_MESSAGE)); errMsg = UtilProperties.getMessage(resource, "loginevents.error_unable_email_password_contact_customer_service_errorwas", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } } catch (GenericServiceException e) { Debug.logWarning(e, "", module); errMsg = UtilProperties.getMessage(resource, "loginevents.error_unable_email_password_contact_customer_service", UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } // don't save password until after it has been sent if (useEncryption) { try { supposedUserLogin.store(); } catch (GenericEntityException e) { Debug.logWarning(e, "", module); Map messageMap = UtilMisc.toMap("errorMessage", e.toString()); errMsg = UtilProperties.getMessage(resource, "loginevents.error_saving_new_password_email_not_correct_password", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } } if (useEncryption) { errMsg = UtilProperties.getMessage(resource, "loginevents.new_password_createdandsent_check_email", UtilHttp.getLocale(request)); request.setAttribute("_EVENT_MESSAGE_", errMsg); } else { errMsg = UtilProperties.getMessage(resource, "loginevents.new_password_sent_check_email", UtilHttp.getLocale(request)); request.setAttribute("_EVENT_MESSAGE_", errMsg); } return "success"; } /** * Helper method to format a percent string for use in the form widget. * For now, this uses only the forecast percents, but it could be generalized. */ public static String toPercent(Number number) { return UtilNumber.toPercentString(number, com.opensourcestrategies.crmsfa.forecasts.UtilForecast.BD_FORECAST_PERCENT_DECIMALS - 2, com.opensourcestrategies.crmsfa.forecasts.UtilForecast.BD_FORECAST_PERCENT_ROUNDING); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -