01: package org.jasig.portal.layout.alm;
02:
03: import java.sql.ResultSet;
04: import java.sql.SQLException;
05: import java.sql.Types;
06: import java.util.HashMap;
07: import java.util.Map;
08:
09: import javax.sql.DataSource;
10:
11: import org.springframework.jdbc.core.JdbcTemplate;
12: import org.springframework.jdbc.core.RowCallbackHandler;
13:
14: /**
15: * Data access object for getting the attributes of a ALM fragment node.
16: */
17: public class ALNodeAttributeDao {
18:
19: private JdbcTemplate attributeReadTemplate;
20:
21: private JdbcTemplate attributeInsertTemplate;
22:
23: private JdbcTemplate attributeUpdateTemplate;
24:
25: public ALNodeAttributeDao(DataSource dataSource) {
26: this .attributeReadTemplate = new JdbcTemplate(dataSource);
27:
28: this .attributeInsertTemplate = new JdbcTemplate(dataSource);
29: this .attributeInsertTemplate.setMaxRows(1);
30:
31: this .attributeUpdateTemplate = new JdbcTemplate(dataSource);
32: this .attributeUpdateTemplate.setMaxRows(1);
33: }
34:
35: /**
36: * Returns a Map from String attribute names to String attribute values.
37: * @param fragmentId
38: * @param nodeId
39: * @return
40: */
41: public Map attributesForFragmentNode(int fragmentId, int nodeId) {
42:
43: final Map map = new HashMap();
44:
45: String sql = "select param_name, param_value from up_fragment_param where "
46: + "fragment_id = ? and node_id = ?";
47: final Object[] params = new Object[] { new Integer(fragmentId),
48: new Integer(nodeId) };
49:
50: attributeReadTemplate.query(sql, params,
51: new RowCallbackHandler() {
52: public void processRow(ResultSet rs)
53: throws SQLException {
54: String paramName = rs.getString("param_name");
55: String paramValue = rs.getString("param_value");
56: map.put(paramName, paramValue);
57: }
58: });
59:
60: return map;
61: }
62:
63: public int insertFragmentNodeAttribute(int fragmentId, int nodeId,
64: String attributeName, String attributeValue) {
65: String sql = "insert into up_fragment_param (fragment_id, node_id, param_name, param_value) values (?, ?, ?, ?)";
66: Object[] params = new Object[] { new Integer(fragmentId),
67: new Integer(nodeId), attributeName, attributeValue };
68:
69: int[] types = new int[] { Types.INTEGER, Types.INTEGER,
70: Types.VARCHAR, Types.VARCHAR };
71:
72: return attributeInsertTemplate.update(sql, params, types);
73:
74: }
75:
76: public int setFragmentNodeAttribute(int fragmentId, int nodeId,
77: String attributeName, String attributeValue) {
78: String sql = "update up_fragment_param set param_value = ? where fragment_id = ? and node_id = ? and param_name = ?";
79: Object[] params = new Object[] { attributeValue,
80: new Integer(fragmentId), new Integer(nodeId),
81: attributeName };
82:
83: int[] types = new int[] { Types.VARCHAR, Types.INTEGER,
84: Types.INTEGER, Types.VARCHAR };
85:
86: return attributeUpdateTemplate.update(sql, params, types);
87:
88: }
89:
90: }
|