Coverage Report - org.melati.admin.AdminUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
AdminUtils
93%
117/125
89%
52/58
1.627
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/admin/AdminUtils.java,v $
 3  
  * $Revision: 1.90 $
 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  56
     if (Admin.getHomepageURL() == null) 
 142  1
       Admin.setHomepageURL(melati.getDatabase().
 143  
           getSettingTable().get(Admin.class.getName() + ".HomepageURL"));
 144  56
     if (Admin.getHomepageURL() == null)
 145  1
       Admin.setHomepageURL("http://www.melati.org/");
 146  56
     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 MainURL(String ld) {
 174  74
     String url = servletURL + "/" + ld;
 175  74
     return url + "/Main";
 176  
   }
 177  
   /** @return The Main URL. */
 178  
   public String MainURL(Melati melati) {
 179  34
     return getURL(melati, "Main");
 180  
   }
 181  
   /** @return The Main URL after deletion of a tableinfo */
 182  
   public String MainURL(Table<?> table) {
 183  0
     return getURL(table, "Main");
 184  
   }
 185  
   /** @return The Main URL after creation of a tableinfo */
 186  
   public String MainURL(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 TopURL(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 BottomURL(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 BottomURL(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  
   public String bottomURL(Melati melati) {
 228  0
     return BottomURL(melati);
 229  
   }
 230  
   
 231  
   /**
 232  
    * @return The Left URL.
 233  
    */
 234  
   public String TableURL(Table<?> table) {
 235  32
     return getURL(table, "Table");
 236  
   }
 237  
   
 238  
   /**
 239  
    * @return The Right URL.
 240  
    */
 241  
   public String RecordURL(Persistent object) throws AccessPoemException {
 242  4
     return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
 243  
             + "/" + object.troid() + "/Record";
 244  
   }
 245  
 
 246  
   /**
 247  
    * @return The Right URL.
 248  
    */
 249  
   public String RecordURL(Persistent object, String returnTarget, String returnURL) throws AccessPoemException {
 250  2051
     return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
 251  
             + "/" + object.troid() + "/Record" + 
 252  
             "?returnTarget=" + returnTarget + 
 253  
             "&returnURL=" + returnURL;
 254  
   }
 255  
 
 256  
   /**
 257  
    * @return The Right URL.
 258  
    */
 259  
   public String RecordURL(Melati melati) throws AccessPoemException {
 260  32
     return getURL(melati, "Record");
 261  
   }
 262  
 
 263  
   /**
 264  
    * @return The Primary Select URL.
 265  
    */
 266  
   public String PrimarySelectURL(Melati melati) {
 267  33
     return getURL(melati, "PrimarySelect");
 268  
   }
 269  
 
 270  
   /**
 271  
    * @return The Selection URL.
 272  
    */
 273  
   public String SelectionURL(Table<?> table) {
 274  144
     return SelectionURL(table,"admin_record");
 275  
   }
 276  
   /**
 277  
    * @return The Selection URL.
 278  
    */
 279  
   public String SelectionURL(Table<?> table, String returnTarget) {
 280  172
     return SelectionURL(table, "admin_record", returnTarget);
 281  
   }
 282  
   /**
 283  
    * @param table
 284  
    * @param targetPane
 285  
    * @param returnTarget
 286  
    * @return the url
 287  
    */
 288  
   public String SelectionURL(Table<?> table, String targetPane, String returnTarget) {
 289  172
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 290  
             + "/Selection?" +
 291  
             "target=" + targetPane +  
 292  
             "&returnTarget=" + returnTarget;
 293  
   }
 294  
   
 295  
   /**
 296  
    * Toggle the sort order of column.
 297  
    * @return the same url with the toggle field added or removed
 298  
    */
 299  
   public String ToggledOrderSelectionURL(Melati melati, String field, String value) { 
 300  192
     String url = melati.sameURLWith(field,value);
 301  192
     String toggleField = "&" + field + "-toggle=true";
 302  192
     if (url.endsWith(toggleField))
 303  24
       return url.substring(0,url.length() - toggleField.length());
 304  
     else 
 305  168
       return url + "&" + field + "-toggle=true";
 306  
   }
 307  
   
 308  
   /**
 309  
    * @param melati
 310  
    * @return The Selection URL.
 311  
    */
 312  
   public String SelectionURL(Melati melati) {
 313  33
     return SelectionURL(melati,"admin_record");    
 314  
   }
 315  
 
 316  
   /**
 317  
    * @return The Selection URL.
 318  
    */
 319  
   public String SelectionURL(Melati melati, String returnTarget) {
 320  33
     return servletURL + "/" + 
 321  
         logicalDatabase + "/" + 
 322  
         melati.getTable().getName()
 323  
           + "/Selection?" +
 324  
           "target=admin_record" + 
 325  
           "&returnTarget=" + (returnTarget == null ? "" : returnTarget) + 
 326  
           (melati.getObject() == null ? 
 327  
               "" : 
 328  
               "&field_id=" + melati.getObject().troid());
 329  
   }
 330  
   
 331  
   /**
 332  
    * @return The Selection Right URL.
 333  
    */
 334  
   public String SelectionRightURL(Table<?> table) {
 335  6
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 336  
     + "/SelectionRight";
 337  
   }
 338  
 
 339  
   /**
 340  
    * @return The Navigation URL.
 341  
    */
 342  
   public String NavigationURL(Table<?> table) {
 343  33
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 344  
     + "/Navigation";
 345  
   }
 346  
   
 347  
   /**
 348  
    * @return The Edit Header URL.
 349  
    */
 350  
   public String EditHeaderURL(Melati melati) throws AccessPoemException {
 351  37
     if (melati.getObject() == null)
 352  27
       return getURL(melati, "blank");
 353  
     else
 354  10
       return getURL(melati, "EditHeader");
 355  
   }
 356  
 
 357  
   /**
 358  
    * @return The Edit URL.
 359  
    */
 360  
   public String EditURL(Melati melati) throws AccessPoemException {
 361  48
     if (melati.getObject() == null)
 362  27
       return getURL(melati, "blank");
 363  
     else
 364  21
       return getURL(melati, "Edit");
 365  
   }
 366  
   /**
 367  
    * @return The Edit URL.
 368  
    */
 369  
   public String EditURL(Persistent object) throws AccessPoemException {
 370  0
     return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
 371  
             + "/" + object.troid() + "/Edit";
 372  
   }
 373  
 
 374  
   /**
 375  
    * @param melati
 376  
    * @return the name of the Record Fields frame
 377  
    */
 378  
   public String EditFrameName(Melati melati) { 
 379  71
     String name = "admin_edit";
 380  71
     name += "_" + melati.getTable().getName();
 381  71
     if (melati.getObject() != null) 
 382  44
       name += "_" + melati.getObject().troid();
 383  71
     return name;
 384  
   }
 385  
   /**
 386  
    * @return The Tree URL.
 387  
    */
 388  
   public String TreeURL(Persistent object) throws AccessPoemException {
 389  11
     return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
 390  
             + "/" + object.troid() + "/Tree";
 391  
   }
 392  
   
 393  
   /**
 394  
    * @return The Tree URL.
 395  
    */
 396  
   public String TreeURL(Table<?> table) throws AccessPoemException {
 397  44
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 398  
             +  "/Tree";
 399  
   }
 400  
   
 401  
 
 402  
   /**
 403  
    * @return The Add URL.
 404  
    */
 405  
   public String AddURL(Table<?> table) throws AccessPoemException {
 406  58
     return servletURL
 407  
             + "/"
 408  
             + logicalDatabase
 409  
             + "/" 
 410  
             + table.getName() 
 411  
             + "/" 
 412  
             + "Add";
 413  
   }
 414  
 
 415  
   /**
 416  
    * @return The Popup URL.
 417  
    */
 418  
   public String PopUpURL(Table<?> table) {
 419  66
     return servletURL + "/" + logicalDatabase + "/" + table.getName() + "/PopUp";
 420  
   }
 421  
   
 422  
   /**
 423  
    * @return The Selection Window URL.
 424  
    */
 425  
   public String SelectionWindowURL(Table<?> table) {
 426  17
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 427  
             + "/SelectionWindow?target=";
 428  
   }
 429  
 
 430  
   /**
 431  
    * @return The Selection Window Primary Select URL.
 432  
    */
 433  
   public String SelectionWindowPrimarySelectURL(Table<?> table) {
 434  1
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 435  
             + "/SelectionWindowPrimarySelect";
 436  
   }
 437  
 
 438  
   /**
 439  
    * @return The Selection Window Selection URL.
 440  
    */
 441  
   public String SelectionWindowSelectionURL(Table<?> table) {
 442  3
     return servletURL + "/" + logicalDatabase + "/" + table.getName()
 443  
             + "/SelectionWindowSelection";
 444  
   }
 445  
   
 446  
   /**
 447  
    * @return The Status URL.
 448  
    */
 449  
   public String StatusURL() {
 450  17
     return contextPath + "/org.melati.admin.Status/" + logicalDatabase;
 451  
   }
 452  
   
 453  
   /**
 454  
    * @return The Session Analysis URL.
 455  
    */
 456  
   public String SessionURL() {
 457  17
     return contextPath + "/org.melati.test.SessionAnalysisServlet";
 458  
   }
 459  
   
 460  
   /**
 461  
    * @return The URL for DSD generation. 
 462  
    */
 463  
   public String DsdURL() {
 464  17
     return servletURL + "/" + logicalDatabase + "/DSD";
 465  
   }
 466  
   
 467  
   /**
 468  
    * In an insert situation we will not have a Troid, so cannot pass it through.
 469  
    * If your upload handler depends on having a persistent, then you should
 470  
    * override your upload template so that it prevents uploading in an insert
 471  
    * situation.
 472  
    * 
 473  
    * @param table table object belongs to
 474  
    * @param object the Persistent we are dealing with
 475  
    * @param field the upload field
 476  
    * @return Upload Url
 477  
    */
 478  
   public String UploadURL(Table<?> table, Persistent object, Field<?> field) {
 479  8
     return upload(table, object) + "/Upload?field=" + field.getName();
 480  
   }
 481  
   
 482  
   /**
 483  
    * Upload URL.
 484  
    * 
 485  
    * @param table table object belongs to
 486  
    * @param object the Persistent we are dealing with
 487  
    * @param field the upload field
 488  
    * @return Upload done URL
 489  
    */
 490  
   public String UploadHandlerURL(Table<?> table, Persistent object, String field) {
 491  2
     return upload(table, object) + "/UploadDone?field=" + field;
 492  
   }
 493  
   private String upload(Table<?> table, Persistent object) {
 494  10
     String url = servletURL + "/" + logicalDatabase + "/" + table.getName();
 495  10
     if (object != null)
 496  2
       url += "/" + object.troid();
 497  10
     return url;
 498  
   }
 499  
   
 500  
  
 501  
   /**
 502  
    * Render the specials directly to the output.
 503  
    *  
 504  
    * @param melati the Melati
 505  
    * @param ml The MarkupLanguage we are using
 506  
    * @param object a Persistent to render the specials of 
 507  
    * @return an empty String
 508  
    * @throws Exception maybe
 509  
    */
 510  
   public String specialFacilities(Melati melati, MarkupLanguage ml,
 511  
           Persistent object) throws Exception {
 512  20
   if (object instanceof AdminSpecialised)
 513  1
     melati.getTemplateEngine().expandTemplate(melati.getWriter(),
 514  
           ((AdminSpecialised) object).adminSpecialFacilities(melati, ml),
 515  
           melati.getTemplateContext());
 516  20
   return "";
 517  
   /*
 518  
   if (object instanceof AdminSpecialised)
 519  
       return melati.getTemplateEngine().expandedTemplate(
 520  
           melati.getTemplateEngine().template(
 521  
               ((AdminSpecialised) object).adminSpecialFacilities(melati, ml)),
 522  
               melati.getTemplateContext());
 523  
     else 
 524  
       return "";
 525  
     */
 526  
   }
 527  
 
 528  
   /**
 529  
    * @return Defaults to /MelatiStatic/admin
 530  
    */
 531  
   public String getStaticURL() {
 532  653
     return staticURL;
 533  
   }
 534  
 
 535  
   /**
 536  
    *  Create a tree. 
 537  
    * @param node  a tree node
 538  
    * @return a tree with node as its root
 539  
    */
 540  
   public JSStaticTree createTree(Treeable node) {
 541  2
     return new JSStaticTree(new Tree(node), getStaticURL());
 542  
   }
 543  
   
 544  
   /**
 545  
    *  Create a forest of trees. 
 546  
    * @param table  the table to tree 
 547  
    * @return a tree with node as its root
 548  
    */
 549  
   @SuppressWarnings({ "unchecked", "rawtypes" })
 550  
   public JSStaticTree createForest(Table<?> table) {
 551  1
     Object[] all = ArrayUtils.arrayOf((Enumeration) table.selection());
 552  1
     Hashtable<Treeable, Boolean> hasParent = new Hashtable<Treeable, Boolean>();
 553  3
     for (int i = 0; i < all.length; i++) {
 554  2
       if (hasParent.get(all[i]) == null) { 
 555  2
         Treeable[] kids = ((Treeable)all[i]).getChildren();
 556  3
         for (int j = 0; j < kids.length; j++)
 557  1
           hasParent.put(kids[j], Boolean.TRUE);
 558  
       }
 559  
     }
 560  1
     int count = 0;
 561  3
     for (int i = 0; i < all.length; i++) {
 562  2
       if (hasParent.get(all[i]) == null){ 
 563  2
         count++;
 564  
       }
 565  
     }
 566  1
     Treeable[] roots = new Treeable[count];
 567  1
     int j = 0;
 568  3
     for (int i = 0; i < all.length; i++) {
 569  2
       if (hasParent.get(all[i]) == null) {
 570  2
         roots[j] = (Treeable)all[i];
 571  2
         j++;
 572  
       }
 573  
     }
 574  1
     return new JSStaticTree(roots, getStaticURL());
 575  
   }
 576  
 
 577  
   /**
 578  
    * @param qualifiedName
 579  
    * @return text following the last dot
 580  
    */
 581  
   public static String simpleName(String qualifiedName) { 
 582  13
     return qualifiedName.substring(
 583  
         qualifiedName.lastIndexOf('.') != -1 ?
 584  
             qualifiedName.lastIndexOf('.') + 1 : 
 585  
             0,
 586  
         qualifiedName.length());
 587  
   }
 588  
   
 589  
   /**
 590  
    * @param in the String to escape
 591  
    * @return the escaped String
 592  
    */
 593  
   public static String csvEscaped(String in) { 
 594  2276
     StringBuffer b = new StringBuffer();
 595  2276
     StringUtils.appendEscaped(b, in, '"', '"');
 596  2276
     return b.toString();
 597  
   }
 598  
 }