/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxtrust.util.jsf;

import java.io.Serializable;
import java.text.ParseException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.faces.Converter;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

/**
 * @author: Yuriy Movchan Date: 11.24.2010
 */
@Converter()
@Name("uptimeConverter")
@BypassInterceptors
public class UptimeConverter implements javax.faces.convert.Converter, Serializable {

	private static final long serialVersionUID = -4292643644104278424L;

	private static final String[] dateFormats = { "D 'days' HH 'hours' mm 'mins' ss 'seconds'" };

	public Object getAsObject(FacesContext context, UIComponent comp, String value) throws ConverterException {
		if ((value == null) || value.trim().length() == 0) {
			return null;
		}

		try {
			return DateUtils.parseDate(value, dateFormats);
		} catch (ParseException e) {
			throw new ConverterException("Unable to convert " + value + " to seconds!");
		}
	}

	public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
		if (object instanceof String) {
			try {
				return DateFormatUtils.formatUTC(Long.valueOf((String) object), dateFormats[0]);
			} catch (NumberFormatException ex) {
				throw new ConverterException("Unable to convert " + object + " to date!");
			}
		}

		return null;
	}

}
@@ -229,32 +229,70 @@
 .ParseException;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport javax.f
@@ -412,61 +412,8 @@
 n;%0A%0A
-import org.apache.commons.lang.time.DateFormatUtils;%0A
 impo
@@ -1509,33 +1509,26 @@
 urn 
-DateFormatUtils.formatUTC
+getSecondsAsString
 (Lon
@@ -1557,24 +1557,8 @@
 ect)
-, dateFormats%5B0%5D
 );%0A%09
@@ -1685,16 +1685,16 @@
 %09%7D%0A%09%09%7D%0A%0A
-
 %09%09return
@@ -1702,11 +1702,481 @@
 null;%0A%09%7D
+%0A%09%0A%09private String getSecondsAsString(long seconds) %7B%0A%09%09int days = (int) TimeUnit.SECONDS.toDays(seconds);%0A%09%09long hours = TimeUnit.SECONDS.toHours(seconds) - (days * 24);%0A%09%09long mins = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds) * 60);%0A%09%09long secondsInMinute = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) * 60);%0A%0A%09%09return days + %22 days %22 + hours + %22 hours %22 + mins + %22 mins %22 + secondsInMinute + %22 seconds%22;%0A%09%7D
 %0A%0A%7D

Fix Server Status uptime #83