Example usage for org.joda.time LocalDateTime LocalDateTime

List of usage examples for org.joda.time LocalDateTime LocalDateTime

Introduction

In this page you can find the example usage for org.joda.time LocalDateTime LocalDateTime.

Prototype

public LocalDateTime() 

Source Link

Document

Constructs an instance set to the current local time evaluated using ISO chronology in the default zone.

Usage

From source file:com.axelor.auth.AuditInterceptor.java

License:Open Source License

@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
        String[] propertyNames, Type[] types) {

    if (!(entity instanceof AuditableModel)) {
        return false;
    }/*from   w  w  w  . j  a v  a  2s .c  o m*/

    final User user = this.getUser();
    for (int i = 0; i < propertyNames.length; i++) {
        if (!canUpdate(entity, propertyNames[i], previousState[i], currentState[i])) {
            throw new PersistenceException(String.format("You can't update: %s#%s, values (%s=%s)",
                    entity.getClass().getName(), id, propertyNames[i], currentState[i]));
        }
        if (UPDATED_ON.equals(propertyNames[i])) {
            currentState[i] = new LocalDateTime();
        }
        if (UPDATED_BY.equals(propertyNames[i]) && user != null) {
            currentState[i] = user;
        }
    }

    // change tracking
    if (tracker.get() != null) {
        tracker.get().track(getUser(), (AuditableModel) entity, propertyNames, currentState, previousState);
    }

    return true;
}

From source file:com.axelor.auth.AuditInterceptor.java

License:Open Source License

@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {

    boolean changed = updateSequence(entity, propertyNames, state);
    if (!(entity instanceof AuditableModel)) {
        return changed;
    }//from ww  w. jav a 2 s.  co  m

    final User user = this.getUser();
    for (int i = 0; i < propertyNames.length; i++) {
        if (state[i] != null) {
            continue;
        }
        if (CREATED_ON.equals(propertyNames[i])) {
            state[i] = new LocalDateTime();
        }
        if (CREATED_BY.equals(propertyNames[i]) && user != null) {
            state[i] = user;
        }
    }

    // change tracking
    if (tracker.get() != null) {
        tracker.get().track(user, (AuditableModel) entity, propertyNames, state, null);
    }

    return true;
}

From source file:com.axelor.auth.db.AuditInterceptor.java

License:Open Source License

@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
        String[] propertyNames, Type[] types) {
    if (!(entity instanceof AuditableModel)) {
        return false;
    }//from  w ww  .ja  v  a 2s .c o  m
    User user = this.getUser();
    for (int i = 0; i < propertyNames.length; i++) {
        if (!canUpdate(entity, propertyNames[i], previousState[i], currentState[i])) {
            throw new PersistenceException(String.format(I18n.get("You can't update: %s#%s, values (%s=%s)"),
                    entity.getClass().getName(), id, propertyNames[i], currentState[i]));
        }
        if (UPDATED_ON.equals(propertyNames[i])) {
            currentState[i] = new LocalDateTime();
        }
        if (UPDATED_BY.equals(propertyNames[i]) && user != null) {
            currentState[i] = user;
        }
    }
    return true;
}

From source file:com.axelor.auth.db.AuditInterceptor.java

License:Open Source License

@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
    if (!(entity instanceof AuditableModel)) {
        return false;
    }/* ww w . ja v  a  2s  .c o  m*/
    User user = this.getUser();
    for (int i = 0; i < propertyNames.length; i++) {
        if (state[i] != null) {
            continue;
        }
        if (CREATED_ON.equals(propertyNames[i])) {
            state[i] = new LocalDateTime();
        }
        if (CREATED_BY.equals(propertyNames[i]) && user != null) {
            state[i] = user;
        }
    }
    return true;
}

From source file:com.axelor.csv.script.ImportDateTime.java

License:Open Source License

public String importDate(String inputDate) {

    String patDate = "(" + dt + "|TODAY)(\\[(" + String.format(pat, 4, "y") + "?" + String.format(pat, 2, "M")
            + "?" + String.format(pat, 2, "d") + "?" + ")\\])?";
    try {/* w w  w. ja  va 2s  .co  m*/
        if (!Strings.isNullOrEmpty(inputDate) && inputDate.matches(patDate)) {
            List<String> dates = Arrays.asList(inputDate.split("\\["));
            inputDate = dates.get(0).equals("TODAY") ? new LocalDateTime().toString("yyyy-MM-dd")
                    : dates.get(0);
            if (dates.size() > 1) {
                LocalDateTime dateTime = new LocalDateTime(inputDate);
                Matcher matcher = Pattern.compile(String.format(pat, 4, "y")).matcher(dates.get(1));
                if (matcher.find())
                    dateTime = updateYear(dateTime, matcher.group());
                matcher = Pattern.compile(String.format(pat, 2, "M")).matcher(dates.get(1));
                if (matcher.find())
                    dateTime = updateMonth(dateTime, matcher.group());
                matcher = Pattern.compile(String.format(pat, 2, "d")).matcher(dates.get(1));
                if (matcher.find())
                    dateTime = updateDay(dateTime, matcher.group());
                return dateTime.toLocalDate().toString();
            } else
                return inputDate;
        } else
            return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.axelor.csv.script.ImportDateTime.java

License:Open Source License

public String importDateTime(String inputDateTime) {
    String tm = "[0-9]{2}:[0-9]{2}:[0-9]{2}";
    String patTime = "(" + dt + " " + tm + "|NOW)(\\[(" + String.format(pat, 4, "y") + "?"
            + String.format(pat, 2, "M") + "?" + String.format(pat, 2, "d") + "?" + String.format(pat, 2, "H")
            + "?" + String.format(pat, 2, "m") + "?" + String.format(pat, 2, "s") + "?" + ")\\])?";
    try {//from w  ww.  j  a va2 s. c  om
        if (!Strings.isNullOrEmpty(inputDateTime) && inputDateTime.matches(patTime)) {
            List<String> timeList = Arrays.asList(inputDateTime.split("\\["));
            inputDateTime = timeList.get(0).equals("NOW") ? new LocalDateTime().toString("yyyy-MM-dd HH:mm:ss")
                    : timeList.get(0);
            if (timeList.size() > 1) {
                LocalDateTime dateTime = new LocalDateTime(inputDateTime.replace(" ", "T"));
                Matcher matcher = Pattern.compile(String.format(pat, 4, "y")).matcher(timeList.get(1));
                if (matcher.find())
                    dateTime = updateYear(dateTime, matcher.group());
                matcher = Pattern.compile(String.format(pat, 2, "M")).matcher(timeList.get(1));
                if (matcher.find())
                    dateTime = updateMonth(dateTime, matcher.group());
                matcher = Pattern.compile(String.format(pat, 2, "d")).matcher(timeList.get(1));
                if (matcher.find())
                    dateTime = updateDay(dateTime, matcher.group());
                matcher = Pattern.compile(String.format(pat, 2, "H")).matcher(timeList.get(1));
                if (matcher.find())
                    dateTime = updateHour(dateTime, matcher.group());
                matcher = Pattern.compile(String.format(pat, 2, "m")).matcher(timeList.get(1));
                if (matcher.find())
                    dateTime = updateMinute(dateTime, matcher.group());
                matcher = Pattern.compile(String.format(pat, 2, "s")).matcher(timeList.get(1));
                if (matcher.find())
                    dateTime = updateSecond(dateTime, matcher.group());
                return dateTime.toString();
            }
            return inputDateTime.replace(" ", "T");
        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.axelor.script.ScriptBindings.java

License:Open Source License

@SuppressWarnings("all")
private Object getSpecial(String name) throws Exception {
    switch (name) {
    case "__id__":
        if (variables.containsKey("id"))
            return Longs.tryParse(variables.get("id").toString());
        if (variables.containsKey("_id"))
            return Longs.tryParse(variables.get("_id").toString());
        return ((Context) variables).asType(Model.class).getId();
    case "__ids__":
        return variables.get("_ids");
    case "__this__":
        return ((Context) variables).asType(Model.class);
    case "__parent__":
        return ((Context) variables).getParentContext();
    case "__date__":
        return new LocalDate();
    case "__time__":
        return new LocalDateTime();
    case "__datetime__":
        return new DateTime();
    case "__config__":
        if (configContext == null) {
            configContext = new ConfigContext();
        }// www . j ava2  s .  c o m
        return configContext;
    case "__user__":
        return AuthUtils.getUser();
    case "__self__":
        Model bean = ((Context) variables).asType(Model.class);
        if (bean == null || bean.getId() == null)
            return null;
        return JPA.em().getReference(EntityHelper.getEntityClass(bean), bean.getId());
    case "__ref__":
        Map values = (Map) variables.get("_ref");
        Class<?> klass = Class.forName((String) values.get("_model"));
        return JPA.em().getReference(klass, Long.parseLong(values.get("id").toString()));
    }
    return null;
}

From source file:com.axelor.studio.service.wkf.WkfTrackingService.java

License:Open Source License

/**
 * Method add new WkfTrackingLine in WkfTracking for given status if that
 * status is not last status added.//from   w w  w. j  av a2s  .  co m
 * 
 * @param wkfTracking
 *            WkfTracking to update with new line.
 * @param status
 *            Status to check for update.
 * @return WkfTrackingLine created or return null if status not changed.
 */
@Transactional
public WkfTrackingLine updateTrackingLine(WkfTracking wkfTracking, String status) {

    WkfTrackingLine trackingLine = trackingLineRepo.all().filter("self.wkfTracking = ?1", wkfTracking)
            .fetchOne();

    if (trackingLine == null || !trackingLine.getStatus().equals(status)) {

        LocalDateTime now = new LocalDateTime();

        if (trackingLine != null) {
            oldStatus = trackingLine.getStatus();
            LocalDateTime lastUpdated = trackingLine.getCreatedOn();
            Minutes minutes = Minutes.minutesBetween(lastUpdated, now);
            log.debug("Minutes between {} and {} : {}", lastUpdated, now, minutes.getMinutes());
            durationHrs = new BigDecimal(minutes.getMinutes()).divide(new BigDecimal(60), 2,
                    RoundingMode.HALF_UP);
            log.debug("Hours between {} and {} : {}", lastUpdated, now, durationHrs);
            trackingLine.setTimeSpent(durationHrs);
            trackingLineRepo.save(trackingLine);
        }

        trackingLine = new WkfTrackingLine();
        trackingLine.setWkfTracking(wkfTracking);
        trackingLine.setStatus(status);
        trackingLine.setWkfTracking(wkfTracking);
        return trackingLineRepo.save(trackingLine);
    }

    return null;
}

From source file:com.axelor.tool.template.TemplateMaker.java

License:Open Source License

public String make() {
    if (Strings.isNullOrEmpty(this.template)) {
        throw new IllegalArgumentException(I18n.get(IExceptionMessage.TEMPLATE_MAKER_2));
    }//from   w  ww .j  a v  a  2 s.c o m

    ST st = new ST(stGroup, template);

    Map<String, Object> _map = Maps.newHashMap();
    if (localContext != null && !localContext.isEmpty()) {
        _map.putAll(localContext);
    }
    _map.putAll(context);

    //Internal context
    _map.put("__user__", AuthUtils.getUser());
    _map.put("__date__", new LocalDate());
    _map.put("__time__", new LocalTime());
    _map.put("__datetime__", new LocalDateTime());

    for (String key : _map.keySet()) {
        st.add(key, _map.get(key));
    }

    return _make(st);
}

From source file:com.esofthead.mycollab.community.common.service.AppPropertiesServiceImpl.java

License:Open Source License

@Override
public void afterPropertiesSet() throws Exception {
    try {/*  w w  w  . j a v  a2s. co  m*/
        File homeFolder = FileUtils.getHomeFolder();
        File sysFile = new File(homeFolder, ".app.properties");
        properties = new Properties();
        if (sysFile.isFile() && sysFile.exists()) {
            properties.load(new FileInputStream(sysFile));
            String startDate = properties.getProperty("startdate");
            if (startDate == null) {
                properties.setProperty("startdate",
                        DateTimeUtils.formatDateToW3C(new GregorianCalendar().getTime()));
                properties.store(new FileOutputStream(sysFile), "");
            }
        } else {
            properties.setProperty("id",
                    UUID.randomUUID().toString() + new LocalDateTime().getMillisOfSecond());
            properties.setProperty("startdate",
                    DateTimeUtils.formatDateToW3C(new GregorianCalendar().getTime()));
            properties.store(new FileOutputStream(sysFile), "");

        }
    } catch (IOException e) {
        LOG.error("Error", e);
    }
}