changeset 168:54d68cda84c4

create edit Attribute & Folder Method
author tatsuki
date Tue, 29 Jul 2014 18:51:44 +0900
parents 91f6dd655a01
children 2403b9a4416f
files src/main/java/alice/jungle/persistent/PersistentJournal.java src/main/java/app/bbs/NetworkBulletinBoard.java src/main/java/app/bbs/NetworkJungleBulletinBoard.java src/main/java/app/bbs/codesegment/StartBBSCodeSegment.java src/main/java/app/bbs/thinks/EditAttribute.java src/main/java/app/bbs/thinks/EditFolderName.java src/main/java/app/bbs/thinks/ShowMatrix.java src/test/java/alice/jungle/PersistentJournalTest.java
diffstat 8 files changed, 203 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/alice/jungle/persistent/PersistentJournal.java	Tue Jul 29 10:58:11 2014 +0900
+++ b/src/main/java/alice/jungle/persistent/PersistentJournal.java	Tue Jul 29 18:51:44 2014 +0900
@@ -18,6 +18,7 @@
 	private ChangeListReader READER;
 	private OutputStream out;
 	private InputStream in;
+	private String logFileName;
 
 	public PersistentJournal() {
 	}
@@ -25,7 +26,7 @@
 	public PersistentJournal(File file) throws FileNotFoundException {
 		out = new FileOutputStream(file,true);
 		in = new FileInputStream(file);
-		// WRITER = new PersistentChangeListWriter(out);
+		//「 WRITER = new PersistentChangeListWriter(out);
 		READER = new PersistentChangeListReader(in);
 	}
 	
@@ -37,7 +38,7 @@
 	@Override
 	public ChangeListWriter getWriter() {
 		String timeStamp = Long.toString(System.currentTimeMillis());
-		String logFileName = timeStamp + ".log";
+		logFileName = timeStamp + ".log";
 		OutputStream outStream = null;
 		try {
 			outStream = new FileOutputStream(new File("./log/"+logFileName));
@@ -74,6 +75,10 @@
 		return in;
 	}
 	
+	public String getLogName(){
+		return logFileName;
+	}
+	
 	public void close() throws IOException {
 		out.close();
 		in.close();
--- a/src/main/java/app/bbs/NetworkBulletinBoard.java	Tue Jul 29 10:58:11 2014 +0900
+++ b/src/main/java/app/bbs/NetworkBulletinBoard.java	Tue Jul 29 18:51:44 2014 +0900
@@ -1,6 +1,5 @@
 package app.bbs;
 
-import java.nio.ByteBuffer;
 
 import app.bbs.thinks.getAttributeImp;
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.BoardMessage;
@@ -15,4 +14,5 @@
 	public void createFolder(String boardName, String author, String msg,String key, String _nodeNum);
 	public void createAttribute(String boardName, String uuid, String author, String msg, String key);
 	public getAttributeImp getAttribute(String _bname, String nodeNum);
+	public void editAttribute(String boardName, String path, String id, String message);
 }
--- a/src/main/java/app/bbs/NetworkJungleBulletinBoard.java	Tue Jul 29 10:58:11 2014 +0900
+++ b/src/main/java/app/bbs/NetworkJungleBulletinBoard.java	Tue Jul 29 18:51:44 2014 +0900
@@ -299,7 +299,7 @@
 
 	}
 
-	public void editMessage(String _board, String _uuid, final String _author, final String _message, final String _editKey) {
+	public void editMessage(String _board, String _nodeNum, final String _author, final String _message, final String _editKey) {
 		requestCounter.incrementAndGet();
 		final long timestamp = System.currentTimeMillis();
 		final ByteBuffer tBuffer = ByteBuffer.allocate(16);
@@ -309,8 +309,13 @@
 	
 		do {
 			DefaultNodePath path = new DefaultNodePath();
-			path = path.add(Integer.parseInt(_uuid));
-
+			try {
+				for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) {
+					if (!_nodeNum.substring(count, count + 1).equals("/"))
+						path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1)));
+				}
+			} catch (Exception _e) {
+			}
 			JungleTreeEditor editor = tree.getTreeEditor();
 			NodeEditor e = new NodeEditor() {
 				public <T extends EditableNode<T>> Either<Error, T> edit(T _e) {
@@ -356,7 +361,7 @@
 					int count = 0;
 						for (; _e.getAttributes().get("mes" + String.valueOf(count)) != null; count++) {
 						}
-						str = String.valueOf(count);
+					str = String.valueOf(count);
 					_e = _e.getAttributes().put("mes" + str, ByteBuffer.wrap(_message.getBytes())).b();
 					_e = _e.getAttributes().put("timestamp" + str, tBuffer).b();
 					return DefaultEither.newB(_e);
@@ -371,6 +376,42 @@
 		} while (either.isA());
 	}
 
+	public void editAttribute(String boardName, String _path, String id, String _message) {
+		requestCounter.incrementAndGet();
+		final long timestamp = System.currentTimeMillis();
+		final ByteBuffer tBuffer = ByteBuffer.allocate(16);
+		tBuffer.putLong(timestamp);
+		JungleTree tree = jungle.getTreeByName(boardName);
+		Either<Error, JungleTreeEditor> either = null;
+		DefaultNodePath path = new DefaultNodePath();
+		do {
+			try {
+				for (int count = 0; _path.substring(count, count + 1) != null; count++) {
+					if (!_path.substring(count, count + 1).equals("/"))
+						path = path.add(Integer.parseInt(_path.substring(count,	count + 1)));
+				}
+			} catch (Exception _e) {
+			}
+			
+			JungleTreeEditor editor = tree.getTreeEditor();
+			NodeEditor e = new NodeEditor() {
+				public <T extends EditableNode<T>> Either<Error, T> edit(T _e) {
+					// EnableNodeWrapper<T> node = _e.getWrap();
+					_e = _e.getAttributes().put("mes" + id, ByteBuffer.wrap(_message.getBytes())).b();
+					_e = _e.getAttributes().put("timestamp" + id, tBuffer).b();
+					return DefaultEither.newB(_e);
+				}
+			};
+			either = editor.edit(path, e);
+			if (either.isA()) {
+				throw new IllegalStateException();
+			}
+			editor = either.b();
+			either = editor.success();
+		} while (either.isA());
+	}
+
+	
 	public void editMatrixMessage(String _board, String _uuid, final String _author, final String _message, final String _editKey) {
 		requestCounter.incrementAndGet();
 		final long timestamp = System.currentTimeMillis();
@@ -515,4 +556,6 @@
 
 	}
 
+
+
 }
--- a/src/main/java/app/bbs/codesegment/StartBBSCodeSegment.java	Tue Jul 29 10:58:11 2014 +0900
+++ b/src/main/java/app/bbs/codesegment/StartBBSCodeSegment.java	Tue Jul 29 18:51:44 2014 +0900
@@ -26,6 +26,8 @@
 import app.bbs.RequestNumCheckServlet;
 import app.bbs.ShowMessageWithTimeStampServlet;
 import app.bbs.thinks.CreateFolderMatrix;
+import app.bbs.thinks.EditAttribute;
+import app.bbs.thinks.EditFolderName;
 import app.bbs.thinks.ShowMatrix;
 import app.bbs.thinks.createAttributeMatrix;
 
@@ -79,17 +81,20 @@
         String showBoardMessagePath = "/showBoardMessage";
         String showMatrixPath = "/showMatrix";
         String createAttributePath = "/createAttribute";
+        String editAttributePath = "/editAttribute";
+        
         Server serv = new Server(bbsPort);
         ThreadPool thp = serv.getThreadPool();
         Servlet createBoardMessage = new CreateBoardMessageServlet(cassaBBS);
         Servlet createFolder = new CreateFolderMatrix(cassaBBS);
         Servlet createBoard = new CreateBoardServlet(cassaBBS);
-        Servlet editBoardMessage = new EditMessageServlet(cassaBBS);
+        Servlet editBoardMessage = new EditFolderName(cassaBBS);
         Servlet createAttribute = new createAttributeMatrix(cassaBBS);
+        Servlet editAttribute = new EditAttribute(cassaBBS);
         Servlet index = new ShowBoardsServlet(cassaBBS,createBoardPath,showBoardMessagePath);
         //    	Servlet board = new ShowBoardMessageServlet(cassaBBS,createBoardMessagePath,editMessagePath);
         Servlet board = new ShowMessageWithTimeStampServlet(cassaBBS,createBoardMessagePath,editMessagePath,showMatrixPath, thp);
-        Servlet matrix = new ShowMatrix(cassaBBS,createFolderPath,editMessagePath, showMatrixPath, createAttributePath,thp);
+        Servlet matrix = new ShowMatrix(cassaBBS,createFolderPath,editMessagePath, showMatrixPath, createAttributePath, editAttributePath,thp);
         
         ServletHandler context = new ServletHandler();
         context.addServletWithMapping(new ServletHolder(createBoardMessage),createBoardMessagePath);
@@ -98,6 +103,7 @@
         context.addServletWithMapping(new ServletHolder(editBoardMessage),editMessagePath);
         context.addServletWithMapping(new ServletHolder(createAttribute),createAttributePath);
         context.addServletWithMapping(new ServletHolder(index),"/");
+        context.addServletWithMapping(new ServletHolder(editAttribute),editAttributePath);
         context.addServletWithMapping(new ServletHolder(board),showBoardMessagePath);
         context.addServletWithMapping(new ServletHolder(matrix),showMatrixPath);
         /* 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/app/bbs/thinks/EditAttribute.java	Tue Jul 29 18:51:44 2014 +0900
@@ -0,0 +1,66 @@
+package app.bbs.thinks;
+
+import java.io.PrintWriter;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import app.bbs.NetworkBulletinBoard;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.BulletinBoard;
+
+public class EditAttribute extends HttpServlet
+{
+	private final NetworkBulletinBoard bbs;
+	private static final String PARAM_BOARD_NAME = "bname";
+	private static final String PARAM_BOARD_MSGID = "uuid";
+	private static final String PARAM_BOARD_MESSAGE= "msg";
+	private static final String PARAM_BOARD_EDITKEY = "key";
+	
+	private static final long serialVersionUID = 1L;
+	
+	public EditAttribute(NetworkBulletinBoard _bbs)
+	{
+		bbs = _bbs;
+	}
+	
+	public void doGet(HttpServletRequest _req,HttpServletResponse _res)
+	{
+		String bname = _req.getParameter(PARAM_BOARD_NAME);
+		String path = _req.getParameter("path");
+		String id = _req.getParameter("count");
+		
+		
+		try{
+			PrintWriter pw = _res.getWriter();
+			pw.write("<html><body><h1>edit message</h1>");
+			pw.write("<form method='POST'\n");
+			pw.write("<p><input type='hidden' name='path' value='"+path+"'/>" +
+					"<input type='hidden' name='id' value='"+id+"'/>" +
+					"<input type='hidden' name='bname' value='"+bname+"'</p>\n");
+			pw.write("<p>Message<br/> <input type='textarea' name='msg'/> </p>\n");
+			pw.write("<p><input type='submit' value='submit'/></p>\n");
+			pw.write("</body></html>");
+			pw.flush();
+		}catch(Exception _e){
+			_res.setStatus(500);
+		}
+	}
+
+	public void doPost(HttpServletRequest _req,HttpServletResponse _res)
+	{
+		String boardName = _req.getParameter(PARAM_BOARD_NAME);
+		String msg = _req.getParameter(PARAM_BOARD_MESSAGE);
+		String path = _req.getParameter("path");
+		String id = _req.getParameter("id");
+		
+		try{
+			bbs.editAttribute(boardName, path,id, msg);
+			PrintWriter pw = _res.getWriter();
+			pw.write("successfully written");
+			pw.flush();
+		}catch(Exception _e){
+			_res.setStatus(500);
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/app/bbs/thinks/EditFolderName.java	Tue Jul 29 18:51:44 2014 +0900
@@ -0,0 +1,66 @@
+package app.bbs.thinks;
+
+import java.io.PrintWriter;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.BulletinBoard;
+
+public class EditFolderName extends HttpServlet
+{
+	private final BulletinBoard bbs;
+	private static final String PARAM_BOARD_NAME = "bname";
+	private static final String PARAM_BOARD_MSGID = "uuid";
+	private static final String PARAM_BOARD_AUTHOR = "author";
+	private static final String PARAM_BOARD_MESSAGE= "msg";
+	private static final String PARAM_BOARD_EDITKEY = "key";
+	
+	private static final long serialVersionUID = 1L;
+	
+	public EditFolderName(BulletinBoard _bbs)
+	{
+		bbs = _bbs;
+	}
+	
+	public void doGet(HttpServletRequest _req,HttpServletResponse _res)
+	{
+		String bname = _req.getParameter(PARAM_BOARD_NAME);
+		String uuid = _req.getParameter(PARAM_BOARD_MSGID);
+		
+		
+		try{
+			PrintWriter pw = _res.getWriter();
+			pw.write("<html><body><h1>edit message</h1>");
+			pw.write("<form method='POST'\n");
+			pw.write("<p><input type='hidden' name='author'/>" +
+					"<input type='hidden' name='key' value='"+uuid+"'/>" +
+					"<input type='hidden' name='bname' value='"+bname+"'</p>\n");
+			pw.write("<p>Message<br/> <input type='textarea' name='msg'/> </p>\n");
+			pw.write("<p><input type='submit' value='submit'/></p>\n");
+			pw.write("</body></html>");
+			pw.flush();
+		}catch(Exception _e){
+			_res.setStatus(500);
+		}
+	}
+
+	public void doPost(HttpServletRequest _req,HttpServletResponse _res)
+	{
+		String boardName = _req.getParameter(PARAM_BOARD_NAME);
+		String author = _req.getParameter(PARAM_BOARD_AUTHOR);
+		String msg = _req.getParameter(PARAM_BOARD_MESSAGE);
+		String key = _req.getParameter(PARAM_BOARD_EDITKEY);
+		String uuid = _req.getParameter(PARAM_BOARD_MSGID);
+		
+		try{
+			bbs.editMessage(boardName, uuid, author, msg, key);
+			PrintWriter pw = _res.getWriter();
+			pw.write("successfully written");
+			pw.flush();
+		}catch(Exception _e){
+			_res.setStatus(500);
+		}
+	}
+}
--- a/src/main/java/app/bbs/thinks/ShowMatrix.java	Tue Jul 29 10:58:11 2014 +0900
+++ b/src/main/java/app/bbs/thinks/ShowMatrix.java	Tue Jul 29 18:51:44 2014 +0900
@@ -21,13 +21,15 @@
 	private final String editMessagePath;
 	private final String showMatrixPath;
 	private final String createAttributePath;
+	private final String editAttributePath;
 	private static final String PARAM_BOARD_NAME = "bname";
 
 	public ShowMatrix(NetworkBulletinBoard _bbs,
 			String _createBoardMessagePath, String _editMessagePath,
-			String _showMatrixPath, String _createAttributePath, ThreadPool thp) {
+			String _showMatrixPath, String _createAttributePath, String _editAttributePath, ThreadPool thp) {
 		bbs = _bbs;
 		createAttributePath = _createAttributePath;
+		editAttributePath = _editAttributePath;
 		showMatrixPath = _showMatrixPath;
 		createBoardMessagePath = _createBoardMessagePath;
 		editMessagePath = _editMessagePath;
@@ -75,16 +77,16 @@
 			_pw.write("<hr/>");
 			_pw.write("<small><a href=" + showMatrixPath + "?bname=" + _bname + "&uuid=" + nodeNum + "/" + msg.getUUID() + "&nodeName=" + msg.getMessage() + ">" + msg.getMessage() + "</a></small>");
 			_pw.write("   ");
-			_pw.write("<small><a href='" + createAttributePath + "?bname=" + _bname + "&uuid=" + nodeNum  + "/"+ msg.getUUID() + "'>edit(not s)</a></small>");
+			_pw.write("<small><a href='" + editMessagePath + "?bname=" + _bname + "&uuid=" + nodeNum  + "/"+ msg.getUUID() + "'>edit</a></small>");
 		}
 		
 		
 		_pw.write("<br><hr/><p>Value</p><br>");
 		getAttributeImp attribute = (bbs.getAttribute(_bname, nodeNum));
 		for (int count = 0; attribute.getMessage(count) != null; count++) {
-			_pw.write("<p><b>" + "Varue  " + count + " : " + attribute.getMessage(count) + "</b></p>");
+			_pw.write("<p><b>" + "Value  " + count + " :  </b>");
+			_pw.write("<a href='" + editAttributePath + "?bname=" + _bname + "&path=" + nodeNum  + "&count="+ count + "'>"  + attribute.getMessage(count) + "</a></p>");
 		}
-
 		_pw.write("</body></html>");
 		_pw.flush();
 	}
--- a/src/test/java/alice/jungle/PersistentJournalTest.java	Tue Jul 29 10:58:11 2014 +0900
+++ b/src/test/java/alice/jungle/PersistentJournalTest.java	Tue Jul 29 18:51:44 2014 +0900
@@ -44,8 +44,8 @@
 		assertFalse(either.isA());
 		
 		PersistentJournal journal2 = new PersistentJournal();
-		journal2.setInputFile(new File("./log/commit.log"));
-		journal2.setOutputFile(new File("./log/commit2.log"));
+		journal2.setInputFile(new File("./log/"+journal1.getLogName()));
+		journal2.setOutputFile(new File("./log/"+journal1.getLogName()));
 		Jungle jungle2 = new PersistentJungle(journal1, "uuid2", new DefaultTreeEditor(new DefaultTraverser()));
 		ChangeListReader reader = journal2.getReader();
 		for (ChangeList chList : reader) {