Search in sources :

Example 1 with DateField

use of com.manydesigns.elements.fields.DateField in project Portofino by ManyDesigns.

the class XlsUtil method addFieldToCell.

public void addFieldToCell(WritableSheet sheet, int i, int j, Field field) throws WriteException {
    if (field instanceof NumericField) {
        NumericField numField = (NumericField) field;
        if (numField.getValue() != null) {
            jxl.write.Number number;
            BigDecimal decimalValue = numField.getValue();
            if (numField.getDecimalFormat() == null) {
                number = new Number(j, i, decimalValue.doubleValue());
            } else {
                number = new Number(j, i, decimalValue.doubleValue(), getNumberFormat(numField.getDecimalFormat().toPattern()));
            }
            sheet.addCell(number);
        }
    } else if (field instanceof PasswordField) {
        Label label = new Label(j, i, PasswordField.PASSWORD_PLACEHOLDER);
        sheet.addCell(label);
    } else if (field instanceof DateField) {
        DateField dateField = (DateField) field;
        DateTime dateCell;
        Date date = dateField.getValue();
        if (date != null) {
            dateCell = new DateTime(j, i, date, getDateFormat(dateField.getDatePattern()));
            sheet.addCell(dateCell);
        }
    } else {
        Label label = new Label(j, i, field.getStringValue());
        sheet.addCell(label);
    }
}
Also used : Number(jxl.write.Number) Number(jxl.write.Number) NumericField(com.manydesigns.elements.fields.NumericField) PasswordField(com.manydesigns.elements.fields.PasswordField) DateField(com.manydesigns.elements.fields.DateField) jxl.write(jxl.write) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Example 2 with DateField

use of com.manydesigns.elements.fields.DateField in project Portofino by ManyDesigns.

the class PersistenceTest method testDateWithDTSSwitch.

// Disable because it fails on the CI server where the timezone is different
@Test(enabled = false)
public void testDateWithDTSSwitch() {
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome"));
    // 1973-06-03 DTS switch at midnight in tz Europe/Rome
    Date problematicDate = new Date(73, 5, 3);
    Map<String, Object> order = new HashMap<>();
    order.put("orderid", new BigInteger("1"));
    order.put("userid", "x");
    order.put("orderdate", problematicDate);
    order.put("shipaddr1", "x");
    order.put("shipaddr2", "x");
    order.put("shipcity", "x");
    order.put("shipstate", "x");
    order.put("shipzip", "x");
    order.put("shipcountry", "x");
    order.put("billaddr1", "x");
    order.put("billaddr2", "x");
    order.put("billcity", "x");
    order.put("billstate", "x");
    order.put("billzip", "x");
    order.put("billcountry", "x");
    order.put("courier", "x");
    order.put("totalprice", new BigDecimal(1.0));
    order.put("billtofirstname", "x");
    order.put("billtolastname", "x");
    order.put("shiptofirstname", "x");
    order.put("shiptolastname", "x");
    order.put("creditcard", "x");
    order.put("exprdate", "x");
    order.put("cardtype", "x");
    order.put("locale", "x");
    Session session = persistence.getSession("jpetstore");
    session.save("orders", order);
    FormBuilder fb = new FormBuilder(persistence.getTableAccessor("jpetstore", "orders"));
    Form form = fb.configFields("orderdate").build();
    DateField dateField = (DateField) form.findFieldByPropertyName("orderdate");
    dateField.readFromObject(order);
    assertEquals("dd-MM-yyyy", ElementsProperties.getConfiguration().getString("fields.date.format"));
    String strDate = "03-06-1973";
    assertEquals(strDate, dateField.getStringValue());
    MutableHttpServletRequest request = new MutableHttpServletRequest();
    request.setParameter("date", strDate);
    dateField.readFromRequest(request);
    assertTrue(dateField.validate());
    dateField.writeToObject(order);
    session.update("orders", order);
    session.flush();
    // Forget about order
    session.clear();
    order = (Map) session.get("orders", new BigInteger("1"));
    // Convert to java.util.Date from java.sql.Date
    Date date = new Date(((Date) order.get("orderdate")).getTime());
    assertEquals(73, date.getYear());
    assertEquals(5, date.getMonth());
    assertEquals(3, date.getDate());
    assertEquals(1, date.getHours());
    assertEquals(0, date.getMinutes());
    assertEquals(0, date.getSeconds());
    dateField.readFromObject(order);
    assertEquals(strDate, dateField.getStringValue());
}
Also used : FormBuilder(com.manydesigns.elements.forms.FormBuilder) Form(com.manydesigns.elements.forms.Form) BigInteger(java.math.BigInteger) FileObject(org.apache.commons.vfs2.FileObject) MutableHttpServletRequest(com.manydesigns.elements.servlet.MutableHttpServletRequest) DateField(com.manydesigns.elements.fields.DateField) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session)

Aggregations

DateField (com.manydesigns.elements.fields.DateField)2 BigDecimal (java.math.BigDecimal)2 NumericField (com.manydesigns.elements.fields.NumericField)1 PasswordField (com.manydesigns.elements.fields.PasswordField)1 Form (com.manydesigns.elements.forms.Form)1 FormBuilder (com.manydesigns.elements.forms.FormBuilder)1 MutableHttpServletRequest (com.manydesigns.elements.servlet.MutableHttpServletRequest)1 BigInteger (java.math.BigInteger)1 LocalDate (java.time.LocalDate)1 Date (java.util.Date)1 jxl.write (jxl.write)1 Number (jxl.write.Number)1 FileObject (org.apache.commons.vfs2.FileObject)1 Session (org.hibernate.Session)1