Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 eTimeline, LLC. All rights reserved.
  #
 #  License:
 #    See the license.txt file in the project's top-level directory for details.
 #
 #  Authors:
 #    * Andy White
 #
 ************************************************************************ */
 package divconq.schema;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
DivConq uses a specialized type system that provides type consistency across services (including web services), database fields and stored procedures, as well as scripting. All scalars (including primitives) and composites (collections) are wrapped by some subclass of Struct. List/array collections are expressed by this class. This class is analogous to an Array in JSON but may contain type information as well, similar to Yaml. There are schema files (written in Xml and stored in the Packages repository) that define all the known data types, including complex data types. These schema files get compiled for for a given project and delpoyed as part of the conf directory. This class oversees the management of all the known data types as well as database tables, stored procedures and services (including web services).
 
 public class SchemaManager {
 	// composite schema of database
 	protected DatabaseSchema db = new DatabaseSchema(this);
 	
 	// composite schema of database
 	protected ServiceSchema service = new ServiceSchema(this);
 	
 	// types with ids
 	protected HashMap<StringDataTypeknownTypes = new HashMap<StringDataType>();

Returns:
schema pertaining to the dcDb (stored procs, tables, etc)
 
 	public DatabaseSchema getDb() {
 		return this.;
 	}

Returns:
schema pertaining to services
 
 	public ServiceSchema getService() {
 		return this.;
 	}

Returns:
map of all known data types
 
 	public Map<StringDataTypeknownTypes() {
 		return this.;
 	}

Take a given message and treat it is a service request - see that it is valid.

Parameters:
msg service request
Returns:
log of validation attempt
 
 		
 		OperationContext tc = OperationContext.get();
 		
 		if (tc == null) {
 			mr.errorTr(431);		
		}
		else {
			Op op = this..getOp(msg);
			if (op == null)
				mr.errorTr(432);		
			else if (op.request == null)
				mr.errorTr(433);	
			else if (!msg.isVerifyRequest() && !tc.isAuthorized(op.securityTags)) {
				mr.errorTr(434);
				..println("cannot call: " + msg);
			}
			else
				op.request.validate(msgmr);
		}
		return mr;
	}

Take a given message and treat it is a service response - see that it is valid.

Parameters:
msg service response
original original request
Returns:
log of validation attempt
	public OperationResult validateResponse(Message msgMessage original){
		return this.validateResponse(msgoriginal.getFieldAsString("Service"), original.getFieldAsString("Feature"), original.getFieldAsString("Op"));
	}

Take a given message and treat it is a service response - see that it is valid.

Parameters:
msg service response
service name
feature name
op name
Returns:
log of validation attempt
	public OperationResult validateResponse(Message msgString serviceString featureString op){
		DataType dt = this..getResponseType(servicefeatureop);
		if (dt == null)
			mr.errorTr(435);		
		else
			dt.validate(msgmr);
		return mr;
	}

For a given stored procedure, check that the parameters comprise a valid request.

Parameters:
name of the procedure
req procedure parameters
Returns:
log of validation attempt
		OperationContext tc = OperationContext.get();
		if (tc == null) {
			mr.errorTr(425);
		}
		else {
			DbProc proc = this..getProc(name);
			if (proc == null)
				mr.errorTr(426);		
			else {
				if (!tc.isAuthorized(proc.securityTags))
					mr.errorTr(427);		
				else {
					if (proc.request == null) {
						if ((req != null) && !req.isEmpty())
							mr.errorTr(428);		
					}
					else
						proc.request.validate(reqmr);
				}
			}
		}
		return mr;
	}

For a given stored procedure, check that the response is a valid structure.

Parameters:
name of stored procedure
resp structure returned
Returns:
log of validation attempt
		DbProc proc = this..getProc(name);
		if (proc == null)
			mr.errorTr(429);		
		else {
			if (proc.response == null) {
				if ((resp != null) && !resp.isEmpty())
					mr.errorTr(430);		
			}
			else
				proc.response.validate(respmr);
		}
		return mr;
	}

For a given structure, validate that it conforms to a given schema type

Parameters:
data structure to validate
type schema name of type
Returns:
log of validation attempt
	public OperationResult validateType(Struct dataString type){
		DataType dt = this..get(type);
		if (dt == null)
			mr.errorTr(436);		
		else
			dt.validate(datamr);
		return mr;
	}
	// TODO maybe just dump right to builder/output stream instead - save extra work 
	public RecordStruct toJsonDef() {
		RecordStruct def = new RecordStruct();
		ListStruct known = new ListStruct();
		def.setField("DataTypes"known);
		for (DataType dt : this..values()) 
			known.addItem(dt.toJsonDef(10));
		return def;
	}

Parameters:
type schema name of type
Returns:
the schema data type
	public DataType getType(String type) { 
		return this..get(type);
	}

Create a new record structure using a schema data type.

Parameters:
type type schema name of type
Returns:
initialized record structure
	public RecordStruct newRecord(String type) {
		DataType tp = this..get(type);
		if ((tp == null) || (tp.kind != .))
			return null;
		return new RecordStruct(tp);
	}

Schema files contain interdependencies, after loading the files call compile to resolve these interdependencies.

Returns:
log of compilation activity
		OperationResult mr = new OperationResult(OperationContext.getHubContext());
		// compiling not thread safe, do it once at start
		for (DataType dt : this..values()) 
			dt.compile(mr);
		this..compile(mr);
		this..compile(mr);
		return mr;
	}

Load a file containing schema into the master schema.

Parameters:
fl file to load
Returns:
log of the load attempt
		OperationResult or = new OperationResult(OperationContext.getHubContext());
		if (fl == null) {
			or.error(108, "Unable to apply schema file, file null");
			return or;
		}
		if (!fl.exists()) {
			or.error(109, "Missing schema file, expected: " + fl.getAbsolutePath());
			return or;
		}
		FuncResult<XElementxres3 = XmlReader.loadFile(flfalse);
		if (xres3.hasErrors()) {
			or.copyMessages(xres3);
			or.error(110, "Unable to apply schema file, missing xml");
			return or;
		}
		XElement schema = xres3.getResult();
		Schema s = new Schema();
		s.manager = this;
		s.loadSchema(orschema);
		return or;
	}

Load a file containing schema into the master schema.

Parameters:
fl file to load
Returns:
log of the load attempt
		OperationResult or = new OperationResult(OperationContext.getHubContext());
		if (fl == null) {
			or.error(108, "Unable to apply schema file, file null");
			return or;
		}
		FuncResult<XElementxres3 = XmlReader.parse(flfalse);
		if (xres3.hasErrors()) {
			or.copyMessages(xres3);
			or.error(110, "Unable to apply schema file, missing xml");
			return or;
		}
		XElement schema = xres3.getResult();
		Schema s = new Schema();
		s.manager = this;
		s.loadSchema(orschema);
		return or;
	}

Load type definition from an Xml element

Parameters:
or log of the load
schema to associate type with
dtel xml source of the definition
Returns:
the schema data type
	public DataType loadDataType(OperationResult orSchema schemaXElement dtel) {
		DataType dtype = new DataType(schema);
		dtype.load(ordtel);
		if (StringUtil.isNotEmpty(dtype.id))
			this..put(dtype.iddtype);
		return dtype;
	}
	/*
	 * TODO 
	 * 
	 * @param name
	 * @param mr
	 * @return
	 */
		List<DataTypeld = new ArrayList<DataType>();
		if (name.contains(":")) {
			String[] parts = name.split(":");
			DataType t1 = this..get(parts[0]);
			if (t1 == null
				return ld;
			t1.compile(mr);
			if (t1.fields == null)
				return ld;
			Field f1 = t1.fields.get(parts[1]);
			if (f1 == null
				return ld;
			return f1.options;
		}
		DataType d4 = this..get(name);
		if (d4 != null)
			ld.add(d4);
		return ld;
	}
New to GrepCode? Check out our FAQ X