Wednesday, 2 April 2014

Export and Import Favorites

Index :
1. Export
2. Import
3. Copy

1. Export :

 void clicked()
{
    #AOT
    #TreeNodeSysNodeType
    #Properties
    #AviFiles
    CommaIo     fileOut;
    FileName    fileName;
    TreeNode menuTypeNode,parentnode,groupnode;
    str errmsg="";
    Dialog      dialog;
    DialogField fieldfilename;

    // Function which parses the treenode to get all its child nodes..
    void searchchildnode(TreeNode rootnode)
    {
        TreeNode childnode;
        TreeNodeIterator rootiterator = null;
        str object='Group',objecttype='Group',Label;
        String255 nodeType = 'Group';
        Label = groupnode.treeNodeName();
        if(rootnode)
        {
            //To send Group information to file
            fileOut.write(groupnode.treeNodeName(),rootnode.treeNodePath(),nodeType,Label,objecttype,object,rootnode.treeNodePath());
            rootiterator = rootnode.AOTiterator();
            childnode = rootiterator.next();
            while(childnode)
            {
                // Checks whether treenode is menuitem or folder
                //IF Menu Item then assign values required for creating as Favourite
                if (childnode.treeNodeType().id() == #NT_MENUITEM)
                {
                    nodeType = 'MenuItem';
                    objecttype = findProperty(childnode.AOTgetProperties(),#PropertyMenuItemType);
                    object = findProperty(childnode.AOTgetProperties(),#PropertyMenuItemName);
                    label = findProperty(childnode.AOTgetProperties(),#PropertyMenuItemLabel);
                    if(objecttype == 'Display')
                    {
                        menuTypeNode = TreeNode::findNode(#MenuItemsPath +'\\Display\\' + object);
                        Label = findProperty(menuTypeNode.AOTgetProperties(), #PropertyLabel);
                    }
                    else if(objecttype == 'Action')
                    {
                        menuTypeNode = TreeNode::findNode(#MenuItemsPath +'\\Action\\' + object);
                        Label = findProperty(menuTypeNode.AOTgetProperties(), #PropertyLabel);
                    }
                    else if(objecttype == 'Output')
                    {
                        menuTypeNode = TreeNode::findNode(#MenuItemsPath +'\\Output\\' + object);
                        Label = findProperty(menuTypeNode.AOTgetProperties(), #PropertyLabel);
                    }
                }
                //IF folder then assign values required for folder
                else
                {
                    nodeType = 'Folder';
                    Label = childnode.treeNodeName();
                    objecttype = 'Folder';
                    Object = 'Folder';
                }
                parentnode = childnode.AOTparent();
                // To pass the Group Name, Node path,Type of Node - folder/menuitem, Object Label, Object Type-display/output/Action, Object assigned for menu
                fileOut.write(groupnode.treeNodeName(),parentnode.treeNodePath(),nodeType,Label,objecttype,object,childnode.treeNodePath());
                // If have child nodes then perform Recursive action till End
                if(childnode.AOTchildNodeCount()>0)
                searchchildnode(childnode);
                childnode = rootiterator.next();
            }
        }
    }
    //Function to Parse through all the Favourite Groups.
    void getUserMenus()
    {
        TreeNode treeNode;
        TreeNodeIterator iterator;
        ;
        treeNode = infolog.userNode();
        iterator = treeNode.AOTiterator();
        treeNode = iterator.next();
        if (treeNode)
        {
            iterator = treeNode.AOTiterator();
            treeNode = iterator.next();
            // loop which parses all the Groups of favourites
            while (treeNode)
            {
                groupnode = treeNode;
                searchchildnode(treenode);
                treeNode = treeNode.AOTnextSibling();
            }
        }
    }
    #File
    dialog = new Dialog("Export");
    dialog.addText("Select your favorite customer:");
    fieldfilename= dialog.addField(extendedTypeStr(FilenameSave));
    dialog.filenameLookupFilter(["CSV","*.csv"]);
    dialog.filenameLookupTitle("Create Only CSV");
    dialog.run();
    if (dialog.closedOk())
    {
    fileName=fieldfilename.value();
    }
    fileOut = new CommaIo(filename, #io_write);
    if(fileOut)
    {
    getUserMenus(); // To parse all Favourite Groups
    }
    info("Favorites are imported to "+fileName);
}


2. Import :

void clicked()
{
    #File
    #TreeNodeSysNodeType
    #Properties
    #AOT
    #AviFiles
    IO  iO;
    TreeNode treenode,groupnode,checknode,menuTypeNode;
    MenuFunction mf;
    TreeNodeIterator iterator;
    FilenameOpen        filename;
    Container           record;
    boolean first = true;
    Menu menuNode;
    TreeNodePath path,objectpath;
    MenuItemType menutype;
    str object="",label="",objecttype="",nodeType,groupName,objectform;
    UserMenuList userMenu;
    boolean nodeexists;
    Dialog      dialog;
    DialogField fieldfilename;
    // Function to get the treenode in with Specified path.
        TreeNode searchchildnode(TreeNode rootnode,TreeNodeIterator tmpiterator,TreeNodePath xpath)
        {
            TreeNode childnode;
            TreeNodeIterator rootiterator = tmpiterator;
            TreeNodePath ypath;
            if(rootnode)
            {
                rootiterator = rootnode.AOTiterator();
                childnode = rootiterator.next();
                while(childnode)
                {
                    ypath = childnode.treeNodePath();
                    if(xpath == ypath)
                    {
                        treenode = childnode;
                        break;
                    }
                    if(childnode.AOTchildNodeCount()>0)
                        searchchildnode(childnode,rootiterator,xpath);
                    childnode = rootiterator.next();
                }
            }
            if(treenode == rootnode)
                return rootnode; // Sent Group node
            else
                return treenode; // send Exact path of where to add node.
        }
    ;
    // Open the dialog to select the file
    dialog = new Dialog("Import");
    dialog.addText("Select your favorite customer:");
    fieldfilename= dialog.addField(extendedTypeStr(FilenameOpen));
    dialog.filenameLookupFilter(["CSV","*.csv"]);
    dialog.filenameLookupTitle("Upload only CSV");
    dialog.run();
    if (dialog.closedOk())
    {
        fileName=fieldfilename.value();
    }
    iO = new CommaTextIo(filename,#IO_Read);
    if (! iO || iO.status() != IO_Status::Ok)
    {
        throw error("File Cannot be Opened");
    }
    //Loops Until File End
    while (iO.status() == IO_Status::Ok)
    {
        record = iO.read();
        if (record)
        {
            nodeexists = true;
            groupName = conpeek(record, 1);
            path = conpeek(record, 2);
            nodeType  = conPeek (record,3);
            label = conpeek(record, 4);
            objecttype = conpeek(record, 5);
            object  = conPeek (record,6);
            objectpath =conPeek(record,7);
            treeNode = infolog.userNode();
            iterator = treeNode.AOTiterator();
            treeNode = iterator.next();
            groupnode = treeNode.AOTfindChild(groupName);
            //If specified Group is not created, it creates the Group.
            if(!groupnode)
            {
                userMenu = treeNode;
                userMenu.createMenu(groupName);
                treeNode = userMenu.AOTfindChild(groupName);
                menuNode = treeNode;
                menuNode.AOTsave();
            }
            if(nodeType != 'Group')
            {
            nodeexists = true;
            if(nodeType == 'MenuItem')
            {
                if(objecttype == 'Display')
                {
                    menuTypeNode = TreeNode::findNode(#MenuItemsPath +'\\Display\\' + object);
                    label = findProperty(menuTypeNode.AOTgetProperties(), #PropertyLabel);
                }
                else if(objecttype == 'Action')
                {
                    menuTypeNode = TreeNode::findNode(#MenuItemsPath +'\\Action\\' + object);
                    label = findProperty(menuTypeNode.AOTgetProperties(), #PropertyLabel);
                }
                else if(objecttype == 'Output')
                {
                    menuTypeNode = TreeNode::findNode(#MenuItemsPath +'\\Output\\' + object);
                    label = findProperty(menuTypeNode.AOTgetProperties(), #PropertyLabel);
                }
                if(!menuTypeNode)
                {
                nodeexists = false;  // If menu item doesn't exist in imported environment
                }
            }
            if(nodeexists)
                treeNode = searchchildnode(treeNode,iterator,path); // Get Node in specified path
            if(treeNode)
                checknode = treenode.AOTfirstChild();
            // Check whether the object is already existed in particular path
            while(checknode)
            {
                if(checknode.treeNodePath() == objectpath)
                {
                    nodeexists = false;
                    break;
                }
                checknode = checknode.AOTnextSibling();
            }
            // Condition to check If we get path and object do not exists then It creates new one.
            if (treeNode && nodeexists)
            {
                // To create folder to the Favourite...
                if(nodeType == 'Folder')
                {
                menuNode = treenode;
                menuNode.addSubmenu(label);
                menuNode.aotsave();
                //menuNode.save();
                }
                // To create Menu item
                if(nodeType == 'MenuItem')
                {
                    if(objecttype == 'Display')
                    {
                        menutype = MenuItemType::Display;
                        objectform = 'Form';
                    }
                    else if(objecttype == 'Action')
                    {
                        menutype = MenuItemType::Action;
                        objectform = 'Class';
                    }
                    else if(objecttype == 'Output')
                    {
                        menutype = MenuItemType::Output;
                        objectform = 'SSRSReport';
                    }
                    menuNode = treeNode;
                    mf = new MenuFunction(object,menutype);
                    mf.AOTsetProperty('Label',label);
                    mf.AOTsetProperty('ObjectType',objectform);
                    mf.AOTsetProperty('Object',object);
                    mf.AOTsave();
                    menuNode.addMenuitem(mf);
                    menuNode.AOTsave();
                    //menuNode.save();
                    }
                }
            }
        }
    }
info("Favourites Imported from" +fileName);
}

3. Copy Favorites Between Users :

 

No comments:

Post a Comment