NxFilter Tutorial
Tutorial Index

DAO and Data classes
In typical web programming, dealing with DB is almost everything. We are using DAO (Data Access Object) and Data classes for manipulating DB.


Common methods for a data access object
We have some common methods for most data access object classes. For example, on policy,policy.jsp we use PolicyDao and PolicyData class for manipulating policies. PolicyDao has the following methods.


		public int selectCount() : The number of policies
		public List selectList() : Fetching policies as a list
		public PolicyData selectOne(int id) : Fetching one policy by ID column
		public boolean insert(PolicyData data) : Insert a new policy
		public boolean update(PolicyData data) : Update a existing policy
		public boolean delete(int id) : Delete a policy by ID column
	

Every policy data has its own unique ID which is a number and we use this ID for finding, updating a policy data.


Insert, delete, update, select data
If we want to modify whitelist,domain.jsp, we have to use WhitelistDomainDao and WhitelistData classes.

To insert a new data,


		<%
		WhitelistDomainDao dao = new WhitelistDomainDao();

		WhitelistData data = new WhitelistData();
		data.domain = "*.nxfilter.org";
		data.bypassAuth = true;
		data.bypassFilter = true;

		dao.insert(data);
		%>
	

To delete a data when its ID is 12,


		<%
		WhitelistDomainDao dao = new WhitelistDomainDao();
		dao.delete(12);
		%>
	

To select a data when its ID is 12,


		<%
		WhitelistDomainDao dao = new WhitelistDomainDao();
		WhitelistData data = dao.selectOne(12);
		%>
	

And to update the selected data,


		<%
		data.bypassFilter = false;
		dao.update(data);
		%>
	

Lastly, to list data.


		<%
		WhitelistDomainDao dao = new WhitelistDomainDao();
		List dataList = dao.selectList();
		for(WhitelistData data : dataList){
			out.println(data.domain + "<br>");
		}
		%>
	


Accessing data fields
Many Java developers are using 'get' and 'set' accessors for encapsulation and additional data processing. But for the sake of simplicity, we access public fields directly in most cases. For example, you get an instance of UserData and uses its 'name' property like the code below,


		<%
		UserData data = new UserDao().selectOne(1);
		out.println(data.name)
		%>
	

However, there are some data classes having methods starting with 'get'. These methods are mostly about formatting. We have 'ctime' property for RequestData which we use on 'Logging > Request'. If you use it directly you get '201507081415' but when you use its 'getCtime()' method you get '07/08 14:14'.