Tuesday, March 16, 2010

getDesignElementNames (in Java)

/**
     * Method to return the names of a particular set or all design elements from a database
     * depending upon the user's input
     * @param    targetDB-    The database from which the design elements needs to be obtained
     * @param    DESIGN_ELEMENT_TYPE - An integer representation of the design element type
     * @return    String[]
     * @author    karthikeyan_a
     * @since    29-April-2009
     * @see        NotesCollection selectDesigns(Database targetDB,int DESIGN_ELEMENT_TYPE)
     * @see        ArrayList filterHiddenElements(ArrayList designElementNames)
     * @see        ArrayList removeAlias(ArrayList designElementNames)
     * @see        String[] CreateStringArrayFromVector(ArrayList stringVector)
     */   
    public String[] getDesignElementNames(Database targetDB,int DESIGN_ELEMENT_TYPE)    {
       
        if (targetDB==null) return null;

        String noteID=null;
        String noteIDTemp=null;
        Document dsgnDoc=null;
        NoteCollection nc=null;
        ArrayList designElementNames=null;
        //initialize the return value
        String[] designElementNamesString=null;

        try {
            //create an empty note collection
            nc =selectDesigns(targetDB,DESIGN_ELEMENT_TYPE);
            //initiate return value
            designElementNames=new ArrayList();
           
            if (nc.getCount()>0){
                noteID=nc.getFirstNoteID();
                //check if the noteID is neither empty nor null and get the design doc's name associated with the id
                while (noteID!=null && !noteID.equals(""))    {     //start of noteID while       
                    try {       
                        noteIDTemp = noteID;
                        dsgnDoc=targetDB.getDocumentByID(noteIDTemp);
                        //add the names of the design elements into the vector
                        designElementNames.add(dsgnDoc.getItemValueString("$TITLE"));
                        noteID=nc.getNextNoteID(noteID);
                    } catch (NotesException ne)    {
                        ne.printStackTrace();
                    }   
                }    //end of noteID while
            }
        } catch (NotesException e) {
            e.printStackTrace();
        }       
       
        // remove alias names of design elements
        designElementNames=removeAlias(designElementNames);
        //filter hidden design elements
        designElementNames=filterHiddenElements(designElementNames);
        //create a string array of the resultant design elements name
        designElementNamesString=arrayListToStringArray(designElementNames);
       
        //recycle objects
        noteID=null;
        nc=null;
        noteIDTemp=null;
        dsgnDoc=null;
        targetDB=null;
        designElementNames=null;
       
        //return the array of names
        return designElementNamesString;
    }    //end of function::getDesignElementNames

    /**
     * Method to return a particular set or all design elements from a database
     * depending upon the user's input
     * @param    targetDB-    The database from which the design elements needs to be obtained
     * @param    DESIGN_ELEMENT_TYPE - An integer representation of the design element type
     * @return    NoteCollection
     * @author    karthikeyan_a
     * @since    29-April-2009
     */   
    public NoteCollection selectDesigns(Database targetDB,int DESIGN_ELEMENT_TYPE) throws NotesException    {
        NoteCollection nc=null;
        nc = targetDB.createNoteCollection(false);
        if (DESIGN_ELEMENT_TYPE==1)    {
            nc.setSelectForms(true);
        } else if(DESIGN_ELEMENT_TYPE==2)    {
            nc.setSelectViews(true);
            nc.setSelectFolders(true);
        } else    {
            nc=null;
            return nc;
        }
        //build the design document collection of the resultant collection
        nc.buildCollection();
        //return the design collection
        return nc;
    }    //end of function::selectDesigns

    /**
     * Method to remove the strings from a string vector (Vector) which corresponds
     * to the name of an hidden element
     * @param    designElementNames  - an ArrayList with String Objects
     * @return    ArrayList
     * @author    karthikeyan_a
     * @since    29-April-2009
     */
    public ArrayList filterHiddenElements(ArrayList designElementNames)    {
        //if the input param in null then return null
        if (designElementNames==null)return null;
        //initialize the return value
        ArrayList filteredDsgnElements=new ArrayList();
        String dsgnName=null;   
        Object[] buffer=designElementNames.toArray();
        int countItr=0;
        /**
         *    loop through all the string objects in the input vector and remove the string objects
         *    that correspond to the name of an hidden design element
         */
        for(countItr=0;countItr
            dsgnName=buffer[countItr].toString().trim();
            if ( !( (dsgnName.indexOf("(")==0) && (dsgnName.indexOf(")")==(dsgnName.length()-1)) ) )    {
                 filteredDsgnElements.add(dsgnName);
            }   
        }
        //recycle objects
        dsgnName=null;
        designElementNames=null;
       
        return filteredDsgnElements;
    }    //end of function::filterHiddenElements

    /**
     * Method to remove the part of strings from string objects in a string vector (Vector)
     * which correspond to the alias names of the design elements
     * @param    designElementNames  - an ArrayList with String Objects
     * @return    ArrayList
     * @author    Karthikeyan_a
     * @since    29-April-2009
     */   
    public ArrayList removeAlias(ArrayList designElementNames)    {
        //if the input parameter in null then return null
        if (designElementNames==null)return null;
        //initialize the return value
        ArrayList filteredDsgnElements=new ArrayList();       
        String dsgnName=null;       
        Object[] buffer=designElementNames.toArray();
        int countItr=0;
        /**
         *    loop through all the string objects in the input vector and remove the part of stirngs in
         *    string objects that correspond to the alias names of the design elements
         */
        for(countItr=0;countItr
            dsgnName=buffer[countItr].toString().trim();
            if(dsgnName.indexOf("|")==-1)    {
                filteredDsgnElements.add(dsgnName);
            }    else    {
                filteredDsgnElements.add(dsgnName.substring(0, dsgnName.indexOf("|")).trim());
            }   
        }   
        //recycle objects
        dsgnName=null;   
        designElementNames=null;
       
        return filteredDsgnElements;
    }    //end of function::removeAlias

    /**
     * Method to assimilate the contents of a string Array List into a string array
     * @param    arrList  - an array list with string objects
     * @return    String[]
     * @author    karthikeyan_a
     * @since    30-April-2009  
     */
    public String[] arrayListToStringArray(ArrayList arrList){
        String[] strArray=null;
        Object[] elements=arrList.toArray();
        strArray=new String[elements.length];
        int countItr=0;
        for (countItr=0;countItr
            strArray[countItr]=elements[countItr].toString();
        }
        //recycle objects       
        elements=null;
        arrList=null;
       
        return strArray;
    }

    /**
     * Method to obtain a collection of all documents from a specified view in a specified database
     * @param    targetDB  - the database from which the document collection is to be obtained
     * @param    viewName  - the name of the view from which the document collection is to be obtained
     * @return    DocumentCollection
     * @author    karthikeyan_a
     * @since    30-April-2009       
     */
    public DocumentCollection getViewDocuments(Database targetDB, String viewName)    {
        //if any of the input parameters in undefined then return null
        if ((targetDB==null)|| (viewName==null) || (viewName.trim().equals("")))    {
            return null;
        }
        //mark the return value and initialize other variables
        DocumentCollection viewDocs=null;
        Document viewDoc=null;
        ViewEntryCollection viewEntries=null;
        ViewEntry viewEntry=null;
        View chosenView=null;
        try {
            //set the handle for the view mentioned by the view name
            chosenView=targetDB.getView(viewName);
            //if the view handle is not set then return null
            if (chosenView==null)    {
                return null;
            }
           
            //ensure that an empty document collection is created
            Random generator = new Random();
            viewDocs=chosenView.getAllDocumentsByKey(".~^$#$^&~."+generator.toString());
           
            /**
             * loop through all the entries in the view and push their associated
             * documents into the document collection
             */
            viewEntries= chosenView.getAllEntries();
            //get the handle for the first entry in the collection
            viewEntry=viewEntries.getFirstEntry();
            while(viewEntry!=null)    {
                if (viewEntry.isDocument())    {
                    viewDoc=viewEntry.getDocument();
                    /*
                     * If the document concerned with the entry is already present in the
                     * collection then a duplicate exception is thrown. So catch the same and
                     * dont allow that to hurt the process
                     */
                    try {
                        viewDocs.addDocument(viewDoc);
                    }
                    catch (NotesException duplicateException) {
                        //by pass exception
                        duplicateException=null;
                    }
                }
                //push the handle to the next entry in the collection
                viewEntry=viewEntries.getNextEntry(viewEntry);
            }
           
        } catch (NotesException e) {
            e.printStackTrace();
        }
        //return the collection of documents thus obtained
        return viewDocs;
    }

No comments:

Post a Comment