Coverage Report - org.melati.admin.AdminUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
AdminUtils
94%
117/124
89%
52/58
1.64
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/admin/AdminUtils.java,v $
 3  
  * $Revision: 1.91 $
 4  
  *
 5  
  * Copyright (C) 2000 William Chesters
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     William Chesters <williamc At paneris.org>
 42  
  *     http://paneris.org/~williamc
 43  
  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
 44  
  */
 45  
 package org.melati.admin;
 46  
 
 47  
 
 48  
 import java.util.Enumeration;
 49  
 import java.util.Hashtable;
 50  
 
 51  
 import org.melati.Melati;
 52  
 import org.melati.poem.AccessPoemException;
 53  
 import org.melati.poem.Field;
 54  
 import org.melati.poem.Persistent;
 55  
 import org.melati.poem.Table;
 56  
 import org.melati.poem.Treeable;
 57  
 import org.melati.poem.util.ArrayUtils;
 58  
 import org.melati.poem.util.StringUtils;
 59  
 import org.melati.template.MarkupLanguage;
 60  
 import org.melati.util.JSStaticTree;
 61  
 import org.melati.util.Tree;
 62  
 
 63  
 
 64  
 /**
 65  
  * A utility object for placing in a <code>ServletTemplateContext</code>.
 66  
  */
 67  
 public class AdminUtils {
 68  
   
 69  
   private String contextPath;
 70  
   private String servletURL;
 71  
   private String staticURL;
 72  
   private String logicalDatabase;
 73  
   
 74  
   /**
 75  
    *  Constructor. 
 76  
    */
 77  
   public AdminUtils(Melati melati) {
 78  459
     this(melati.getRequest() == null ? null : melati.getRequest().getContextPath(),
 79  
          melati.getRequest() == null ? null : melati.getRequest().getServletPath(),
 80  
          melati.getConfig().getStaticURL() ,
 81  
          melati.getPoemContext().getLogicalDatabase());    
 82  459
   }
 83  
 
 84  
   /**
 85  
    *  Constructor. 
 86  
    */
 87  
   private AdminUtils(String contextPath, String servlet, 
 88  459
                     String staticURL, String logicalDatabase) {
 89  459
     this.contextPath = contextPath;
 90  459
     this.servletURL = contextPath + servlet;
 91  459
     this.staticURL = staticURL;
 92  459
     this.logicalDatabase = logicalDatabase;
 93  
     // HACK if we are using 2.0 Servlet API then zone is
 94  
     // included in servlet and contextPath is empty
 95  459
     if (contextPath == "") {
 96  0
       this.contextPath = servlet.substring(0, servlet.lastIndexOf("/"));
 97  
     }
 98  459
   }
 99  
   
 100  
   /**
 101  
    * @return the name of the default table to display  
 102  
    */
 103  
   public static String getPrimaryDisplayTable(Melati melati) { 
 104  240
     if (Admin.getPrimaryDisplayTable() == null) 
 105  1
       Admin.setPrimaryDisplayTable(melati.getDatabase().
 106  
           getSettingTable().get(Admin.class.getName() + ".PrimaryDisplayTable"));
 107  240
     if (Admin.getPrimaryDisplayTable() == null)
 108  1
       Admin.setPrimaryDisplayTable("columninfo");
 109  240
     return Admin.getPrimaryDisplayTable();
 110  
   }
 111  
   
 112  
   /**
 113  
    * @param melati to get db from
 114  
    * @return the stylesheet for screen media  
 115  
    */
 116  
   public String getScreenStylesheetURL(Melati melati) {
 117  283
     if (Admin.getScreenStylesheetURL() == null) 
 118  1
       Admin.setScreenStylesheetURL(melati.getDatabase().
 119  
           getSettingTable().get(Admin.class.getName() + ".ScreenStylesheetURL"));
 120  283
     if (Admin.getScreenStylesheetURL() == null)
 121  1
       Admin.setScreenStylesheetURL("/admin.css");
 122  283
     return staticURL + Admin.getScreenStylesheetURL();
 123  
   }
 124  
   /**
 125  
    * @return the settings table setup url
 126  
    */
 127  
   public String getSetupURL() {
 128  17
     return servletURL + "/" + logicalDatabase + 
 129  
         "/setting/setup";
 130  
   }
 131  
   
 132  
   
 133  
   /**
 134  
    * Check if setting in db, provide default if not, do not 
 135  
    * write default to db. 
 136  
    * 
 137  
    * @param melati to get db from
 138  
    * @return the homepage URL for this databse  
 139  
    */
 140  
   public String getHomepageURL(Melati melati) {
 141  60
     if (Admin.getHomepageURL() == null) 
 142  1
       Admin.setHomepageURL(melati.getDatabase().
 143  
           getSettingTable().get(Admin.class.getName() + ".HomepageURL"));
 144  60
     if (Admin.getHomepageURL() == null)
 145  1
       Admin.setHomepageURL("http://www.melati.org/");
 146  60
     return Admin.getHomepageURL();
 147  
   }
 148  
   
 149  
   /**
 150  
    * @param melati the melati
 151  
    * @param name of template
 152  
    * @return name prepended with ldb, table and troid if not null
 153  
    */
 154  
   public String getURL(Melati melati, String name) { 
 155  207
     String url = servletURL + "/" + logicalDatabase;
 156  207
     if (melati.getTable() != null)
 157  182
       url += "/" + melati.getTable().getName();
 158  207
     if (melati.getObject() != null)
 159  49
       url += "/" + melati.getObject().getTroid();
 160  207
     return url + "/" + name;
 161  
   }
 162  
   /**
 163  
    * @return name prepended with ldb and table name
 164  
    */
 165  
   public String getURL(Table<?> table, String name) { 
 166  32
     String url = servletURL + "/" + logicalDatabase;
 167  32
     url += "/" + table.getName();
 168  32
     return url + "/" + name;
 169  
   }
 170  
   
 171  
   
 172  
   /** @return The Main URL. */
 173  
   public String getMainURL(String ld) {
 174  176
     String url = servletURL + "/" + ld;
 175  176
     return url + "/Main";
 176  
   }
 177  
   /** @return The Main URL. */
 178  
   public String getMainURL(Melati melati) {
 179  34
     return getURL(melati, "Main");
 180  
   }
 181  
   /** @return The Main URL after deletion of a tableinfo */
 182  
   public String getMainURL(Table<?> table) {
 183  0
     return getURL(table, "Main");
 184  
   }
 185  
   /** @return The Main URL after creation of a tableinfo */
 186  
   public String getMainURL(Table<?> table,Persistent p) {
 187  0
     String url = servletURL + "/" + logicalDatabase;
 188  0
     url += "/" + table.getName();
 189  0
     url += "/" + p.troid();
 190  0
     return url + "/" + "Main";
 191  
  }
 192  
   
 193  
   /** @return The Top URL. */
 194  
   public String getTopURL(Melati melati) {
 195  23
     return getURL(melati, "Top");
 196  
   }
 197  
   
 198  
   /**
 199  
    * @return The Bottom URL.
 200  
    */
 201  
   /*
 202  
    * Do not think this is used
 203  
   public String getBottomURL(Table<?> table, Melati melati) {
 204  
     return  servletURL + "/" + logicalDatabase + 
 205  
         "/" + table.getName() +
 206  
         (melati.getObject() != null &&  
 207  
                 melati.getObject().getTable() == table ? 
 208  
                         "/" + melati.getObject().getTroid() 
 209  
                         : "") + 
 210  
         "/Bottom";
 211  
   }
 212  
   */
 213  
   /**
 214  
    * @return The Bottom URL.
 215  
    */
 216  
   public String getBottomURL(Melati melati) {
 217  53
     String url =  servletURL + "/" + logicalDatabase + "/";
 218  53
     if (melati.getTable() != null)
 219  28
       url += melati.getTable().getName();
 220  
     else 
 221  25
       url += getPrimaryDisplayTable(melati); 
 222  53
     if (melati.getObject() != null)
 223  11
       url += "/" + melati.getObject().getTroid();
 224  53
     url += "/Bottom";
 225  53
     return url;
 226  
   }
 227  
   
 228  
   /**
 229  
    * @return The Left URL.
 230  
    */
 231  
   public String getTableURL(Table<?> table) {
 232  32
     return getURL(table, "Table");
 233  
   }
 234  
   
 235  
   /**
 236  
    * @return The Right URL.
 237  
    */
 238  
   public String getRecordURL(Persistent object) throws AccessPoemException {
 239  4
     return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
 240  
             + "/" + object.troid() + "/Record";
 241  
   }
 242  
 
 243  
   /**
 244  
    * @return The Right URL.
 245  
    */
 246  
   public String getRecordURL(Persistent object, String returnTarget, String returnURL) throws AccessPoemException {
 247  2231
     return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
 248  
             + "/" + object.troid() + "/Record" + 
 249  
             "?returnTarget=" + returnTarget + 
 250  
             "&returnURL=" + returnURL;
 251  
   }
 252  
 
 253  
   /**
 254  
    * @return The Right URL.
 255  
    */
 256  
   public String getRecordURL(Melati melati) throws AccessPoemException {
 257  32
     return getURL(melati, "Record");
 258  
   }
 259  
 
 260  
   /**
 261  
    * @return The Primary Select URL.
 262  
    */
 263  
   public String getPrimarySelectURL(Melati melati) {
 264  33
     return getURL(melati, "PrimarySelect");
 265  
   }
 266  
 
 267  
   /**
 268  
    * @return The Selection URL.
 269  
    */
 270  
   public String getSelectionURL(Table<?> table) {
 271  144
     return getSelectionURL(table,"admin_record");
 272  
   }
 273  
   /**
 274  
    * @return The Selection URL.
 275  
    */
 276  
   public String getSelectionURL(Table<?> table, String returnTarget) {
 277  172
     return getSelectionURL(table, "admin_record", returnTarget);
 278  
   }
 279  
   /**
 280  
    * @param table
 281  
    * @param targetPane
 282  
    * @param returnTarget
 283  
    * @return the url
 284  
    */
 285  
   public String getSelectionURL(Table<?> table, String targetPane, String returnTarget) {
 286  172
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 287  
             + "/Selection?" +
 288  
             "target=" + targetPane +  
 289  
             "&returnTarget=" + returnTarget;
 290  
   }
 291  
   
 292  
   /**
 293  
    * Toggle the sort order of column.
 294  
    * @return the same url with the toggle field added or removed
 295  
    */
 296  
   public String getToggledOrderSelectionURL(Melati melati, String field, String value) { 
 297  192
     String url = melati.sameURLWith(field,value);
 298  192
     String toggleField = "&" + field + "-toggle=true";
 299  192
     if (url.endsWith(toggleField))
 300  24
       return url.substring(0,url.length() - toggleField.length());
 301  
     else 
 302  168
       return url + "&" + field + "-toggle=true";
 303  
   }
 304  
   
 305  
   /**
 306  
    * @param melati
 307  
    * @return The Selection URL.
 308  
    */
 309  
   public String getSelectionURL(Melati melati) {
 310  33
     return getSelectionURL(melati,"admin_record");    
 311  
   }
 312  
 
 313  
   /**
 314  
    * @return The Selection URL.
 315  
    */
 316  
   public String getSelectionURL(Melati melati, String returnTarget) {
 317  33
     return servletURL + "/" + 
 318  
         logicalDatabase + "/" + 
 319  
         melati.getTable().getName()
 320  
           + "/Selection?" +
 321  
           "target=admin_record" + 
 322  
           "&returnTarget=" + (returnTarget == null ? "" : returnTarget) + 
 323  
           (melati.getObject() == null ? 
 324  
               "" : 
 325  
               "&field_id=" + melati.getObject().troid());
 326  
   }
 327  
   
 328  
   /**
 329  
    * @return The Selection Right URL.
 330  
    */
 331  
   public String getSelectionRightURL(Table<?> table) {
 332  6
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 333  
     + "/SelectionRight";
 334  
   }
 335  
 
 336  
   /**
 337  
    * @return The Navigation URL.
 338  
    */
 339  
   public String getNavigationURL(Table<?> table) {
 340  33
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 341  
     + "/Navigation";
 342  
   }
 343  
   
 344  
   /**
 345  
    * @return The Edit Header URL.
 346  
    */
 347  
   public String getEditHeaderURL(Melati melati) throws AccessPoemException {
 348  37
     if (melati.getObject() == null)
 349  27
       return getURL(melati, "blank");
 350  
     else
 351  10
       return getURL(melati, "EditHeader");
 352  
   }
 353  
 
 354  
   /**
 355  
    * @return The Edit URL.
 356  
    */
 357  
   public String getEditURL(Melati melati) throws AccessPoemException {
 358  48
     if (melati.getObject() == null)
 359  27
       return getURL(melati, "blank");
 360  
     else
 361  21
       return getURL(melati, "Edit");
 362  
   }
 363  
   /**
 364  
    * @return The Edit URL.
 365  
    */
 366  
   public String getEditURL(Persistent object) throws AccessPoemException {
 367  0
     return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
 368  
             + "/" + object.troid() + "/Edit";
 369  
   }
 370  
 
 371  
   /**
 372  
    * @param melati
 373  
    * @return the name of the Record Fields frame
 374  
    */
 375  
   public String getEditFrameName(Melati melati) { 
 376  71
     String name = "admin_edit";
 377  71
     name += "_" + melati.getTable().getName();
 378  71
     if (melati.getObject() != null) 
 379  44
       name += "_" + melati.getObject().troid();
 380  71
     return name;
 381  
   }
 382  
   /**
 383  
    * @return The Tree URL.
 384  
    */
 385  
   public String getTreeURL(Persistent object) throws AccessPoemException {
 386  11
     return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
 387  
             + "/" + object.troid() + "/Tree";
 388  
   }
 389  
   
 390  
   /**
 391  
    * @return The Tree URL.
 392  
    */
 393  
   public String getTreeURL(Table<?> table) throws AccessPoemException {
 394  44
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 395  
             +  "/Tree";
 396  
   }
 397  
   
 398  
 
 399  
   /**
 400  
    * @return The Add URL.
 401  
    */
 402  
   public String getAddURL(Table<?> table) throws AccessPoemException {
 403  60
     return servletURL
 404  
             + "/"
 405  
             + logicalDatabase
 406  
             + "/" 
 407  
             + table.getName() 
 408  
             + "/" 
 409  
             + "Add";
 410  
   }
 411  
 
 412  
   /**
 413  
    * @return The Popup URL.
 414  
    */
 415  
   public String getPopUpURL(Table<?> table) {
 416  66
     return servletURL + "/" + logicalDatabase + "/" + table.getName() + "/PopUp";
 417  
   }
 418  
   
 419  
   /**
 420  
    * @return The Selection Window URL.
 421  
    */
 422  
   public String getSelectionWindowURL(Table<?> table) {
 423  19
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 424  
             + "/SelectionWindow?target=";
 425  
   }
 426  
 
 427  
   /**
 428  
    * @return The Selection Window Primary Select URL.
 429  
    */
 430  
   public String getSelectionWindowPrimarySelectURL(Table<?> table) {
 431  1
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 432  
             + "/SelectionWindowPrimarySelect";
 433  
   }
 434  
 
 435  
   /**
 436  
    * @return The Selection Window Selection URL.
 437  
    */
 438  
   public String getSelectionWindowSelectionURL(Table<?> table) {
 439  3
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 440  
             + "/SelectionWindowSelection";
 441  
   }
 442  
   
 443  
   /**
 444  
    * @return The Status URL.
 445  
    */
 446  
   public String getStatusURL() {
 447  17
     return contextPath + "/org.melati.admin.Status/" + logicalDatabase;
 448  
   }
 449  
   
 450  
   /**
 451  
    * @return The Session Analysis URL.
 452  
    */
 453  
   public String getSessionURL() {
 454  17
     return contextPath + "/org.melati.test.SessionAnalysisServlet";
 455  
   }
 456  
   
 457  
   /**
 458  
    * @return The URL for DSD generation. 
 459  
    */
 460  
   public String getDsdURL() {
 461  17
     return servletURL + "/" + logicalDatabase + "/DSD";
 462  
   }
 463  
   
 464  
   /**
 465  
    * In an insert situation we will not have a Troid, so cannot pass it through.
 466  
    * If your upload handler depends on having a persistent, then you should
 467  
    * override your upload template so that it prevents uploading in an insert
 468  
    * situation.
 469  
    * 
 470  
    * @param table table object belongs to
 471  
    * @param object the Persistent we are dealing with
 472  
    * @param field the upload field
 473  
    * @return Upload Url
 474  
    */
 475  
   public String getUploadURL(Table<?> table, Persistent object, Field<?> field) {
 476  8
     return upload(table, object) + "/Upload?field=" + field.getName();
 477  
   }
 478  
   
 479  
   /**
 480  
    * Upload URL.
 481  
    * 
 482  
    * @param table table object belongs to
 483  
    * @param object the Persistent we are dealing with
 484  
    * @param field the upload field
 485  
    * @return Upload done URL
 486  
    */
 487  
   public String getUploadHandlerURL(Table<?> table, Persistent object, String field) {
 488  2
     return upload(table, object) + "/UploadDone?field=" + field;
 489  
   }
 490  
   private String upload(Table<?> table, Persistent object) {
 491  10
     String url = servletURL + "/" + logicalDatabase + "/" + table.getName();
 492  10
     if (object != null)
 493  2
       url += "/" + object.troid();
 494  10
     return url;
 495  
   }
 496  
   
 497  
  
 498  
   /**
 499  
    * Render the specials directly to the output.
 500  
    *  
 501  
    * @param melati the Melati
 502  
    * @param ml The MarkupLanguage we are using
 503  
    * @param object a Persistent to render the specials of 
 504  
    * @return an empty String
 505  
    * @throws Exception maybe
 506  
    */
 507  
   public String specialFacilities(Melati melati, MarkupLanguage ml,
 508  
           Persistent object) throws Exception {
 509  20
   if (object instanceof AdminSpecialised)
 510  1
     melati.getTemplateEngine().expandTemplate(melati.getWriter(),
 511  
           ((AdminSpecialised) object).adminSpecialFacilities(melati, ml),
 512  
           melati.getTemplateContext());
 513  20
   return "";
 514  
   /*
 515  
   if (object instanceof AdminSpecialised)
 516  
       return melati.getTemplateEngine().expandedTemplate(
 517  
           melati.getTemplateEngine().template(
 518  
               ((AdminSpecialised) object).adminSpecialFacilities(melati, ml)),
 519  
               melati.getTemplateContext());
 520  
     else 
 521  
       return "";
 522  
     */
 523  
   }
 524  
 
 525  
   /**
 526  
    * @return Defaults to /MelatiStatic/admin
 527  
    */
 528  
   public String getStaticURL() {
 529  691
     return staticURL;
 530  
   }
 531  
 
 532  
   /**
 533  
    *  Create a tree. 
 534  
    * @param node  a tree node
 535  
    * @return a tree with node as its root
 536  
    */
 537  
   public JSStaticTree createTree(Treeable node) {
 538  2
     return new JSStaticTree(new Tree(node), getStaticURL());
 539  
   }
 540  
   
 541  
   /**
 542  
    *  Create a forest of trees. 
 543  
    * @param table  the table to tree 
 544  
    * @return a tree with node as its root
 545  
    */
 546  
   @SuppressWarnings({ "unchecked", "rawtypes" })
 547  
   public JSStaticTree createForest(Table<?> table) {
 548  1
     Object[] all = ArrayUtils.arrayOf((Enumeration) table.selection());
 549  1
     Hashtable<Treeable, Boolean> hasParent = new Hashtable<Treeable, Boolean>();
 550  3
     for (int i = 0; i < all.length; i++) {
 551  2
       if (hasParent.get(all[i]) == null) { 
 552  2
         Treeable[] kids = ((Treeable)all[i]).getChildren();
 553  3
         for (int j = 0; j < kids.length; j++)
 554  1
           hasParent.put(kids[j], Boolean.TRUE);
 555  
       }
 556  
     }
 557  1
     int count = 0;
 558  3
     for (int i = 0; i < all.length; i++) {
 559  2
       if (hasParent.get(all[i]) == null){ 
 560  2
         count++;
 561  
       }
 562  
     }
 563  1
     Treeable[] roots = new Treeable[count];
 564  1
     int j = 0;
 565  3
     for (int i = 0; i < all.length; i++) {
 566  2
       if (hasParent.get(all[i]) == null) {
 567  2
         roots[j] = (Treeable)all[i];
 568  2
         j++;
 569  
       }
 570  
     }
 571  1
     return new JSStaticTree(roots, getStaticURL());
 572  
   }
 573  
 
 574  
   /**
 575  
    * @param qualifiedName
 576  
    * @return text following the last dot
 577  
    */
 578  
   public static String simpleName(String qualifiedName) { 
 579  13
     return qualifiedName.substring(
 580  
         qualifiedName.lastIndexOf('.') != -1 ?
 581  
             qualifiedName.lastIndexOf('.') + 1 : 
 582  
             0,
 583  
         qualifiedName.length());
 584  
   }
 585  
   
 586  
   /**
 587  
    * @param in the String to escape
 588  
    * @return the escaped String
 589  
    */
 590  
   public static String csvEscaped(String in) { 
 591  2276
     StringBuffer b = new StringBuffer();
 592  2276
     StringUtils.appendEscaped(b, in, '"', '"');
 593  2276
     return b.toString();
 594  
   }
 595  
 }