001: package de.webman.template.eventhandler;
002:
003: import java.io.*;
004: import java.sql.*;
005: import com.teamkonzept.db.*;
006: import com.teamkonzept.webman.WebManEvent;
007: import com.teamkonzept.lib.*;
008: import com.teamkonzept.lib.templates.*;
009: import com.teamkonzept.webman.mainint.events.*;
010: import com.teamkonzept.webman.mainint.*;
011: import com.teamkonzept.web.*;
012: import com.oroinc.text.regex.*;
013: import com.teamkonzept.webman.mainint.db.queries.template.CheckTemplateDependency;
014: import com.teamkonzept.webman.mainint.db.queries.template.CheckTemplateExists;
015:
016: /**
017: * Utility Klasse für die "TE-" -EventHandler
018: *
019: * @author $Author: tina $
020: * @version $Revision: 1.5 $
021: */
022: public class TEUtils implements TemplateTypes, UserCodes,
023: ParameterTypes {
024: /**
025: * Methode zum Anzeigen des Templatetextes.
026: *
027: * @param evt über den Event werden die verschiedenen Parameter
028: * wie TEMPLATE_NAME, etc. ausgewertet
029: * @throws Throwable schiefgehen kann immer etwas
030: * @throws FileNotFoundException im Falle das File existiert gar nicht
031: * @return true einfach nur um zusagen das alles okay ist
032: */
033: public static boolean showSavedText(TKEvent evt) throws Throwable,
034: FileNotFoundException {
035: try {
036: TKHTMLTemplate t = evt
037: .getPrepHTMLTemplate("te_editTmpl.tmpl");
038: String tmplName = evt.getParameter(PARAMETER,
039: "TEMPLATE_NAME");
040: String tmplId = evt.getParameter(PARAMETER, "TEMPLATE_ID");
041: t.set("TEMPLATE_NAME", tmplName);
042: t.set("TEMPLATE_ID", tmplId);
043:
044: // Text bekommen !!!
045: String s = TKTemplateCache
046: .getSource("file://"
047: + evt.getHttpInterface().getDocumentRoot()
048: + File.separator
049: + TemplateUtils.getGenerationDirectory()
050: + tmplName);
051: t.set("TEMPLATE_TEXT", s);
052:
053: WebManEvent.fillEventsIntoTemplate(evt.getRemoteUser(), t,
054: ContextConstants.TEMPLATES);
055: evt.finishTemplate(t);
056: } catch (Throwable e) {
057: throw WebmanExceptionHandler.getException(e);
058: }
059: return true;
060: }
061:
062: /**
063: * Methode prüft ob der eingegebene Templatename eine gültige Endung hat.
064: *
065: * @param name der Name des Templates.
066: * @throws TKException warum weiß ich auch nicht.
067: * @throws Throwable falls was schief geht
068: * @return cName Templatename
069: */
070: public static boolean isValidTemplateName(String name)
071: throws TKException, Throwable {
072: try {
073: String cName = name.toUpperCase();
074: // hier noch auf Sonderzeichen pruefen !
075: isValidName(name);
076: return cName.endsWith(TK_TEMPLATE)
077: || cName.endsWith(JSP_TEMPLATE)
078: || cName.endsWith(XSLT_TEMPLATE);
079: } catch (Throwable e) {
080: throw WebmanExceptionHandler.getException(e);
081: }
082: }
083:
084: public static boolean isValidName(String name) throws Throwable {
085: if (name == null || name.equals("")) {
086: // leer ist natuerlich auch nicht gut...
087: throw new TKUserException(
088: "Es muß ein Name angegeben werden", NO_PATHNAME,
089: USER_SEVERITY, true, null);
090: }
091:
092: PatternMatcher matcher = TKReg.getMatcher();
093: PatternCompiler compiler = TKReg.getCompiler();
094: Pattern patNotAllowed;
095:
096: // nur wordcharacter (alphanumeric) fuer dirnamen: a-zA-Z_0-9 und das -
097: // to DO :: Das paßt nicht zur Localisation !!!
098: patNotAllowed = compiler.compile("[^a-zA-Z_0-9\\-./]");
099: if (matcher.contains(name, patNotAllowed)) {
100: // kein gueltiger TemplateName
101: throw new TKUserException(
102: "Kein gültiger Pfadname: " + name,
103: NO_VALID_PATHNAME, USER_SEVERITY, true, null);
104: } else {
105: return true;
106: }
107: }
108:
109: /**
110: * Diese Methode soll gucken ob es dieses Template (beim Neuanlegen) schon
111: * gibt, und wenn ja den user informieren
112: *
113: * @param name der zuüberprüfende Templatename
114: * @throws Throwable
115: * @return true wenn der Templatename in der DB bereits existitert.
116: *
117: */
118: public static boolean templateNameExists(String name)
119: throws Throwable {
120: TKQuery q = TKDBManager.newQuery(CheckTemplateExists.class);
121: q.setQueryParams("TEMPLATE_NAME", name);
122: q.execute();
123: ResultSet rs = q.fetchResultSet();
124: if (rs.next()) {// Template exists
125: return true;
126: } else {// Template doesn't exist
127: return false;
128: }
129: }
130:
131: /**
132: * Methode checked die Abhängigkeiten des Templates vor dem Löschen
133: *
134: * @param templateId die ID des Templates, zur Identifizierung in
135: * der DB
136: * @throws SQLException falls die ID nicht gefunden werden konnte
137: * @return references TKVector welcher die Abhängigkeiten enthält.
138: */
139: public static TKVector getDependencies(Integer templateId)
140: throws SQLException {
141: TKVector references = new TKVector();
142:
143: TKQuery q = TKDBManager.newQuery(CheckTemplateDependency.class);
144: q.setQueryParams("TEMPLATE_ID", templateId);
145: q.execute();
146: ResultSet rs = q.fetchResultSet();
147: while (rs.next()) {
148: TKHashtable temp = new TKHashtable();
149: String name = rs.getString("PRESENTATION_NAME");
150: temp.put("REF2", name);
151: references.addElement(temp);
152: }
153: return references;
154: }
155: }
|