001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.jfreereport.legacy;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018: import org.jfree.report.event.ReportEvent;
019: import org.jfree.report.function.AbstractFunction;
020: import org.jfree.report.function.Expression;
021:
022: import java.util.Date;
023:
024: @Deprecated
025: public class TimeDiffAndFormatFunction extends AbstractFunction {
026: @Nullable
027: private String formatted;
028:
029: @Nullable
030: private String field1;
031: @Nullable
032: private String field2;
033:
034: public TimeDiffAndFormatFunction() {
035: }
036:
037: public TimeDiffAndFormatFunction(@NotNull
038: final String name) {
039: this ();
040: setName(name);
041: }
042:
043: @Nullable
044: public String getField1() {
045: return field1;
046: }
047:
048: public void setField1(@Nullable
049: String field1) {
050: this .field1 = field1;
051: }
052:
053: @Nullable
054: public String getField2() {
055: return field2;
056: }
057:
058: public void setField2(@Nullable
059: String field2) {
060: this .field2 = field2;
061: }
062:
063: public void itemsAdvanced(@NotNull
064: final ReportEvent event) {
065: formatted = null;
066:
067: final Object fieldValue1 = getDataRow().get(getField1());
068: final Object fieldValue2 = getDataRow().get(getField2());
069: long value1;
070: if (fieldValue1 instanceof Number) {
071: Number number = (Number) fieldValue1;
072: value1 = number.longValue();
073: } else if (fieldValue1 instanceof Date) {
074: Date date = (Date) fieldValue1;
075: value1 = date.getTime();
076: } else {
077: return;
078: }
079:
080: long value2;
081: if (fieldValue2 instanceof Number) {
082: Number number = (Number) fieldValue2;
083: value2 = number.longValue();
084: } else if (fieldValue2 instanceof Date) {
085: Date date = (Date) fieldValue2;
086: value2 = date.getTime();
087: } else {
088: return;
089: }
090:
091: long diff = value1 - value2;
092: format(diff);
093: }
094:
095: private void format(long value1) {
096: long hours = value1 / (60 * 60 * 1000);
097: long rest = value1 - (hours * 60 * 60 * 1000);
098: long minutes = rest / (60 * 1000);
099: rest -= minutes * 60 * 1000;
100: long seconds = rest / 1000;
101:
102: String min = String.valueOf(minutes);
103: String sec = String.valueOf(seconds);
104: if (min.length() < 2) {
105: min = "0" + min;
106: }
107: if (sec.length() < 2) {
108: sec = "0" + sec;
109: }
110: formatted = hours + ":" + min + ":" + sec;
111: }
112:
113: @Nullable
114: public Object getValue() {
115: return formatted;
116: }
117:
118: @NotNull
119: public Expression getInstance() {
120: return super.getInstance();
121: }
122:
123: }
|