KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > template > eventhandler > TEUtils


1 package de.webman.template.eventhandler;
2
3 import java.io.*;
4 import java.sql.*;
5 import com.teamkonzept.db.*;
6 import com.teamkonzept.webman.WebManEvent;
7 import com.teamkonzept.lib.*;
8 import com.teamkonzept.lib.templates.*;
9 import com.teamkonzept.webman.mainint.events.*;
10 import com.teamkonzept.webman.mainint.*;
11 import com.teamkonzept.web.*;
12 import com.oroinc.text.regex.*;
13 import com.teamkonzept.webman.mainint.db.queries.template.CheckTemplateDependency;
14 import com.teamkonzept.webman.mainint.db.queries.template.CheckTemplateExists;
15
16 /**
17  * Utility Klasse für die "TE-" -EventHandler
18  *
19  * @author $Author: tina $
20  * @version $Revision: 1.5 $
21  */

22 public class TEUtils implements TemplateTypes, UserCodes, ParameterTypes
23 {
24     /**
25      * Methode zum Anzeigen des Templatetextes.
26      *
27      * @param evt über den Event werden die verschiedenen Parameter
28      * wie TEMPLATE_NAME, etc. ausgewertet
29      * @throws Throwable schiefgehen kann immer etwas
30      * @throws FileNotFoundException im Falle das File existiert gar nicht
31      * @return true einfach nur um zusagen das alles okay ist
32      */

33     public static boolean showSavedText( TKEvent evt ) throws Throwable JavaDoc, FileNotFoundException
34     {
35         try
36         {
37             TKHTMLTemplate t = evt.getPrepHTMLTemplate( "te_editTmpl.tmpl" );
38             String JavaDoc tmplName = evt.getParameter( PARAMETER, "TEMPLATE_NAME" );
39             String JavaDoc tmplId = evt.getParameter( PARAMETER, "TEMPLATE_ID" );
40             t.set( "TEMPLATE_NAME", tmplName );
41             t.set( "TEMPLATE_ID", tmplId );
42
43             // Text bekommen !!!
44
String JavaDoc s = TKTemplateCache.getSource("file://"+ evt.getHttpInterface().getDocumentRoot() + File.separator + TemplateUtils.getGenerationDirectory() + tmplName);
45             t.set("TEMPLATE_TEXT", s);
46
47             WebManEvent.fillEventsIntoTemplate(evt.getRemoteUser(), t, ContextConstants.TEMPLATES);
48             evt.finishTemplate(t);
49         }
50         catch (Throwable JavaDoc e)
51         {
52             throw WebmanExceptionHandler.getException(e);
53         }
54         return true;
55     }
56     /**
57      * Methode prüft ob der eingegebene Templatename eine gültige Endung hat.
58      *
59      * @param name der Name des Templates.
60      * @throws TKException warum weiß ich auch nicht.
61      * @throws Throwable falls was schief geht
62      * @return cName Templatename
63      */

64     public static boolean isValidTemplateName( String JavaDoc name ) throws TKException, Throwable JavaDoc
65     {
66         try
67         {
68             String JavaDoc cName = name.toUpperCase();
69             // hier noch auf Sonderzeichen pruefen !
70
isValidName(name );
71             return cName.endsWith(TK_TEMPLATE) || cName.endsWith(JSP_TEMPLATE) || cName.endsWith(XSLT_TEMPLATE);
72         }
73         catch (Throwable JavaDoc e)
74         {
75             throw WebmanExceptionHandler.getException(e);
76         }
77     }
78     
79     public static boolean isValidName( String JavaDoc name ) throws Throwable JavaDoc
80     {
81         if( name == null || name.equals( "" ) )
82         {
83             // leer ist natuerlich auch nicht gut...
84
throw new TKUserException("Es muß ein Name angegeben werden", NO_PATHNAME, USER_SEVERITY, true, null);
85         }
86
87         PatternMatcher matcher = TKReg.getMatcher();
88         PatternCompiler compiler = TKReg.getCompiler();
89         Pattern patNotAllowed;
90
91         // nur wordcharacter (alphanumeric) fuer dirnamen: a-zA-Z_0-9 und das -
92
// to DO :: Das paßt nicht zur Localisation !!!
93
patNotAllowed = compiler.compile( "[^a-zA-Z_0-9\\-./]" );
94         if( matcher.contains( name, patNotAllowed ) )
95         {
96             // kein gueltiger TemplateName
97
throw new TKUserException("Kein gültiger Pfadname: " + name, NO_VALID_PATHNAME, USER_SEVERITY, true, null);
98         } else {
99             return true;
100         }
101     }
102     
103     /**
104      * Diese Methode soll gucken ob es dieses Template (beim Neuanlegen) schon
105      * gibt, und wenn ja den user informieren
106      *
107      * @param name der zuüberprüfende Templatename
108      * @throws Throwable
109      * @return true wenn der Templatename in der DB bereits existitert.
110      *
111      */

112     public static boolean templateNameExists ( String JavaDoc name ) throws Throwable JavaDoc
113     {
114         TKQuery q = TKDBManager.newQuery(CheckTemplateExists.class);
115         q.setQueryParams( "TEMPLATE_NAME", name );
116         q.execute();
117         ResultSet rs = q.fetchResultSet();
118         if (rs.next())
119         {// Template exists
120
return true;
121         }
122         else
123         {// Template doesn't exist
124
return false;
125         }
126     }
127     
128     /**
129      * Methode checked die Abhängigkeiten des Templates vor dem Löschen
130      *
131      * @param templateId die ID des Templates, zur Identifizierung in
132      * der DB
133      * @throws SQLException falls die ID nicht gefunden werden konnte
134      * @return references TKVector welcher die Abhängigkeiten enthält.
135      */

136     public static TKVector getDependencies(Integer JavaDoc templateId) throws SQLException
137     {
138         TKVector references = new TKVector();
139         
140         TKQuery q = TKDBManager.newQuery(CheckTemplateDependency.class);
141         q.setQueryParams( "TEMPLATE_ID", templateId );
142         q.execute();
143         ResultSet rs = q.fetchResultSet();
144         while (rs.next())
145         {
146             TKHashtable temp = new TKHashtable();
147             String JavaDoc name = rs.getString("PRESENTATION_NAME");
148             temp.put("REF2", name );
149             references.addElement(temp);
150         }
151         return references;
152     }
153 }
154
Popular Tags