// todo: Evaluate the actual needs to keep this override. @SuppressWarnings("rawtypes") @Override public FieldPanel<Date> setNewModel(final ListItem item) { final FastDateFormat formatter = datePattern == null ? FastDateFormat.getInstance( SyncopeConstants.DEFAULT_DATE_PATTERN, SyncopeConsoleSession.get().getLocale()) : FastDateFormat.getInstance(datePattern, SyncopeConsoleSession.get().getLocale()); IModel<Date> model = new Model<Date>() { private static final long serialVersionUID = 6799404673615637845L; @Override public Date getObject() { Date date = null; final Object obj = item.getModelObject(); if (obj != null && !obj.toString().isEmpty()) { if (obj instanceof String) { // Parse string using datePattern try { date = formatter.parse(obj.toString()); } catch (ParseException e) { LOG.error("While parsing date", e); } } else if (obj instanceof Date) { // Don't parse anything date = (Date) obj; } else { // consider Long date = new Date((Long) obj); } } return date; } @Override @SuppressWarnings("unchecked") public void setObject(final Date object) { item.setModelObject(object != null ? formatter.format(object) : null); } }; field.setModel(model); return this; }
/** * Transforms a {@link String} to a {@link Date} * * @author Esteban Robles Luna */ public class StringToDate extends AbstractTransformer { protected FastDateFormat format = FastDateFormat.getInstance("dd/MM/yyyy"); @Override protected Object primTransform(Object anObject) throws Exception { return format.parse((String) anObject); } }
@Override protected Object primTransform(Object anObject) throws Exception { return format.parse((String) anObject); }
/** * Contains constants naming the Lucene index fields used by this Plugin and some helper methods for * proper handling of special field values like dates. * * @version $Id: c5240610a807da9be47eebfeb002872449773cb6 $ */ public abstract class IndexFields { /** * Keyword field, holds a string uniquely identifying a document across the index. this is used * for finding old versions of a document to be indexed. */ public static final String DOCUMENT_ID = "_docid"; /** Keyword field, holds the name of the virtual wiki a document belongs to */ public static final String DOCUMENT_WIKI = "wiki"; /** Title of the document */ public static final String DOCUMENT_TITLE = "title"; /** Name of the document */ public static final String DOCUMENT_NAME = "name"; /** Name of the web the document belongs to */ @Deprecated public static final String DOCUMENT_WEB = "web"; /** Name of the space the document belongs to */ public static final String DOCUMENT_SPACE = "space"; /** FullName of the document (example : Main.WebHome) */ public static final String DOCUMENT_FULLNAME = "fullname"; /** Version of the document */ public static final String DOCUMENT_VERSION = "version"; /** Language of the document */ public static final String DOCUMENT_LANGUAGE = "lang"; /** * Type of a document, "attachment", "wikipage" or "objects", used to control presentation of * searchresults. See {@link SearchResult}and xdocs/searchResult.vm. */ public static final String DOCUMENT_TYPE = "type"; /** Filename, only used for attachments */ public static final String FILENAME = "filename"; /** XWiki object type, only used for objects */ public static final String OBJECT = "object"; /** Last modifier */ public static final String DOCUMENT_AUTHOR = "author"; /** Creator of the document */ public static final String DOCUMENT_CREATOR = "creator"; /** Date of last modification */ public static final String DOCUMENT_DATE = "date"; /** Date of creation */ public static final String DOCUMENT_CREATIONDATE = "creationdate"; /** Document hidden flag. */ public static final String DOCUMENT_HIDDEN = "hidden"; /** Fulltext content, not stored (and can therefore not be restored from the index). */ public static final String FULLTEXT = "ft"; /** not in use */ public static final String KEYWORDS = "kw"; /** * Format for date storage in the index, and therefore the format which has to be used for * date-queries. */ public static final String DATE_FORMAT = "yyyyMMddHHmm"; private static final FastDateFormat DF = FastDateFormat.getInstance(IndexFields.DATE_FORMAT); public static final String dateToString(Date date) { return DF.format(date); } public static final Date stringToDate(String dateValue) { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); try { return sdf.parse(dateValue); } catch (Exception e) { // silently ignore } return null; } public static final boolean stringToBoolean(String booleanValue) { return Boolean.parseBoolean(booleanValue); } private IndexFields() {} }
public static final String dateToString(Date date) { return DF.format(date); }
static { TimeZone gmt = TimeZone.getTimeZone("GMT"); TIME_FORMAT_DATE = FastDateFormat.getInstance("yyyy-MM-dd", gmt); TIME_FORMAT_TIME = FastDateFormat.getInstance("hh:mm:ss", gmt); TIME_FORMAT_TIMESTAMP = FastDateFormat.getInstance("yyyy-MM-dd hh:mm:ss", gmt); }
protected Object convert(CsvFieldType fieldType, String string) { if (fieldType == null) { return string; } switch (fieldType) { case BOOLEAN: if (string.length() == 0) { return null; } return Boolean.parseBoolean(string); case BYTE: if (string.length() == 0) { return null; } return Byte.parseByte(string); case SHORT: if (string.length() == 0) { return null; } return Short.parseShort(string); case INT: if (string.length() == 0) { return null; } return Integer.parseInt(string); case LONG: if (string.length() == 0) { return null; } return Long.parseLong(string); case FLOAT: if (string.length() == 0) { return null; } return Float.parseFloat(string); case DOUBLE: if (string.length() == 0) { return null; } return Double.parseDouble(string); case DATE: if (string.length() == 0) { return null; } try { Date date = TIME_FORMAT_DATE.parse(string); return new java.sql.Date(date.getTime()); } catch (ParseException e) { return null; } case TIME: if (string.length() == 0) { return null; } try { Date date = TIME_FORMAT_TIME.parse(string); return new java.sql.Time(date.getTime()); } catch (ParseException e) { return null; } case TIMESTAMP: if (string.length() == 0) { return null; } try { Date date = TIME_FORMAT_TIMESTAMP.parse(string); return new java.sql.Timestamp(date.getTime()); } catch (ParseException e) { return null; } case STRING: default: return string; } }
/** * Record and output test-suite & test-case results. * * <p>It expected that this is parsed by Jenkins. * * @see <a * href="https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/tasks/junit/SuiteResult.java">Jenkins * SuiteResult class.</a> * @see <a * href="https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/tasks/junit/CaseResult.java">Jenkins * CaseResult class.</a> */ public final class JUnitResult { /** level info. */ public static String INFO = "INFO"; /** level error. */ public static String ERROR = "ERROR"; private static final FastDateFormat DATE_TIME_FORMAT = FastDateFormat.getInstance("[yyyy-MM-dd HH:mm:ss.SSS] "); private static JAXBContext context = initContext(); private static final Map<Object, TestResult> map = new ConcurrentHashMap<Object, TestResult>(); private static String resultDir = null; private static PrintStream ps = null; private static JAXBContext initContext() { try { return JAXBContext.newInstance(ObjectFactory.class); } catch (JAXBException e) { throw new RuntimeException(e); } } /** * Set directory for storing results. * * @param dir directory. */ public static void setResultDir(String dir) { resultDir = dir; } /** * Set PrintStream instance. * * @param ps PrintStream instance. */ public static void setPrintStream(PrintStream ps) { JUnitResult.ps = ps; } /** * Start test-suite. * * @param testSuite test-suite instance. */ public static void startTestSuite(ITestSuite testSuite) { map.put(testSuite, factory.createTestSuiteResult(testSuite.getName())); } /** * End test-suite. * * @param testSuite test-suite instatnce. */ public static void endTestSuite(ITestSuite testSuite) { TestSuiteResult suiteResult = (TestSuiteResult) map.remove(testSuite); suiteResult.endTestSuite(); if (resultDir == null) return; try { Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); File file = new File(resultDir, "TEST-" + suiteResult.getName() + ".xml"); marshaller.marshal(suiteResult, file); } catch (JAXBException e) { throw new RuntimeException(e); } } /** * Add property in test-suite. * * @param testSuite test-suite instatnce. * @param name property name. * @param value property value. */ public static void addProperty(ITestSuite testSuite, String name, String value) { TestSuiteResult suiteResult = (TestSuiteResult) map.get(testSuite); suiteResult.addProperty(name, value); } /** * Start test-case. * * @param testSuite test-suite instance. * @param testCase test-case instance. */ public static void startTestCase(ITestSuite testSuite, ITestCase testCase) { TestCaseResult caseResult = factory.createTestCaseResult(testCase.getName()); map.put(testCase, caseResult); if (testSuite != null) { TestSuiteResult suiteResult = (TestSuiteResult) map.get(testSuite); suiteResult.addTestCaseResult(caseResult); } } /** * End test-case. * * @param testCase test-case instance. */ public static void endTestCase(ITestCase testCase) { TestCaseResult caseResult = (TestCaseResult) map.remove(testCase); caseResult.endTestCase(); } /** * Set success. * * @param testCase test-case instance. */ public static void setSuccess(ITestCase testCase) { TestCaseResult caseResult = (TestCaseResult) map.get(testCase); caseResult.setSuccess(); } /** * Set error in test-case. * * @param testCase test-case instance. * @param message error message. * @param trace error trace. */ public static void setError(ITestCase testCase, String message, String trace) { TestCaseResult caseResult = (TestCaseResult) map.get(testCase); caseResult.setError(message, trace); } /** * Set failure in test-case. * * @param testCase test-case instance. * @param message error message. * @param trace error trace. */ public static void setFailure(ITestCase testCase, String message, String trace) { TestCaseResult caseResult = (TestCaseResult) map.get(testCase); caseResult.setFailure(message, trace); } /** * Add System.out'ed message string. * * @param testCase test-case instance. * @param message system-out message. */ public static void addSystemOut(ITestCase testCase, String message) { TestCaseResult caseResult = (TestCaseResult) map.get(testCase); caseResult.addSystemOut(message); } /** * Add System.err'ed message string. * * @param testCase test-case instance. * @param message system-err message. */ public static void addSystemErr(ITestCase testCase, String message) { TestCaseResult caseResult = (TestCaseResult) map.get(testCase); caseResult.addSystemErr(message); } private static String logFormat(String level, String message) { return DATE_TIME_FORMAT.format(System.currentTimeMillis()) + "[" + level + "] " + message; } /** * Add System.out'ed message string. * * @param testCase test-case instance. * @param level level string. * @param message info log message. */ public static void sysOutLog(ITestCase testCase, String level, String message) { String msg = logFormat(level, message); if (testCase != null) addSystemOut(testCase, msg); if (ps != null) ps.println(msg); } /** * Add System.err'ed message string. * * @param testCase test-case instance. * @param level level string. * @param message error log message. */ public static void sysErrLog(ITestCase testCase, String level, String message) { String msg = logFormat(level, message); if (testCase != null) { addSystemOut(testCase, msg); addSystemErr(testCase, msg); } if (ps != null) ps.println(msg); } }
private static String logFormat(String level, String message) { return DATE_TIME_FORMAT.format(System.currentTimeMillis()) + "[" + level + "] " + message; }