📄 utilcommon.java
字号:
/* * Copyright (C) 2006 Open Source Strategies, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *//* Copyright (c) 2005-2006 Open Source Strategies, Inc. *//* * $Id:$ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package com.opensourcestrategies.crmsfa.util;import java.util.Map;import java.util.List;import java.util.Locale;import java.util.Calendar;import java.util.Iterator;import java.sql.Timestamp;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.text.ParseException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilFormatOut;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilHttp;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilNumber;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.collections.ResourceBundleMapWrapper;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;import org.ofbiz.service.ModelService;import org.ofbiz.security.Security;import org.ofbiz.securityext.login.LoginServices;import org.ofbiz.party.contact.ContactHelper;/** * UtilCommon - A place for common crmsfa helper methods * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> * @version $Rev: 12 $ */public class UtilCommon { public static final String module = UtilCommon.class.getName(); /************************************************************************/ /** Logging Methods **/ /************************************************************************/ /** * Log only a uiLabel to the log file and return the label as a user error. */ public static Map createAndLogServiceError(String uiLabel, Locale locale, String _module) { ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); String errorMsg = (String) uiLabelMap.get(uiLabel); Debug.logError(null, errorMsg, _module); return ServiceUtil.returnError(errorMsg); } /** * Log a uiLabel + message to the log file and return it to the user. */ public static Map createAndLogServiceError(String message, String uiLabel, Locale locale, String _module) { ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); String errorMsg = (String) uiLabelMap.get(uiLabel) + " " + message; Debug.logError(null, errorMsg, _module); return ServiceUtil.returnError(errorMsg); } /** * Log the uiLabel + serviceResult error to the log file and return it to the user. */ public static Map createAndLogServiceError(Map serviceResult, String uiLabel, Locale locale, String _module) { ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); String errorMsg = (String) uiLabelMap.get(uiLabel) + " " + ServiceUtil.getErrorMessage(serviceResult); Debug.logError(null, errorMsg, _module); return ServiceUtil.returnError(errorMsg); } /** * Log the uiLabel + exception message to the log file and return it to the user. */ public static Map createAndLogServiceError(Exception e, String uiLabel, Locale locale, String _module) { ResourceBundleMapWrapper uiLabelMap = (ResourceBundleMapWrapper) UtilProperties.getResourceBundleMap("CRMSFAUiLabels", locale); String errorMsg = (String) uiLabelMap.get(uiLabel) + " " + e.getMessage(); Debug.logError(e, errorMsg, _module); return ServiceUtil.returnError(errorMsg); } /************************************************************************/ /** Time Methods **/ /************************************************************************/ /** * This method takes the date/time/duration form input and transforms it into an end timestamp. * It uses Java Date formatting capabilities to transform the duration input into an interval. * * @param start Full date, hour, minute and second of the starting time * @param duration The user input for hour such as "1:00" * @throws IllegalArgumentException If the duration input is unparseable or negative */ public static Timestamp getEndTimestamp(Timestamp start, String duration, Locale locale) throws IllegalArgumentException { // return the start timestamp if no duration specified (i.e., duration = 0) if (duration == null || duration.length() == 0) return start; Calendar cal = Calendar.getInstance(); // Turn the duraiton into a date and time with the hour being the duration DateFormat df = new SimpleDateFormat("HH:mm", locale); try { cal.setTime(df.parse(duration)); } catch (ParseException e) { throw new IllegalArgumentException("Duration input must be in HH:mm format."); } // extract the hours and minutes int hours = cal.get(Calendar.HOUR_OF_DAY); int minutes = cal.get(Calendar.MINUTE); // set to the start time and add the hours and minutes cal.setTime(start); cal.set(Calendar.HOUR_OF_DAY, hours + cal.get(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, minutes + cal.get(Calendar.MINUTE)); // create the end timestamp Timestamp end = new Timestamp(cal.getTimeInMillis()); // make sure it's after the start timestamp if (end.before(start)) throw new IllegalArgumentException("Cannot set a negative duration."); // return our result as a Timestamp return end; } /** * Get the duration between two timestamps in a locale-sensitive format, such as HH:mm. */ public static String getDuration(Timestamp start, Timestamp end) { Calendar cal = Calendar.getInstance(); // set the time to the beginning of the day cal.setTime(UtilDateTime.getDayStart(start)); // the duration in milliseconds long duration = end.getTime() - start.getTime(); // add this difference to the calendar (net effect is the duration is the time since the day began) cal.set(Calendar.MILLISECOND, (int) duration); // format the duration in hours DateFormat df = new SimpleDateFormat("HH:mm");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -