Java tutorial
/* * Copyrights 2002-2013 Webb Fontaine * Date: 24/06/13 * This software is the proprietary information of Webb Fontaine. * Its use is subject to License terms. */ package com.webbfontaine.valuewebb.irms.impl.assignment.snapshot; import com.google.common.base.Throwables; import com.webbfontaine.valuewebb.config.NativeQueriesContainer; import com.webbfontaine.valuewebb.model.util.Utils; import org.apache.commons.lang3.time.DateUtils; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.jboss.seam.annotations.Transactional; import org.jboss.seam.annotations.async.Asynchronous; import org.jboss.seam.annotations.async.Expiration; import org.jboss.seam.annotations.async.IntervalCron; import org.jboss.seam.async.QuartzTriggerHandle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import java.text.ParseException; import java.util.Date; @Name("snapshotManager") @Scope(ScopeType.APPLICATION) public class SnapshotManager implements Serializable { private static final long serialVersionUID = -1863719407894576912L; private static final Logger LOGGER = LoggerFactory.getLogger(SnapshotManager.class); public static final Date INITIAL_DEFAULT_DATE = parseDate(); private static final String SNAPSHOT_UPDATE_QUERY = "UPDATE WORKLOAD_SNAPSHOT SNAP SET WORKLOAD = (WORKLOAD + " + "(SELECT NVL(SUM(WORKLOAD), 0) FROM TT_ASSIGNMENT_EVENT EVENT WHERE EVENT.ASSIGNEE=SNAP.ASSIGNEE AND EVENT.DATE_CREATED > SNAP.UPDATE_DATE AND EVENT.DATE_CREATED < ?)), " + "UPDATE_DATE = ?"; private static final String SNAPSHOT_INSERT_QUERY_NAME = "SNAPSHOT_INSERT_QUERY"; private NativeQueriesContainer nativeQueriesContainer; @Asynchronous @Transactional public QuartzTriggerHandle updateSnapshot(@Expiration Date when, @IntervalCron String interval) { LOGGER.debug("Workload snapshot insert native query: {}", nativeQueriesContainer.getNativeQuery(SNAPSHOT_INSERT_QUERY_NAME)); LOGGER.info("Updating workload snapshot...."); try (Connection connection = Utils.getSQLConnection(); PreparedStatement insertStmt = connection .prepareStatement(nativeQueriesContainer.getNativeQuery(SNAPSHOT_INSERT_QUERY_NAME)); PreparedStatement updateStmt = connection.prepareStatement(SNAPSHOT_UPDATE_QUERY)) { insertStmt.setTimestamp(1, toTimestamp(INITIAL_DEFAULT_DATE)); int insertedCount = insertStmt.executeUpdate(); Date now = new Date(); updateStmt.setTimestamp(1, toTimestamp(now)); updateStmt.setTimestamp(2, toTimestamp(now)); int updatedCount = updateStmt.executeUpdate(); LOGGER.debug("Snapshot update stat: inserted new rows count: {}, updated rows count: {}", insertedCount, updatedCount); } catch (SQLException e) { LOGGER.error("", e); } return null; } private Timestamp toTimestamp(Date dateValue) { return new Timestamp(dateValue.getTime()); } private static Date parseDate() { try { return DateUtils.parseDate("01-01-1970", "dd-MM-yyyy"); } catch (ParseException e) { throw Throwables.propagate(e); } } private void writeObject(ObjectOutputStream os) throws IOException { os.defaultWriteObject(); } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); } @In(create = true) public void setNativeQueriesContainer(NativeQueriesContainer nativeQueriesContainer) { this.nativeQueriesContainer = nativeQueriesContainer; } }