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

ca.carleton.gcrc.couch.date.DateServiceActions Maven / Gradle / Ivy

There is a newer version: 2.2.7
Show newest version
package ca.carleton.gcrc.couch.date;

import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;

import ca.carleton.gcrc.couch.date.impl.DateSource;
import ca.carleton.gcrc.couch.date.impl.DocumentWithInterval;
import ca.carleton.gcrc.couch.date.impl.NowReference;
import ca.carleton.gcrc.couch.date.impl.TimeInterval;
import ca.carleton.gcrc.couch.date.impl.SerializableToDot;
import ca.carleton.gcrc.couch.date.impl.SerializableToInfo;

public class DateServiceActions {
	
	private DateSource dateSource;
	
	public DateServiceActions(DateSource dateSource){
		this.dateSource = dateSource;
	}

	public JSONObject getDocIdsFromInterval(TimeInterval interval) throws Exception {
		NowReference now = NowReference.now();
		DateSource.SearchResults searchResults = dateSource.getDateIntervalsIntersectingWith(interval, now);
		List docWithInts = searchResults.documentWithIntervals;
		
		Set docIds = new HashSet();
		for(DocumentWithInterval docWithInt : docWithInts){
			docIds.add(docWithInt.getDocId());
		}
		
		JSONObject result = new JSONObject();
		
		JSONArray arr = new JSONArray();
		for(String docId : docIds){
			arr.put(docId);
		}
		result.put("docIds", arr);
		
		result.put("clusterCount", searchResults.clusterCount);
		result.put("intervalCount", searchResults.intervalCount);
		result.put("intervalMatched", searchResults.intervalMatched);
		
		return result;
	}

	public void getInfo(PrintWriter pw) throws Exception {
		if( dateSource instanceof SerializableToInfo ){
			SerializableToInfo infoSource = (SerializableToInfo)dateSource;
			infoSource.printInfo(pw);
		}
	}

	public void getDotInfo(PrintWriter pw) throws Exception {
		if( dateSource instanceof SerializableToDot ){
			SerializableToDot dotSource = (SerializableToDot)dateSource;
			dotSource.printDot(pw);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy