home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / flow.js < prev    next >
Encoding:
JavaScript  |  2004-07-12  |  5.4 KB  |  177 lines

  1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *     http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. var repo = new Packages.org.apache.cocoon.components.Repository.getInstance();
  17.  
  18. var users;
  19. var home;
  20. var base;
  21. var userid = "";
  22. var username = "";
  23.  
  24. /*
  25.  * Main entry point for the flow. This is where user authorization takes place.
  26.  */
  27. function main(action,root,baseURL) {
  28.      home = root + "/";
  29.       base = baseURL;
  30.  
  31.     var args = new Array(arguments.length - 3);
  32.     for (var i = 3; i < arguments.length; i++) {
  33.         args[i-3] = arguments[i];
  34.     }            
  35.  
  36.     if ((userid == undefined) || (userid == "")) {
  37.         login(action, args);
  38.     }
  39.                 
  40.     invoke(action, args);
  41. }
  42.  
  43. /*
  44.  * If the user is not yet authorized, than authentication takes place
  45.  */
  46. function login(action, args) {
  47.     var name = "";
  48.     var password = "";
  49.     var userError = "";
  50.     var passError = "";
  51.  
  52.     while (true) {
  53.         cocoon.sendPageAndWait("screen/login", { base : base, username : name, userError : userError, passError : passError});
  54.  
  55.         name = cocoon.request.getParameter("username");
  56.         password = cocoon.request.getParameter("password");
  57.         
  58.         if (users == undefined) {
  59.             var stream = new java.io.FileInputStream(home + "linotype.users.properties");
  60.             users = new Packages.org.apache.cocoon.components.UserManager.getInstance(stream);
  61.         }
  62.                 
  63.         if (users.isValidName(name)) {
  64.             if (users.isValidPassword(name,password)) {
  65.                 userid = name;
  66.                 username = users.getFullName(name);
  67.                 break;
  68.             } else {
  69.                 userError = "";
  70.                 passError = "Password doesn't match";
  71.             }
  72.         } else {
  73.             userError = "User not found";
  74.             passError = "";
  75.         }
  76.     }
  77. }
  78.  
  79. /*
  80.  * Now that the user has been authenticated and authorized, execute what 
  81.  * he's asking for. This method checks the flowscript to see if the
  82.  * called action exists as a flowscript function. If so, it's called with
  83.  * the given arguments. If not, the appropriate admin screen is sent
  84.  * to the user.
  85.  */
  86. function invoke(action, args) {
  87.     func = this[action];
  88.     if (func != undefined) {
  89.         func.apply(this,args);
  90.     } else {
  91.         cocoon.sendPage("screen/" + action, { base : base, user : username});
  92.     }
  93. }
  94.  
  95. // ----------------------------- actions ----------------------------------
  96.  
  97. /*
  98.  * The logout action clears the userid from the session thus signaling
  99.  * that the user has logged out and should be further considered authenticated. 
  100.  */
  101. function logout() {
  102.     userid = "";
  103.     cocoon.sendPage("screen/logout", { base : base });
  104. }
  105.    
  106. /*
  107.  * The edit action performs the editing subflow.
  108.  */
  109. function edit(id,type,subpage) {
  110.     var repository = home + "repository/" + type + "/";
  111.  
  112.     if (id == "template") {
  113.         id += "-" + getID(repository);
  114.         repo.copy(repository + "template", repository + id);
  115.         cocoon.redirectTo("../" + id + "/");
  116.     } else if ((subpage != undefined) && (subpage != "")) {
  117.         cocoon.sendPage("edit/" + type + "/" + id + "/" + subpage,{});
  118.     } else {
  119.         var document = repository + id;
  120.  
  121.         while (true) {
  122.             var versions = repo.getVersions(document);
  123.             cocoon.sendPageAndWait("edit/" + type + "/" + id + "/", { 
  124.                 userid : userid, 
  125.                 username : username, 
  126.                 versions : versions,
  127.                 innerHTML : cocoon.request.getParameter("innerHTML") 
  128.             });
  129.             var action = cocoon.request.getParameter("action");
  130.             if (action == "delete") {
  131.                 repo.remove(document);
  132.                 break;
  133.             } else if (action == "restore") {
  134.                 var version = cocoon.request.getParameter("version");
  135.                 repo.revertFrom(document,version);
  136.             } else {
  137.                 var output = repo.getOutputStream(document);
  138.                 cocoon.processPipelineTo("action/save-" + type,{},output);
  139.                 output.close();
  140.                 repo.fomSave(cocoon, document);
  141.                 if (action == "finish") {
  142.                     break;
  143.                 }
  144.                 if (action == "publish") {
  145.                     if (id.indexOf("template-") > -1) {
  146.                         var realID = id.substring(id.indexOf("-") + 1);
  147.                         repo.copy(document, repository + realID);
  148.                         repo.remove(document);
  149.                     }
  150.                     break;
  151.                 }
  152.             }                   
  153.         }
  154.  
  155.         cocoon.redirectTo("../../../" + type);
  156.     }
  157. }
  158.  
  159. function getID(repository) {
  160.  
  161.     var dirs = new java.io.File(repository).listFiles();
  162.     var id = 0;
  163.  
  164.     for (i = 0; i < dirs.length; i++) {
  165.         if (dirs[i].isDirectory()) {
  166.             var name = dirs[i].getName();
  167.             if (name.indexOf("template-") > -1) {
  168.                 name = name.substring(name.indexOf("-") + 1);
  169.             }
  170.             var localid = parseInt(name);
  171.             if (localid > id) id = localid;
  172.         }
  173.     }
  174.     
  175.     return ++id;
  176. }
  177.