package group04; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import org.springframework.context.support.ClassPathXmlApplicationContext; public class nodeOptConsol { public static BasicNode current = null; private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springmvc-config.xml"); public static ClassPathXmlApplicationContext getContext() { return context; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //nums.add(1); String command = ""; while(true) { try { if(current != null) System.out.print(current.getClass() + "#" + current.getId()); System.out.print(">"); command = br.readLine(); } catch (Exception e) { System.out.println("errer : " + e.toString()); } String[] prams = command.split(" "); command = prams[0]; if(prams.length > 1) { prams = Arrays.copyOfRange(prams, 1, prams.length); } // System.out.println(command); switch(command.toLowerCase()) { //select node from database or loaded node case "sel": if(prams.length > 0) { try { long id = Long.valueOf(prams[0]); current = NodeDao.loadNodeTreeById(id); } catch(NumberFormatException ex) { System.out.println("can't id press to number"); } } break; case "log": System.out.println(current); break; //parent node case "par": if(current != null) current = current.getParentNode(); break; //list node case "ls": if(current != null) System.out.println(current.getChildLevel()); else System.out.println("null"); break; //move node to case "mov": if(prams.length > 0) { try { long id = Long.valueOf(prams[0]); BasicNode target = NodeDao.loadNodeTreeById(id); if(target != null) { if(prams.length > 1) { switch(prams[1].toLowerCase()) { //next of target case "/n": target.insertNext(current); break; //previous node of target case "/p": target.insertPerv(current); break; } } else { target.insertNext(current); } } } catch(NumberFormatException ex) { System.out.println("can't id press to number"); } } break; //replace node case "rp": if(prams.length > 0) { try { long id = Long.valueOf(prams[0]); BasicNode target = NodeDao.loadNodeTreeById(id); if(target != null) { if(prams.length > 1) { switch(prams[1].toLowerCase()) { //replace current case "/c": current.replaceBy(target); current = target; break; //replace target case "/t": target.replaceBy(current); break; case "/e": current.exchangeWith(target); break; } } else { current.replaceBy(target); current = target; } } } catch(NumberFormatException ex) { System.out.println("can't id press to number"); } } break; //create node case "new": if(prams.length > 0) { try { long id = Long.valueOf(prams[0]); BasicNode newNode = new BasicNode(id); if(newNode.defineded()) current = newNode; } catch(NumberFormatException ex) { System.out.println("can't id press to number"); } } break; case "del": if(current != null) current.deleteNode(); current = null; break; //set level parent node case "slp": if(prams.length > 0) { try { long id = Long.valueOf(prams[0]); BasicNode target = NodeDao.loadNodeTreeById(id); if(target != null) { target.setChildLevel(current); } } catch(NumberFormatException ex) { System.out.println("can't id press to number"); } } break; //join child level case "jcl": if(prams.length > 0) { try { long id = Long.valueOf(prams[0]); BasicNode target = NodeDao.loadNodeTreeById(id); if(target != null) { if(target.getChildLevel() == null) { if(current != null) { current.dropFromLevel(); target.setChildLevel(current); } } else { target.getChildLevel().endOfNodeLevel().insertNext(current); } } } catch(NumberFormatException ex) { System.out.println("can't id press to number"); } } break; case "ex": BasicNode.Commit(); return; } } } }