<%@ LANGUAGE = JScript %> <% // Copyright (c) 1999-2000 by Fragment Art & Research, Inc, All Rights Reserved // Processes parameters for each form (for profile entry and update), applies those parameters to the DOM tree and generates next form. var DEBUG_IdentityXSL = false; var DEBUG_AllLanguages = false; var DEBUG_WriteFormParams = false; var DEBUG_WriteKeyValues = false; /* The function getDefaultElement gets default elements and values for potentially missing elements in the DOM tree returned from the database. An element is missing when no entry exists in database. */ function getDefaultElement(elementName, lang){ var defaultElement = Server.CreateObject("Microsoft.XMLDOM"); if (elementName == "languageSkills"){ defaultElement.loadXML(""); }else if (elementName == "references"){ defaultElement.loadXML(""); }else if (elementName == "location"){ defaultElement.loadXML(""); }else if (elementName == "jobSkill"){ defaultElement.loadXML("..."); }else if (elementName == "erp"){ defaultElement.loadXML(""); }else if (elementName == "name-languageSpecificInfo"){ defaultElement.loadXML(""); }else if (elementName == "contact-languageSpecificInfo"){ defaultElement.loadXML(""); }else if (elementName == "personal-languageSpecificInfo"){ defaultElement.loadXML(""); }else if (elementName == "location-languageSpecificInfo"){ defaultElement.loadXML(""); }else if (elementName == "job-languageSpecificInfo"){ defaultElement.loadXML(""); }else if (elementName == "education-languageSpecificInfo"){ defaultElement.loadXML(""); }else if (elementName == "project-languageSpecificInfo"){ defaultElement.loadXML(""); }else if (elementName == "jobSkill-languageSpecificInfo"){ defaultElement.loadXML(""); }else if (elementName == "new-job"){ defaultElement.loadXML(""); }else if (elementName == "new-educationElement"){ defaultElement.loadXML(""); }else if (elementName == "new-project"){ defaultElement.loadXML(""); }else{ Response.Write("BUG: can not find default structure for: " + elementName); defaultElement.loadXML(""); } return defaultElement; } /* This function converts form fields into values in the dom tree. This is done by assuming that the field name is a path into the DOM after some massaging: - underscores are turned into slashes - languageInfo and consultant are added - if a key ends in 0 the value is used to delete that subtree first (e.g. _languageSkills_0 -> everything below will be deleted) - if a collection key is required (it ends in @) its value must be not null and a new record will be generated - if a collection key is not required and the node does not exist, the value will be ignored - if a key ends in ! the ! is removed and the value assigned, this is used for checkbox values, where the form value will always be preceeded by the default reset value. - if the key ends in a ':' the ':' is removed and it is checked if the key element exists. If not, the value points as a key to a default XML structure that will be added to the but-last element pointed to by the original key. (last slash) - if the key ends in a '-' the '-' is removed and the value is taken as an attribute name, which will be removed from the element that is pointed to by the key. - if the key ends in a '.' the value is taken to be a date in yyyy-mm format, and '-01' is added. - if the key is pointing to an attribute that does not exist yet, it will be added before the value is assigned. */ function applyFormToConsultant(consultant, lang, consultantID){ var langSpecific = "languageSpecificInfo[@lang='" + lang +"']/"; var consultantPrefix = "consultant[@id='" + consultantID + "']/"; var re = /_[A-Za-z0-9_%@.-]+=/g var m; var j; var s = new String(Request.Form); var runder; var deletions = new Array(); var currentStructure; var currentStructureKey; var smallKey; if (DEBUG_WriteFormParams) Response.Write(s + "\n"); if ((m = s.match(re)) != null){ for (j = 0; j < m.length; j++){ var key = m[j].substr(0, m[j].length-1); runder = /%5B/g key = key.replace(runder, "["); runder = /%5D/g key = key.replace(runder, "]"); runder = /%5E/g key = key.replace(runder, "^"); runder = /%3D/g key = key.replace(runder, "="); runder = /%27/g key = key.replace(runder, "'"); runder = /%3A/g key = key.replace(runder, ":"); runder = /%40/g key = key.replace(runder, "@"); runder = /%21/g key = key.replace(runder, "!"); // setting the default for a checkbox value var value = Request.Form(key); if (value){// this is a real key-value pair // trim the key into a path key = key.substr(1); runder = /_/g key = key.replace(runder, "/"); smallKey = "" + key; runder = /languageSpecificInfo\//g key = consultantPrefix + key.replace(runder, langSpecific); // this way we do allow lsi[2] if (key.charAt(key.length-1) == "0"){ // delete all children, this is the structure marker key = key.substr(0, key.length-2); currentStructureKey = key; currentStructure = consultant.selectSingleNode(currentStructureKey); var i; for (i = currentStructure.childNodes.length; i>0; i--){ currentStructure.removeChild(currentStructure.childNodes.item(0)); } currentStructureKey = currentStructureKey.substring( currentStructureKey.lastIndexOf('/') + 1); }else{ if (key.charAt(key.length-1) == "@"){ key = key.substr(0, key.length-1); // always create a new record in the current structure // later this should come from some defaults file // indexed by currentStructureKey var defaultElement = getDefaultElement(currentStructureKey, lang); // Response.Write("new: " + currentStructureKey + "\n"); currentStructure.appendChild(defaultElement.documentElement); // if (value == "") { // if the value does not exist, add to delete paths // find the last [i] from the back var lqi = key.lastIndexOf("]"); var deleteKey = key.substr(0, lqi+1); deletions[deletions.length] = deleteKey; continue; } } else if (key.charAt(key.length-1) == "!"){ // deal with checkbox default values key = key.substr(0, key.length-1); } else if (key.charAt(key.length-1) == "."){ // deal with yyyy-mm value key = key.substr(0, key.length-1); if (value != ""){ value += '-01'; } } else if (key.charAt(key.length-1) == "-"){ // remove attribute by treating the key // as a selector into the consultant and the value as the attribute name to be removed key = key.substr(0, key.length-1); if (value){ var node = consultant.selectSingleNode(key); node.removeAttribute(value); if (DEBUG_WriteKeyValues) Response.Write("removed attribute: " + value + " from: " + key); }else{ if (DEBUG_WriteKeyValues) Response.Write("could not remove attribute: " + value + " from: " + key); } continue; // deal with next form key } else if (key.charAt(key.length-1) == ":"){ // deal with required default structures // trim key and check if it exists key = key.substr(0, key.length-1); var node = consultant.selectSingleNode(key); if (DEBUG_WriteKeyValues) Response.Write("required default structure: " + key + " " + value + "\n"); if (node == null){ // create default structure var defaultElement = getDefaultElement(value, lang); var parentKey = key.substring(0, key.lastIndexOf('/')); var parentNode = consultant.selectSingleNode(parentKey); if (DEBUG_WriteKeyValues) Response.Write("parentKey: " + parentKey); if (parentNode != null){ parentNode.appendChild(defaultElement.documentElement); if (DEBUG_WriteKeyValues) Response.Write(" added default structure: " + value + "\n"); } } continue; // deal with next form key } else if (key.charAt(key.length-1) == "^"){ // either this is a reference from a job or an education element to a project... key = key.substr(0, key.length-1); value = value.item; var projectID; var fromID; // value is projectID[:fromID] if (value.lastIndexOf(":") == -1){//only project id projectID = value; fromID = ""; }else{ projectID = value.substr(0, value.lastIndexOf(":")); fromID = value.substr(value.lastIndexOf(":")+1); } // Response.Write("ref to project from: " + key + " project id: " + projectID + " ref id: " + fromID); var projectRefNode; var newProjectsNode; if (smallKey == "educationElement^"){ // if this was in an education element before, delete it projectRefNode = consultant.selectSingleNode(consultantPrefix + "education/educationElement/projects/projectReference[@id='"+ projectID + "']"); newProjectsNode = consultant.selectSingleNode(consultantPrefix + "education/educationElement[@id='" + fromID + "']/projects"); }else if (smallKey == "job^"){ // if this was in a job element before, delete it projectRefNode = consultant.selectSingleNode(consultantPrefix + "jobs/job/projects/projectReference[@id='"+ projectID + "']"); newProjectsNode = consultant.selectSingleNode(consultantPrefix + "jobs/job[@id='" + fromID + "']/projects"); } if (projectRefNode != null){// was in job/education element before projectRefNode.parentNode.removeChild(projectRefNode); if (fromID != "") newProjectsNode.appendChild(projectRefNode); }else{// was not in an job/education element before, create new reference unless fromID == "" if (fromID != ""){ var newProjectRefNode = consultant.createElement("projectReference"); newProjectRefNode.setAttribute("id", projectID); newProjectsNode.appendChild(newProjectRefNode); } } continue; } if (DEBUG_WriteKeyValues) Response.Write("key:" + key); var node = consultant.selectSingleNode(key); if (node != null){ if (DEBUG_WriteKeyValues) Response.Write(" value: " + value + " \n"); node.text = value; // note, this is wrong for memo types where html can be inside } else{ // check if this is an attribute, if so add it and then set the value. var attributeIndex = key.lastIndexOf("/@"); if (attributeIndex == -1){ if (DEBUG_WriteKeyValues) Response.Write(" no node found" + "\n"); }else{ var keyPrefix = key.substr(0, attributeIndex); var attributeName = key.substr(attributeIndex + 2, key.length); if (attributeName.indexOf("/") == -1){// some simple test that this is really an attribute name // find the node node = consultant.selectSingleNode(keyPrefix); if (node != null){// node really exists // add the attribute value node.setAttribute(attributeName, value); if (DEBUG_WriteKeyValues) Response.Write(" added attribute: " + attributeName + " to: " + keyPrefix + " value: " + value + "\n"); } } } } } } } for (j = deletions.length -1; j >= 0; j--){ // find the node for each deletion and remove it from the tree... var child = consultant.selectSingleNode(deletions[j]); if (child != null){ child.parentNode.removeChild(child); } } } } var paramLang; var actionCancel; var actionSaveContinue; var actionSaveOtherContinue; var paramSaveOtherContinue; var actionSave; var actionAddReference; var actionAddLocation; var actionAddJobSkill; var actionEditPart; var actionDeletePart; var actionEditDetailsSAP; var actionEditDetailsBaan; var paramNextStep; var paramCurrentStep; var paramJobSkill; var paramPartID; var paramPartType; var styleSheet; // check first if user logged in already... // if not redirect to login page, carry the arguments along. Response.Buffer=true; var sessionID = Request.Cookies("sic-user"); if (sessionID == "0" || sessionID == ""){ // user has not registered yet, go to login script Response.Redirect("login.asp"); } var profiles = openDB(); var loginName = getLoginNameFromSessionID(sessionID, profiles); var consultantIDs = getConsultantsForAccount(loginName, profiles); var consultantID; if (consultantIDs.size == 0){ consultantID = null; }else{ consultantID = consultantIDs[0]; } // if logged in process the form... // var consultantID = Request.Form("consultantID").item; if (consultantID == null){ // this needs to say that there is no consultant for this account... consultantID = 1; // this is wrong cancel= true; actionSaveContinue = false; paramLang = Request.QueryString("param-lang").item; paramNextStep = Request.QueryString("param-nextStep").item; paramCurrentStep= Request.QueryString("param-currentStep").item; if (paramLang == null) paramLang = "EN"; if (!paramNextStep) paramNextStep="showPersonalData"; if (!paramCurrentStep) paramCurrentStep="showPersonalData"; }else{ consultantID = parseInt(consultantID); paramLang = Request.Form("param-lang").item; if (paramLang == null) { paramLang = Request.QueryString("param-lang").item; if (paramLang == null) paramLang = 'EN'; } actionCancel = (Request.Form("action-cancel").item) != null; actionSaveContinue = (Request.Form("action-save-continue").item) != null; actionSaveOtherContinue = (Request.Form("action-save-other-continue").item) != null; paramSaveOtherContinue = Request.Form("param-save-other-continue").item; actionSave = (Request.Form("action-save").item) != null; actionAddReference = (Request.Form("action-add-reference").item) != null; actionAddLocation = (Request.Form("action-add-location").item) != null; actionAddJobSkill = (Request.Form("action-add-jobSkill").item) != null; actionEditPart = (Request.Form("action-edit-part").item) != null; actionDeletePart = (Request.Form("action-delete-part").item) != null; actionEditDetailsSAP = (Request.Form("action-edit-details-SAP").item) != null; actionEditDetailsBaan = (Request.Form("action-edit-details-BAAN").item) != null; paramJobSkill = Request.Form("param-jobSkill").item; paramPartID = Request.Form("paramPartID").item; // funny name because of netscape name == netscape ID paramPartType = Request.Form("paramPartType").item; // funny name because of netscape name == netscape ID paramNextStep = Request.Form("param-nextStep").item; paramCurrentStep= Request.Form("param-currentStep").item; if (!paramNextStep) paramNextStep="showPersonalData"; if (!paramCurrentStep) paramCurrentStep="showPersonalData"; } Response.ContentType="text/html"; // Response.Write("lang: " + paramLang + " next: " + paramNextStep + " current: " + paramCurrentStep + "\n"); // try{ var c; if (DEBUG_AllLanguages){ c = getConsultantDOM(consultantID, "*", profiles); }else{ c = getConsultantDOM(consultantID, paramLang, profiles); } if (actionSaveContinue | actionSave | actionSaveOtherContinue | actionAddReference | actionAddLocation | actionAddJobSkill | actionEditDetailsSAP | actionEditDetailsBaan){ applyFormToConsultant(c, paramLang, consultantID); if (actionAddReference){ var references = c.selectSingleNode("consultant[@id='" + consultantID + "']/references"); var defaultElement = getDefaultElement("references", paramLang); references.appendChild(defaultElement.documentElement); styleSheet = paramCurrentStep; }else if (actionAddLocation){ var locations = c.selectSingleNode("consultant[@id='" + consultantID + "']/locations/preferredLocations"); var defaultElement = getDefaultElement("location", paramLang); locations.appendChild(defaultElement.documentElement); styleSheet = paramCurrentStep; }else if (actionAddJobSkill){ var skills = c.selectSingleNode("consultant[@id='" + consultantID + "']/jobSkills/generalSkills"); var defaultElement = getDefaultElement("jobSkill", paramLang); var elem = defaultElement.selectSingleNode("//jobSkill"); elem.setAttribute("value", paramJobSkill); skills.appendChild(defaultElement.documentElement); styleSheet = paramCurrentStep; }else if (actionEditDetailsSAP){ styleSheet = "skills-sap"; }else if (actionEditDetailsBaan){ styleSheet = "skills-baan"; }else if (actionSave){ styleSheet = paramCurrentStep; }else if (actionSaveOtherContinue){ styleSheet = paramSaveOtherContinue; }else{ styleSheet = paramNextStep; } try{ updateConsultantFromDOM(c, profiles); }catch (e){ Response.Write("Internal error updating the database: " + e.description); } }else if(actionDeletePart){ var part = c.selectSingleNode("consultant[@id='" + consultantID + "']//" + paramPartType + "[@id='" + paramPartID + "']"); if (part != null){ part.parentNode.removeChild(part); paramPartID = null; updateConsultantFromDOM(c, profiles); }else{ if (DEBUG_WriteKeyValues) Response.Write(" could not delete: " + paramPartType + " id: " + paramPartID + "\n"); } styleSheet = paramCurrentStep; }else{ // Do not apply nor save, NOTE, actionEditPart is in here... // else just get all the values and display again... styleSheet = paramCurrentStep; } profiles.Close(); var top = Server.CreateObject("Microsoft.XMLDOM"); top.async=false; var xProfiles = top.createElement("xProfiles"); top.insertBefore(xProfiles, null); var translations = Server.CreateObject("Microsoft.XMLDOM"); translations.async=false; translations.load(Server.MapPath("translations\\" + paramLang + ".xml")); var params = top.createElement("parameters"); params.setAttribute("lang", paramLang); if (paramPartID){ params.setAttribute("part", paramPartID); } xProfiles.appendChild(translations.selectSingleNode("translations[@lang='" + paramLang + "']")); xProfiles.appendChild(params); xProfiles.appendChild(c.childNodes.item(0)); var style = Server.CreateObject("Microsoft.XMLDOM"); style.async=false; if (DEBUG_IdentityXSL | (styleSheet == null)){ style.load(Server.MapPath("styles\\defaultss.xsl")); }else{ style.load(Server.MapPath("styles\\" +styleSheet + ".xsl")); } if (style.parseError.errorCode != 0){ Response.Write("PARSE ERROR: " + style.parseError); }else{ try { var r = top.transformNode(style); Response.Write(r); }catch (exception) { Response.Write("error: " + exception.description); // result = reportRuntimeError(exception); } } // since we are buffered (for redirecting) flush here... /* }catch(exception){ try{profiles.close();}catch(exception){}; Response.Write("error: " + exception.description); Response.Flush(); } */ %>