All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.nn.testtool.MetadataExtractor Maven / Gradle / Ivy

package nl.nn.testtool;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import nl.nn.testtool.util.LogUtil;

import org.apache.log4j.Logger;

/**
 * @author Jaco de Groot
 */
public class MetadataExtractor {
	private static Logger log = LogUtil.getLogger(MetadataExtractor.class);
	public static final int VALUE_TYPE_OBJECT = 0;
	public static final int VALUE_TYPE_STRING = 1;
	public static final int VALUE_TYPE_GUI = 2;
	private static final SimpleDateFormat FORMAT_DATE_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	public List extraMetadataFieldExtractors;
	
	public void setExtraMetadataFieldExtractors(List extraMetadataFieldExtractors) {
		this.extraMetadataFieldExtractors = extraMetadataFieldExtractors;
	}

	public String getLabel(String metadataName) {
		// TODO use reflection or spring or something like that for getLabel and getShortLabel?
		if (metadataName.equals("storageId")) {
			return "Storage Id";
		}
		if (metadataName.equals("storageSize")) {
			return "Storage size";
		}
		if (metadataName.equals("name")) {
			return "Name";
		}
		if (metadataName.equals("description")) {
			return "Description";
		}
		if (metadataName.equals("path")) {
			return "Path";
		}
		if (metadataName.equals("correlationId")) {
			return "Correlation Id";
		}
		if (metadataName.equals("startTime")) {
			return "Start time";
		}
		if (metadataName.equals("endTime")) {
			return "End time";
		}
		if (metadataName.equals("estimatedMemoryUsage")) {
			return "Estimated memory usage";
		}
		if (metadataName.equals("numberOfCheckpoints")) {
			return "Number of checkpoints";
		}
		if (metadataName.equals("duration")) {
			return "Duration";
		}
		String label = null;
		if (extraMetadataFieldExtractors != null) {
			Iterator iterator = extraMetadataFieldExtractors.iterator();
			while (iterator.hasNext() && label == null) {
				MetadataFieldExtractor metadataFieldExtractor = (MetadataFieldExtractor)iterator.next();
				if (metadataFieldExtractor.getName().equals(metadataName)) {
					label = metadataFieldExtractor.getLabel();
				}
			}
		}
		if (label == null) {
			label = metadataName;
		}
		return label;
	}

	public String getShortLabel(String metadataName) {
		if (metadataName.equals("storageId")) {
			return "StorageId";
		}
		if (metadataName.equals("storageSize")) {
			return "StorageSize";
		}
		if (metadataName.equals("name")) {
			return "Name";
		}
		if (metadataName.equals("description")) {
			return "Description";
		}
		if (metadataName.equals("path")) {
			return "Path";
		}
		if (metadataName.equals("correlationId")) {
			return "CorrelationId";
		}
		if (metadataName.equals("startTime")) {
			return "StartTime";
		}
		if (metadataName.equals("endTime")) {
			return "EndTime";
		}
		if (metadataName.equals("estimatedMemoryUsage")) {
			return "EstMemUsage";
		}
		if (metadataName.equals("numberOfCheckpoints")) {
			return "NrChpts";
		}
		if (metadataName.equals("duration")) {
			return "Duration";
		}
		String shortLabel = null;
		if (extraMetadataFieldExtractors != null) {
			Iterator iterator = extraMetadataFieldExtractors.iterator();
			while (iterator.hasNext() && shortLabel == null) {
				MetadataFieldExtractor metadataFieldExtractor = (MetadataFieldExtractor)iterator.next();
				if (metadataFieldExtractor.getName().equals(metadataName)) {
					shortLabel = metadataFieldExtractor.getShortLabel();
				}
			}
		}
		if (shortLabel == null) {
			shortLabel = getLabel(metadataName);
		}
		return shortLabel;
	}

	public Object getMetadata(Report report, String metadataName, int metadataValueType) {
		return fromObjectTo(metadataName, getMetadataAsObject(report, metadataName), metadataValueType);
	}

	private Object getMetadataAsObject(Report report, String metadataName) {
		// TODO use reflection or spring or something like that?
		if (metadataName.equals("storageId")) {
			return report.getStorageId();
		}
		if (metadataName.equals("storageSize")) {
			return report.getStorageSize();
		}
		if (metadataName.equals("name")) {
			return report.getName();
		}
		if (metadataName.equals("description")) {
			return report.getDescription();
		}
		if (metadataName.equals("path")) {
			return report.getPath();
		}
		if (metadataName.equals("correlationId")) {
			return report.getCorrelationId();
		}
		if (metadataName.equals("startTime")) {
			return new Long(report.getStartTime());
		}
		if (metadataName.equals("endTime")) {
			return new Long(report.getEndTime());
		}
		if (metadataName.equals("estimatedMemoryUsage")) {
			return new Long(report.getEstimatedMemoryUsage());
		}
		if (metadataName.equals("numberOfCheckpoints")) {
			return new Integer(report.getNumberOfCheckpoints());
		}
		if (metadataName.equals("duration")) {
			return "" + (report.getEndTime() - report.getStartTime());
		}
		Object metadata = null;
		if (extraMetadataFieldExtractors != null) {
			Iterator iterator = extraMetadataFieldExtractors.iterator();
			while (iterator.hasNext() && metadata == null) {
				MetadataFieldExtractor metadataFieldExtractor = (MetadataFieldExtractor)iterator.next();
				if (metadataFieldExtractor.getName().equals(metadataName)) {
					metadata = metadataFieldExtractor.extractMetadata(report);
				}
			}
		}
		return metadata;
	}

	public Object fromObjectTo(String metadataName, Object metadataValue, int metadataValueType) {
		if (metadataValueType == VALUE_TYPE_STRING) {
			metadataValue = fromObjectToString(metadataName, metadataValue);
		} else if (metadataValueType == VALUE_TYPE_GUI) {
			metadataValue = fromObjectToGUI(metadataName, metadataValue);
		}
		return metadataValue;
	}

	public Object fromObjectToString(String metadataName, Object metadataValue) {
		return "" + metadataValue;		
	}

	public Object fromObjectToGUI(String metadataName, Object metadataValue) {
		if (metadataName.equals("startTime")) {
			return FORMAT_DATE_TIME.format(metadataValue);
		}
		if (metadataName.equals("endTime")) {
			return FORMAT_DATE_TIME.format(metadataValue);
		}
		return metadataValue;		
	}

	public Object fromStringTo(String metadataName, String metadataValue, int metadataValueType) {
		Object value = metadataValue;
		if (metadataValueType == VALUE_TYPE_OBJECT) {
			value = fromStringtoObject(metadataName, metadataValue);
		} else if (metadataValueType == VALUE_TYPE_GUI) {
			value = fromStringtoObject(metadataName, metadataValue);
			value = fromObjectToGUI(metadataName, value);
		}
		return value;
	}

	public Object fromStringtoObject(String metadataName, String metadataValue) {
		if (metadataName.equals("storageId")) {
			return new Integer(metadataValue);
		}
		if (metadataName.equals("storageSize")) {
			return new Long(metadataValue);
		}
		if (metadataName.equals("startTime")) {
			return new Long(metadataValue);
		}
		if (metadataName.equals("endTime")) {
			return new Long(metadataValue);
		}
		if (metadataName.equals("estimatedMemoryUsage")) {
			return new Long(metadataValue);
		}
		if (metadataName.equals("numberOfCheckpoints")) {
			return new Integer(metadataValue);
		}
		return metadataValue;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy