import rose.*;
import java.util.*;

public class RoseExample2 {

    private RoseApplication a;
    private IRoseModel m;

    public static void main(String[] args) throws
    java.net.UnknownHostException,
    java.io.IOException,

    com.linar.jintegra.AutomationException {
	com.linar.jintegra.AuthInfo.setDefault("CMIS", "shijian", "Laiyang1");
        RoseExample2 x = new RoseExample2();
        RoseModel rm1 =  new RoseModel();
        x.a = new rose.RoseApplication(rm1.getApplication());
        //x.a.setVisible(true);
        
        //x.m= x.a.getCurrentModel();setCurrentModel
        IRoseModel rm =x.a.newModel();
        x.m=rm;
        rm.setName("newTest");
        x.a.setVisible(true);
     /*   

//	x.a.openModel("C:\\Data\\Isolde\\Tamot2\\Current\\test.mdl");
        RoseCategory rc= new rose.RoseCategory();
        //IRoseCategory rhs =rc.addCategory("New cat");() 
        rc.setName("New cat"); 
        IRoseCategory rhs =x.m.getRootCategory(); //setRootCategory( rc);
        rhs.addCategory("New cataaaa2");
        
        IRoseCategory useCat =x.m.getRootUseCaseCategory();
        
        IRoseCategory useCat2=useCat.addCategory("New use Cat");
        useCat.addUseCase("New Usecase");
        IRoseClassDiagram  ucsCaseDia = useCat.addClassDiagram("Usecase diagram");
        //RoseUseCase useCase1 = new RoseUseCase(); 
        IRoseUseCase useCase1=useCat.addUseCase("use case in dia");
        IRoseUseCase useCase11=useCat.addUseCase("use case in dia11");
        String associationName = "";
        String associationType = "use case in dia11";
        
        IRoseAssociation asso=useCase1.addAssociation(associationName,associationType);
        asso.setName("use");
        IRoseRole role;
        IRoseRoleCollection roleCol = useCase11.getRoles();
        for (short i = 1; i< roleCol.getCount(); i++){
        	role=(IRoseRole)roleCol.getAt(i);
        	if (role != null)
				System.out.println("role name= "+role.getName() );
        }
		ucsCaseDia.addAssociation(asso);    
	
		role = asso.getRole1();
		asso.setRoleForNameDirection(role);
		
		if (asso.nameIsDirectional())
			System.out.println("directional association");
		else {
			System.out.println("non-directional association");
			role = asso.getRole1();
        	if (role != null)
				System.out.println("role1 name= "+role.getAssociateItem().getName() );
			role = asso.getRole2();
        	if (role != null)
				System.out.println("role2 name= "+role.getAssociateItem().getName() );
		}
		*/
/*		
        //String useCaseName = "use case in dia";
        //useCase1.setName(useCaseName);
        ucsCaseDia.addUseCase(useCase1);
        ucsCaseDia.addUseCase(useCase11);
        useCat2.addUseCase("New Usecase2");
        
        IRoseUseCase useCase2 = useCat.addUseCase("use case with dia");;
        useCase2.addUseCaseDiagram("another usercase diag");
        
		//add scenario daigram to a usecase
        short type = 1;
        IRoseScenarioDiagram sceD =useCase1.addScenarioDiagram("scenario dia 1", type);
		//RoseScenarioDiagram sceD = new RoseScenarioDiagram();
		//sceD.setName("scenario dia 1");
		//IRoseScenarioDiagramCollectionProxy sceDiaCol = new IRoseScenarioDiagramCollectionProxy();
		//sceDiaCol.add(sceD);
		//useCase1.setScenarioDiagrams(sceDiaCol);
		
		//add usecase to usecase category
		//IRoseUseCaseCollectionProxy useCaseCol = new IRoseUseCaseCollectionProxy();
		//useCaseCol.add(useCase1);
		//useCat.setUseCases(useCaseCol);
        useCat2.addUseCase(useCase1.getName());
		
        //add object into scenario diagram
        IRoseObjectInstance user1 = sceD.addInstance("user1", "user");
        IRoseObjectInstance fileMenu = sceD.addInstance("file menu", "menu");
        sceD.addInstanceView(user1, true);
        sceD.addInstanceView(fileMenu, true);
        
        //add message
        String message ="select save menuitem";
        short sequenceNo = 1;
		sceD.createMessage(message, user1, fileMenu,sequenceNo);
		
		//add method
		String appClassName = "Application";
        IRoseObjectInstance system = sceD.addInstance("system", appClassName);
		//IRoseClass systemClass = system.zz_getClass();
        IRoseClass appClass=rhs.addClass(appClassName);
        //RoseClass appClass = new RoseClass();
        //appClass.setName(appClassName);
        
        String opName ="display";
		String returnType = "void";
		appClass.addOperation(opName, returnType);
        sceD.addInstanceView(system, true);
        sequenceNo = 2;
		sceD.createMessage(opName, fileMenu, system,sequenceNo);
		
		//create actor
		IRoseClass  editor =  useCat.addClass("Editor");
		editor.setStereotype("Actor");
        IRoseAssociation asso2=useCase11.addAssociation("","Editor");
        IRoseRole role2 = asso2.getRole1();
		asso2.setRoleForNameDirection(role2);
		ucsCaseDia.addClass(editor);
	//	ucsCaseDia.addAssociation(asso2);

		
		//get main usecase diagram getClassDiagrams
		IRoseClassDiagram ucMain = null;
		IRoseClassDiagram tempUCD = null;
		IRoseClassDiagramCollection ucDiaCol = useCat.getClassDiagrams();
        for (short i = 1; i< ucDiaCol.getCount(); i++){
        	tempUCD=(IRoseClassDiagram)ucDiaCol.getAt(i);
        	if (tempUCD.getName().equals("Main")){
        		ucMain= tempUCD;
				break;
        	}
        }
        if (ucMain != null){
        	ucMain.setName("design task with Tamot");
        } else         
        	  ucMain = useCat.addClassDiagram("design task with Tamot");
        IRoseUseCase useCaseMain=useCat.addUseCase("use case in main");
        ucMain.addUseCase(useCaseMain);
        	
        
        sceD.setVisible	(true);
        x.a.saveAs("D:\\data\\Isolde\\tmRose\\test.mdl", true);
			
		x.dump();
        x.a.release();
        
    */    
    }

    private void dump() throws java.io.IOException,
                               com.linar.jintegra.AutomationException {
        rose.IRoseUseCaseCollection useCategories = // IRoseCategoryCollection categories =
        	m.getAllUseCases(); 	//getRootCategory().getAllCategories();
        short useCount = useCategories.getCount();
		System.out.println("it's working"+ useCount);
        for (short i=1; i <= useCount; i++) {
            System.out.println(useCategories.getAt(i).getName());
        }
        IRoseCategoryCollection categories =
        	m.getRootCategory().getAllCategories();
        short categoriesCount = categories.getCount();
		System.out.println("it's working"+ categoriesCount);
        for (short i=1; i <= categoriesCount; i++) {
            System.out.println(categories.getAt(i).getName());
        }

    }

}