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

ca.carleton.gcrc.couch.command.schema.SchemaDefinition Maven / Gradle / Ivy

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

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.List;
import java.util.Vector;

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

public class SchemaDefinition {
	
	static public SchemaDefinition fronJson(JSONObject jsonDef) throws Exception {
		String groupName = jsonDef.getString("group");
		String schemaId = jsonDef.getString("id");
		
		SchemaDefinition def = new SchemaDefinition(groupName, schemaId);

		// label
		{
			String label = jsonDef.optString("label");
			if( null != label ){
				def.setLabel(label);
			}
		}
		
		// Attributes
		{
			JSONArray attributes = jsonDef.optJSONArray("attributes");
			if( null != attributes ) {
				for(int i=0,e=attributes.length(); i attributes = new Vector();
	private List initialLayers = new Vector();
	private List relatedSchemas = new Vector();
	
	public SchemaDefinition(String groupName, String schemaId){
		this.groupName = groupName;
		this.schemaId = schemaId;
	}
	
	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}
	
	public List getAttributes(){
		return attributes;
	}

	public void addAttribute(SchemaAttribute attribute){
		attributes.add(attribute);
	}
	
	public List getRelatedSchemas(){
		return relatedSchemas;
	}

	public void addRelatedSchema(String relatedSchemaName){
		relatedSchemas.add(relatedSchemaName);
	}
	
	public List getInitialLayers(){
		return initialLayers;
	}

	public void addInitialLayer(String layerId){
		initialLayers.add(layerId);
	}
	
	public String getDocumentIdentifier(){
		return "schema."+groupName+"_"+schemaId;
	}
	
	public String getSchemaName(){
		return groupName+"_"+schemaId;
	}
	
	public String getSchemaLabel(){
		if( null == label ){
			return getSchemaName();
		}
		
		return label;
	}
	
	public void saveToDocsDir(File parentDir) throws Exception {
		
		// Create directory
		File schemaDir = new File(parentDir, getDocumentIdentifier());
		if( false == schemaDir.exists() ){
			boolean created = schemaDir.mkdir();
			if( !created ){
				throw new Exception("Unable to create schema directory");
			}
		}
		
		// definition.json
		{
			File file = new File(schemaDir, "definition.json");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			JSONObject obj = toJson();
			osw.write( obj.toString(3) );
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		saveToSchemaDir(schemaDir);
	}
	
	public void saveToSchemaDir(File schemaDir) throws Exception {
		
		// _id.txt
		{
			File file = new File(schemaDir, "_id.txt");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			osw.write(getDocumentIdentifier());
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// nunaliit_type.txt
		{
			File file = new File(schemaDir, "nunaliit_type.txt");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			osw.write("schema");
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// nunaliit_schema.txt
		{
			File file = new File(schemaDir, "nunaliit_schema.txt");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			osw.write("schema");
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// isRootSchema.json
		{
			File file = new File(schemaDir, "isRootSchema.json");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			osw.write("true");
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// name.txt
		{
			File file = new File(schemaDir, "name.txt");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			osw.write( getSchemaName() );
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// label.txt
		{
			File file = new File(schemaDir, "label.txt");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			if( null != label ){
				osw.write( label );
			} else {
				osw.write( getSchemaName() );
			}
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// relatedSchemas.json
		{
			File file = new File(schemaDir, "relatedSchemas.json");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			JSONArray arr = new JSONArray();
			for(String schemaName : relatedSchemas){
				arr.put(schemaName);
			}
			osw.write( arr.toString(3) );
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// create.json
		{
			File file = new File(schemaDir, "create.json");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			JSONObject jsonCreate = computeCreateField();
			osw.write( jsonCreate.toString(3) );
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// brief.txt
		{
			File file = new File(schemaDir, "brief.txt");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			PrintWriter pw = new PrintWriter(osw);
			printBrief(pw);
			pw.flush();
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// display.txt
		{
			File file = new File(schemaDir, "display.txt");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			PrintWriter pw = new PrintWriter(osw);
			printDisplay(pw);
			pw.flush();
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// form.txt
		{
			File file = new File(schemaDir, "form.txt");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			PrintWriter pw = new PrintWriter(osw);
			printForm(pw);
			pw.flush();
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// export.json
		{
			File file = new File(schemaDir, "export.json");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			JSONArray jsonExport = computeExportField();
			osw.write( jsonExport.toString(3) );
			osw.flush();
			fos.flush();
			fos.close();
		}
		
		// csvExport.json
		{
			File file = new File(schemaDir, "csvExport.json");
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			JSONArray jsonExport = computeExportField();
			osw.write( jsonExport.toString(3) );
			osw.flush();
			fos.flush();
			fos.close();
		}
	}

	public JSONObject toJson() throws Exception {
		JSONObject jsonDef = new JSONObject();
		
		jsonDef.put("id", schemaId);
		jsonDef.put("group", groupName);
		
		if( null != label ) jsonDef.put("label", label);
		
		if( attributes.size() > 0 ){
			JSONArray jsonAttributes  = new JSONArray();
			
			for(SchemaAttribute attribute : attributes){
				JSONObject jsonAttr = attribute.toJson();
				jsonAttributes.put(jsonAttr);
			}
			
			jsonDef.put("attributes", jsonAttributes);
		}
		
		if( relatedSchemas.size() > 0 ){
			JSONArray jsonRelatedSchemas  = new JSONArray();
			
			for(String schemaName : relatedSchemas){
				jsonRelatedSchemas.put(schemaName);
			}
			
			jsonDef.put("releatedSchemas", jsonRelatedSchemas);
		}
		
		if( initialLayers.size() > 0 ){
			JSONArray jsonInitialLayers  = new JSONArray();
			
			for(String layerId : initialLayers){
				jsonInitialLayers.put(layerId);
			}
			
			jsonDef.put("initialLayers", jsonInitialLayers);
		}
		
		return jsonDef;
	}
	
	public JSONObject computeCreateField() throws Exception {
		JSONObject jsonCreate = new JSONObject();

		jsonCreate.put("nunaliit_schema", getSchemaName());
		
		if( initialLayers.size() > 0 ){
			JSONArray nunaliit_layers = new JSONArray();
			
			for(String layerId : initialLayers){
				nunaliit_layers.put(layerId);
			}
			
			jsonCreate.put("nunaliit_layers", nunaliit_layers);
		}
		
		JSONObject jsonDoc = new JSONObject();
		jsonCreate.put(getSchemaName(), jsonDoc);

		for(SchemaAttribute attribute : attributes){
			attribute.addCreateField(jsonCreate, jsonDoc);
		}
		
		return jsonCreate;
	}
	
	public JSONArray computeExportField() throws Exception {
		JSONArray jsonExport = new JSONArray();

//		// id
//		{
//			JSONObject o = new JSONObject();
//			o.put("select", "_id");
//			o.put("label", "id");
//			o.put("type", "text");
//			jsonExport.put(o);
//		}
//
//		// rev
//		{
//			JSONObject o = new JSONObject();
//			o.put("select", "_rev");
//			o.put("label", "rev");
//			o.put("type", "text");
//			jsonExport.put(o);
//		}

		String schemaName = getSchemaName();
		for(SchemaAttribute attribute : attributes){
			attribute.addExportField(jsonExport, schemaName);
		}
		
		return jsonExport;
	}
	
	public void printBrief(PrintWriter pw) throws Exception {
		String schemaName = getSchemaName();

		pw.print("");

		if( null != label ){
			pw.print("");
			pw.print(label);
		} else {
			pw.print("");
			pw.print(schemaName);
		}

		pw.print("(");
		
		boolean first = true;
		for(SchemaAttribute attribute : attributes){
			boolean printed = attribute.printBrief(pw,schemaName,first);
			if( printed ){
				first = false;
			}
		}
		
		pw.print(")");

		pw.print("");
	}
	
	public void printDisplay(PrintWriter pw) throws Exception {
		String schemaName = getSchemaName();
		
		pw.println("
"); pw.println("
"); for(SchemaAttribute attribute : attributes){ attribute.printDisplay(pw,schemaName); } pw.println("
"); pw.println("
"); } public void printForm(PrintWriter pw) throws Exception { String schemaName = getSchemaName(); pw.println("
"); pw.println("
"); for(SchemaAttribute attribute : attributes){ attribute.printForm(pw,schemaName); } pw.println("
"); pw.println("
"); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy