nodeOptConsol.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package group04;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.Arrays;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class nodeOptConsol {
  8. public static BasicNode current = null;
  9. private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springmvc-config.xml");
  10. public static ClassPathXmlApplicationContext getContext()
  11. {
  12. return context;
  13. }
  14. public static void main(String[] args) throws IOException {
  15. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  16. //nums.add(1);
  17. String command = "";
  18. while(true)
  19. {
  20. try
  21. {
  22. if(current != null) System.out.print(current.getClass() + "#" + current.getId());
  23. System.out.print(">");
  24. command = br.readLine();
  25. }
  26. catch (Exception e)
  27. {
  28. System.out.println("errer : " + e.toString());
  29. }
  30. String[] prams = command.split(" ");
  31. command = prams[0];
  32. if(prams.length > 1)
  33. {
  34. prams = Arrays.copyOfRange(prams, 1, prams.length);
  35. }
  36. // System.out.println(command);
  37. switch(command.toLowerCase())
  38. {
  39. //select node from database or loaded node
  40. case "sel":
  41. if(prams.length > 0)
  42. {
  43. try
  44. {
  45. long id = Long.valueOf(prams[0]);
  46. current = NodeDao.loadNodeTreeById(id);
  47. }
  48. catch(NumberFormatException ex)
  49. {
  50. System.out.println("can't id press to number");
  51. }
  52. }
  53. break;
  54. case "log":
  55. System.out.println(current);
  56. break;
  57. //parent node
  58. case "par":
  59. if(current != null) current = current.getParentNode();
  60. break;
  61. //list node
  62. case "ls":
  63. if(current != null) System.out.println(current.getChildLevel());
  64. else System.out.println("null");
  65. break;
  66. //move node to
  67. case "mov":
  68. if(prams.length > 0)
  69. {
  70. try
  71. {
  72. long id = Long.valueOf(prams[0]);
  73. BasicNode target = NodeDao.loadNodeTreeById(id);
  74. if(target != null)
  75. {
  76. if(prams.length > 1)
  77. {
  78. switch(prams[1].toLowerCase())
  79. {
  80. //next of target
  81. case "/n":
  82. target.insertNext(current);
  83. break;
  84. //previous node of target
  85. case "/p":
  86. target.insertPerv(current);
  87. break;
  88. }
  89. }
  90. else
  91. {
  92. target.insertNext(current);
  93. }
  94. }
  95. }
  96. catch(NumberFormatException ex)
  97. {
  98. System.out.println("can't id press to number");
  99. }
  100. }
  101. break;
  102. //replace node
  103. case "rp":
  104. if(prams.length > 0)
  105. {
  106. try
  107. {
  108. long id = Long.valueOf(prams[0]);
  109. BasicNode target = NodeDao.loadNodeTreeById(id);
  110. if(target != null)
  111. {
  112. if(prams.length > 1)
  113. {
  114. switch(prams[1].toLowerCase())
  115. {
  116. //replace current
  117. case "/c":
  118. current.replaceBy(target);
  119. current = target;
  120. break;
  121. //replace target
  122. case "/t":
  123. target.replaceBy(current);
  124. break;
  125. case "/e":
  126. current.exchangeWith(target);
  127. break;
  128. }
  129. }
  130. else
  131. {
  132. current.replaceBy(target);
  133. current = target;
  134. }
  135. }
  136. }
  137. catch(NumberFormatException ex)
  138. {
  139. System.out.println("can't id press to number");
  140. }
  141. }
  142. break;
  143. //create node
  144. case "new":
  145. if(prams.length > 0)
  146. {
  147. try
  148. {
  149. long id = Long.valueOf(prams[0]);
  150. BasicNode newNode = new BasicNode(id);
  151. if(newNode.defineded()) current = newNode;
  152. }
  153. catch(NumberFormatException ex)
  154. {
  155. System.out.println("can't id press to number");
  156. }
  157. }
  158. break;
  159. case "del":
  160. if(current != null) current.deleteNode();
  161. current = null;
  162. break;
  163. //set level parent node
  164. case "slp":
  165. if(prams.length > 0)
  166. {
  167. try
  168. {
  169. long id = Long.valueOf(prams[0]);
  170. BasicNode target = NodeDao.loadNodeTreeById(id);
  171. if(target != null)
  172. {
  173. target.setChildLevel(current);
  174. }
  175. }
  176. catch(NumberFormatException ex)
  177. {
  178. System.out.println("can't id press to number");
  179. }
  180. }
  181. break;
  182. //join child level
  183. case "jcl":
  184. if(prams.length > 0)
  185. {
  186. try
  187. {
  188. long id = Long.valueOf(prams[0]);
  189. BasicNode target = NodeDao.loadNodeTreeById(id);
  190. if(target != null)
  191. {
  192. if(target.getChildLevel() == null)
  193. {
  194. if(current != null)
  195. {
  196. current.dropFromLevel();
  197. target.setChildLevel(current);
  198. }
  199. }
  200. else
  201. {
  202. target.getChildLevel().endOfNodeLevel().insertNext(current);
  203. }
  204. }
  205. }
  206. catch(NumberFormatException ex)
  207. {
  208. System.out.println("can't id press to number");
  209. }
  210. }
  211. break;
  212. case "ex":
  213. BasicNode.checkAndUploadAll();
  214. return;
  215. }
  216. }
  217. }
  218. }