| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- /* Copyright (c) Business Objects 2006. All rights reserved. */
- if (typeof bobj.crv.params == 'undefined') {
- bobj.crv.params = {};
-
- bobj.crv.params.DataTypes = {
- DATE: "d",
- DATE_TIME: "dt",
- TIME: "t",
- STRING: "s",
- NUMBER: "n",
- CURRENCY: "c",
- BOOLEAN: "b"
- };
-
- bobj.crv.params.RangeBoundTypes = {
- UNBOUNDED: 0,
- EXCLUSIVE: 1,
- INCLUSIVE: 2
- };
-
- bobj.crv.params.DefaultDisplayTypes = {
- Description: 0,
- DescriptionAndValue: 1
- };
-
- bobj.crv.params.CompareResults = {
- TOO_BIG: 1,
- TOO_SMALL: -1,
- EQUAL: 0
- };
- }
- /*
- ================================================================================
- Parameter
- ================================================================================
- */
- /**
- * Constructor.
- */
- bobj.crv.params.Parameter = function(paramInfo) {
- var PARAMS = bobj.crv.params;
- var displayTypes = PARAMS.DefaultDisplayTypes;
- MochiKit.Base.update(this, {
- paramName: null,
- reportName: null,
- description: null,
- valueDataType: null,
- value: null,
- modifiedValue: null,
- defaultValues: null,
- defaultDisplayType : displayTypes.DescriptionAndValue,
- maxValue: null,
- minValue: null,
- allowCustomValue: true,
- allowDiscreteValue: true,
- allowMultiValue: false,
- allowNullValue: false,
- allowRangeValue: false,
- editMask: null,
- isOptionalPrompt: false,
- isEditable: true,
- isHidden: false,
- isDataFetching: false,
- attributes: null
- }, paramInfo);
-
- this.valueCounter = new PARAMS.ValueCounter(this.value, this.valueDataType);
- };
- /**
- * IMPORTANT! Use getValue and setValue to access the value of the parameter, instead of the property itself.
- */
- bobj.crv.params.Parameter.prototype = {
- getTitle: function() {
- return (this.description || this.paramName);
- },
-
- hasLOV: function() {
- return (this.defaultValues && this.defaultValues.length);
- },
-
- isPassword: function() {
- return (this.editMask !== null && this.editMask.toLowerCase() == "password");
- },
-
- getValue: function() {
- this._initModifiedValue();
- return this.modifiedValue;
- },
-
- removeValueAt: function(index) {
- this._initModifiedValue();
- var value = this.modifiedValue[index];
- this.modifiedValue.splice(index, 1);
- this.valueCounter.reduceCount(value);
- },
-
- // setValue accepts either an array, or an int + an object.
- setValue: function (i, newValue) {
- this._initModifiedValue();
-
- if (arguments.length == 1 && bobj.isArray(arguments[0])) {
- var value= arguments[0];
- this.modifiedValue = value;
- this.valueCounter.resetValue(value);
- }
- else if (arguments.length == 2) {
- var oldValue = this.modifiedValue[i];
- this.modifiedValue[i] = newValue;
- this.valueCounter.reduceCount(oldValue);
- this.valueCounter.addCount(newValue);
- }
- },
-
- clearValue: function() {
- this._initModifiedValue();
- this.modifiedValue = [];
- },
-
- // commitValue will actually apply the changes made so far
- commitValue: function() {
- this._initModifiedValue();
- this.value = this.modifiedValue.slice(0);
- },
-
- _initModifiedValue: function() {
- if (!this.modifiedValue) {
- if (bobj.isArray(this.value)) {
- this.modifiedValue = this.value.slice(0); // make a deep copy of "value"
- }
- else {
- this.modifiedValue = [];
- }
- }
- },
-
- addDuplicateValuesCB: function(hasDuplicateCB,hasNoDuplicateCB) {
- this.valueCounter.addCallBacks(hasDuplicateCB,hasNoDuplicateCB);
- },
-
- isDCP: function () {
- if (this.attributes != null) {
- if (this.attributes['IsDCP'] === true) {
- return true;
- }
- }
-
- return false;
- }
- };
- /*
- ================================================================================
- Validator for Parameters
- ================================================================================
- */
- bobj.crv.params.Validator = function(){};
- bobj.crv.params.Validator.ValueStatus = {
- OK: 0,
- ERROR: 1,
- VALUE_MISSING: 2, // Required value is missing
- VALUE_INVALID_TYPE: 3, // Value has the wrong data type
- VALUE_TOO_LONG: 4, // Value's length is less than the minimum
- VALUE_TOO_SHORT: 5, // Value's length is greater than the maximum
- VALUE_TOO_BIG: 6, // Value is greater than the maximum
- VALUE_TOO_SMALL: 7, // Value is less than the minimum
- VALUE_DUPLICATE: 8
- };
- bobj.crv.params.Validator.getInstance = function(){
- if (!bobj.crv.params.Validator.__instance) {
- bobj.crv.params.Validator.__instance = new bobj.crv.params.Validator();
- }
- return bobj.crv.params.Validator.__instance;
- };
-
- bobj.crv.params.Validator.prototype = {
- /**
- * Validate a Parameter instance and return a status object
- */
- validateParameter: function(param) {
- var PARAMS = bobj.crv.params;
- if (!param) {return null;}
-
- var Status = PARAMS.Validator.ValueStatus;
-
- if (!bobj.isArray(param.value) || !param.value.length) {
- return {
- isValid: false,
- reason: Status.VALUE_MISSING
- };
- }
-
- var isValid = true;
- var statusList = [];
-
- for (var i = 0, len = param.values.length; i < len; ++i) {
- var status = PARAMS.validateValue(param, i);
- statusList.push(status);
- isValid = isValid && (status === ValueStatus.OK);
- }
-
- return {
- isValid: isValid,
- statusList: statusList
- };
-
- },
-
- /**
- * Validate a parameter value and return a status code
- */
- validateValue: function(param, value) {
- var Status = bobj.crv.params.Validator.ValueStatus;
-
- if (!param || !bobj.isArray(param.value) || (value === undefined)) {
- return Status.ERROR;
- }
-
- var validatorFunc = this._getTypeValidatorFunc(param.valueDataType);
- if (!validatorFunc) {
- return Status.ERROR;
- }
-
- return validatorFunc(param, value);
- },
-
- _getTypeValidatorFunc: function(type) {
- var Type = bobj.crv.params.DataTypes;
-
- switch (type) {
- case Type.STRING:
- return this._validateString;
- case Type.NUMBER:
- case Type.CURRENCY:
- return this._validateNumber;
- case Type.DATE:
- case Type.TIME:
- case Type.DATE_TIME:
- return this._validateDateTime;
- case Type.BOOLEAN:
- return this._validateBoolean;
- default:
- return null;
- }
- },
-
- _validateString: function(param, value) {
- var Status = bobj.crv.params.Validator.ValueStatus;
-
- if (!bobj.isString(value)) {
- return Status.VALUE_INVALID_TYPE;
- }
-
- var maxValue = param.maxValue;
- var minValue = param.minValue;
- if (bobj.isNumber(maxValue) && value.length > maxValue) {
- return Status.VALUE_TOO_LONG;
- }
-
- if (bobj.isNumber(minValue) && value.length < minValue) {
- return Status.VALUE_TOO_SHORT;
- }
-
- return Status.OK;
- },
-
- _validateNumber: function(param, value) {
- //the "value" passed in here is a number that has at most one decimal separator and no group separator
- var Status = bobj.crv.params.Validator.ValueStatus;
- var regNumber = /^(\+|-)?(\d+(\.\d+)?|\.\d+)$/;
-
- if(bobj.isString(value) && regNumber.test(value)) {
- value = parseFloat(value);
- }
- else if(!bobj.isNumber(value)) {
- return Status.VALUE_INVALID_TYPE;
- }
-
- var maxValue = param.maxValue;
- var minValue = param.minValue;
- if(maxValue !== null && value > maxValue) {
- return Status.VALUE_TOO_BIG;
- }
- else if(minValue !== null && value < minValue) {
- return Status.VALUE_TOO_SMALL;
- }
- else {
- return Status.OK;
- }
- },
-
- _validateDateTime: function(param, value) {
- var Result = bobj.crv.params.CompareResults;
- var Status = bobj.crv.params.Validator.ValueStatus;
- if (bobj.isObject(value)) {
- var isNumber = function(sel) {return bobj.isNumber(value[sel]);};
- if (MochiKit.Iter.every(['d','m', 'y', 'h', 'min', 's', 'ms'], isNumber)) {
-
- var compareFunc = bobj.crv.params.getDateCompareFunc(param.valueDataType);
- if(param.minValue && compareFunc(param.minValue,value) == Result.TOO_BIG) {
- return Status.VALUE_TOO_SMALL;
- }
- else if(param.maxValue && compareFunc(param.maxValue,value) == Result.TOO_SMALL) {
- return Status.VALUE_TOO_BIG;
- }
- else {
- return Status.OK;
- }
- }
- }
-
- return Status.VALUE_INVALID_TYPE;
- },
-
- _validateBoolean: function(param, value) {
- return bobj.crv.params.Validator.ValueStatus.OK;
- }
- };
- /*
- ================================================================================
- Utility Functions
- ================================================================================
- */
- /**
- * Get an object that represents a Date and can be serialized to a json string
- *
- * @param date [Date] The Date instance that should be represented as json
- *
- * @return [Object] Object representing the date with (key, value) pairs
- */
- bobj.crv.params.dateToJson = function(date) {
- return {
- d: date.getDate(),
- m: date.getMonth(),
- y: date.getFullYear(),
- h: date.getHours(),
- min: date.getMinutes(),
- s: date.getSeconds(),
- ms: date.getMilliseconds()
- };
- };
- bobj.crv.params.getDateCompareFunc = function(type) {
- var PARAMS = bobj.crv.params;
- var Type = PARAMS.DataTypes;
-
- switch (type) {
- case Type.DATE:
- return PARAMS.compareDate;
- case Type.TIME:
- return PARAMS.compareTime;
- case Type.DATE_TIME:
- return PARAMS.compareDateTime;
- default:
- return null;
- }
- };
- bobj.crv.params.compareDateTime = function(dateTimeA, dateTimeB) {
- var PARAMS = bobj.crv.params;
- var Result = PARAMS.CompareResults;
-
- var dateResult = PARAMS.compareDate(dateTimeA,dateTimeB);
- var timeResult = PARAMS.compareTime(dateTimeA,dateTimeB);
-
- if(dateResult == Result.EQUAL && timeResult == Result.EQUAL) {
- return Result.EQUAL;
- }
-
- if(dateResult != Result.EQUAL) {
- return dateResult;
- }
- else {
- return timeResult;
- }
- };
- /*
- * Compares two dates
- * @param dateA [JSON DateTime {y,m,d,h,m,s,ms}] first date value
- * @param dateB [JSON DateTime {y,m,d,h,m,s,ms}] second date value
- *
- * @return
- * 0: dateA = dateB
- * 1: dateA > dateB
- * -1: dateA < dateB
- */
- bobj.crv.params.compareDate = function(dateTimeA, dateTimeB) {
- var Result = bobj.crv.params.CompareResults;
-
- if( dateTimeA.d == dateTimeB.d && dateTimeA.m == dateTimeB.m && dateTimeA.y == dateTimeB.y){
- return Result.EQUAL;
- }
-
- if( dateTimeA.y > dateTimeB.y) {
- return Result.TOO_BIG;
- }
- else if(dateTimeA.y < dateTimeB.y) {
- return Result.TOO_SMALL;
- }
-
- if(dateTimeA.m > dateTimeB.m) {
- return Result.TOO_BIG;
- }
- else if(dateTimeA.m < dateTimeB.m) {
- return Result.TOO_SMALL;
- }
-
- if(dateTimeA.d > dateTimeB.d) {
- return Result.TOO_BIG;
- }
- else if(dateTimeA.d < dateTimeB.d) {
- return Result.TOO_SMALL;
- }
- };
- /*
- * Compares two times
- * @param timeA [JSON DateTime {y,m,d,h,m,s,ms}] first time value
- * @param timeB [JSON DateTime {y,m,d,h,m,s,ms}] second time value
- *
- * @return
- * 0: dateA = dateB
- * 1: dateA > dateB
- * -1: dateA < dateB
- */
- bobj.crv.params.compareTime = function(dateTimeA,dateTimeB) {
- var Result = bobj.crv.params.CompareResults;
-
- if(dateTimeA.h == dateTimeB.h && dateTimeA.min == dateTimeB.min && dateTimeA.s == dateTimeB.s && dateTimeA.ms == dateTimeB.ms) {
- return Result.EQUAL;
- }
-
- if(dateTimeA.h > dateTimeB.h) {
- return Result.TOO_BIG;
- }
- else if(dateTimeA.h < dateTimeB.h){
- return Result.TOO_SMALL;
- }
-
- if(dateTimeA.min > dateTimeB.min) {
- return Result.TOO_BIG;
- }
- else if(dateTimeA.min < dateTimeB.min) {
- return Result.TOO_SMALL;
- }
-
- if(dateTimeA.s > dateTimeB.s) {
- return Result.TOO_BIG;
- }
- else if(dateTimeA.s < dateTimeB.s) {
- return Result.TOO_SMALL;
- }
-
- if(dateTimeA.ms > dateTimeB.ms) {
- return Result.TOO_BIG;
- }
- else if(dateTimeA.ms < dateTimeB.ms){
- return Result.TOO_SMALL;
- }
- };
- /**
- * Get a Date instance from an object containing (key, value) pairs
- *
- * @param json [Object] Object with keys that match Date properties
- *
- * @return [Date] Returns a Date instance
- */
- bobj.crv.params.jsonToDate = function(json) {
- var date = new Date();
-
- if (json) {
- date.setFullYear(json.y || 0, json.m || 0, json.d || 1);
- date.setHours(json.h || 0);
- date.setMinutes(json.min || 0);
- date.setSeconds(json.s || 0);
- date.setMilliseconds(json.ms || 0);
- }
-
- return date;
- };
- bobj.crv.params.ValueCounter = function(initialValue,valueDataType) {
- this.values = {};
- this.valueDataType = valueDataType;
- this.valueHasDupCB = null;
- this.valueNoDupCB = null;
- this.resetValue(initialValue);
- };
- bobj.crv.params.ValueCounter.prototype = {
-
- addCallBacks: function(valueHasDupCB, valueNoDupCB) {
- this.valueHasDupCB = valueHasDupCB;
- this.valueNoDupCB = valueNoDupCB;
- },
-
- addCount : function(value) {
- if(value !== null && value !== undefined && value.beginValue === undefined) {
- var hashValue = bobj.getValueHashCode(this.valueDataType, value);
- if(this.values[hashValue] !== null && this.values[hashValue] !== undefined) {
- this.values[hashValue] += 1;
- if(this.valueHasDupCB && this.values[hashValue] > 1) {
- this.valueHasDupCB(value);
- }
- }
- else {
- this.values[hashValue] = 1;
- }
- }
- },
-
- reduceCount : function(value) {
- if(value !== null && value !== undefined && value.beginValue === undefined) {
- var hashValue = bobj.getValueHashCode(this.valueDataType, value);
- this.values[hashValue] -= 1;
- if(this.valueNoDupCB && this.values[hashValue] == 1) {
- this.valueNoDupCB(value);
- }
-
- }
- },
-
- resetValue : function(value) {
- if(this.valueNoDupCB) {
- this.valueNoDupCB(); // Removes all duplicate values warning when no value is passed to CB
- }
- for(var k in this.values) {
- if(this.values[k] > 0) {
- this.values[k] = 0;
- }
- }
- if(bobj.isArray(value)) {
- for(var i = 0, len = value.length; i < len; i++) {
- this.addCount(value[i]);
- }
- }
- else {
- this.addCount(value);
- }
- }
- };
|