View Javadoc

1   package org.paneris.paneris.model;
2   
3   import java.sql.Connection;
4   import java.sql.PreparedStatement;
5   import java.sql.ResultSet;
6   
7   import org.paneris.jal.model.DBConnectionManager;
8   import org.paneris.jal.model.DDField;
9   import org.paneris.jal.model.DDRecord;
10  import org.paneris.jal.model.SystemProperties;
11  import org.paneris.user.model.User;
12  
13  
14  /**
15   * Use specific page fragment.
16   * 
17   * @author timp
18   *
19   */
20  public class PageContent  {
21  
22      private boolean adminLink = false;
23      private String userid = "0";
24  
25      /**
26       * Constructor.
27       * 
28       * @param user for whom this is relevant content.
29       */
30      public PageContent(User user) {
31          if (user != null && user.isLoggedOn()) {
32            userid = user.getFieldValue("id");
33           if (user.isAdministrator()) adminLink = true;
34          }
35      }
36  
37      public DDRecord getRecord(String db, String section) {
38          DBConnectionManager connMgr = DBConnectionManager.getInstance();
39          Connection conn;
40          DDRecord res = null;
41          conn = connMgr.getConnection("PageContent",db);
42          if (conn == null) return res;
43          try {
44              String sqlString = "SELECT * FROM pagecontents WHERE contentname=?";
45              PreparedStatement sqlStatement = conn.prepareStatement(sqlString);
46              sqlStatement.setObject(1, section);
47              ResultSet rs = sqlStatement.executeQuery();
48              if (rs.next())
49                  res = new DDRecord(db,"pagecontents",rs);
50          } catch (Exception e) {
51              throw new RuntimeException(e.toString());
52          }
53          connMgr.freeConnection(db, conn);
54          return res;
55      }
56  
57      public String getContent(String db, String section) {
58  
59         String editIcon = "edit";
60         try {
61             SystemProperties sp = new SystemProperties(db);
62             editIcon = sp.getProperty("admin_edit_icon");
63          } catch (Exception e) {
64             editIcon = "edit";
65          }
66  
67          DDRecord res = getRecord(db, section);
68          String contents = null;
69          try {
70              if (res != null) {
71                  contents = ((DDField) res.get("content")).getDisplayValue();
72                  if (adminLink) {
73                      contents += "<a href=org.paneris.jal.controller.Admin?db="+db+
74                                  "&table=pagecontents";
75                      contents += "&id=" + 
76                      ((DDField) res.get("id")).getDisplayValue()+"&action=edit";
77                   }
78              } else {
79                  contents = "<!-- Couldn't get pagecontents for \""+section+"\" -->\n";
80                  if (adminLink) {
81                    contents += "<a href=org.paneris.jal.controller.Admin?db="+db+
82                                "&table=pagecontents";
83                    contents += "&id=0&" +
84                                  "contentname="+section +
85                                  "&action=edit&user_key=" + userid ;
86                  }
87              }
88              if (adminLink) contents += ">" + editIcon + "</a>";
89          } catch (Exception e) {
90              contents = e.toString();
91              System.err.println(e);
92          }
93          return contents;
94      }
95  
96  /*
97  Useage:
98  #set $article = $content.getDDRecord($db,"articles",$article_id)
99  */
100     public DDRecord getDDRecord(String db, String table, String id) {
101         DBConnectionManager connMgr = DBConnectionManager.getInstance();
102         Connection conn;
103         DDRecord returnDDR = null;
104         conn = connMgr.getConnection("PageContent",db);
105         if (conn == null) return returnDDR;
106         try {
107             returnDDR = new DDRecord(db,table,new Integer(id));
108         } catch (Exception e) {
109             throw new RuntimeException(e.toString());
110         }
111         connMgr.freeConnection(db, conn);
112         return returnDDR;
113     }
114 }