Friday, 5 February 2016

Using dojo.aspect() programatically

dojo.aspect -It  is used to call any DOM Events or user defined functions before/after trigger of actual DOM Event or functions.


This session i will explain the usage of aspect using functions.
aspect has three methods but most of the time we use only two methods those are after() and before().

aspect.before()-To trigger any DOM Event /function before actual target Event/function loading,
aspect.after()-To trigger any DOM Event /function after actual target Event/function loading.

Syntax:
aspect.before(targetNode,"ActualDOMEvent",before/after DOM Events);

Example Code:

<html >
    <head>
     <title>Dojo Aspect Example</title>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dijit/themes/claro/claro.css" />
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.9.2/dojo/dojo.js"></script>
    </head>
    <body class="claro">
      
<script>

require(['dojo/aspect'], function(aspect){

function SuperMethod(){
alert("Super Method called");
console.log("super Method called");
}
function subMethod(){
alert("Sub Method is called");
console.log("sub method is called");
}
function helper(){}
helper.SuperMethod=SuperMethod;
helper.subMethod=subMethod;

//i want to call superMethod first but when i call super method i want to execute my sub method ,so use aspect
aspect.before(helper,"SuperMethod",subMethod);
helper.SuperMethod();
//now after calling the sub method i want to call super method
aspect.after(helper,"subMethod",SuperMethod);
helper.subMethod();



});
</script>
    </body>
</html>




Friday, 29 January 2016

Creating Custom Action in IBM Content Navigator 2.0.3

In this session am going to walk you through how to create a custom Action for ICN 2.0.3 version using IBM Plugin.


Use Case Scenario:

Usually any document in the filenet repository will be added by using AddDocument Action from the ToolBar Menu then you would add document by giving some title to the document,

in our scenario when document is added with the same name of any other document already resides in repository we need to give caution to user that document is already added in the repository.

We are going to develop this custom SearchBase AddDocument Action button using IBM plugin.

Starting Code:

I will take you through each and every step from the start point to endpoint of whole development process.

1:Creating Plugin Project

      --Open Up your Eclipse create new IBM Content navigator Plugin



--Give project name and package name,Navigator.jar location and click on next until it says finish.

--Right click on project create new Action Name it as searchBasedAddDocument click on finish.




Now lets start actual coding part.

2.After you create action  eclipse creates a  "searchBase AddDocument" Attach point in .js file .

3.To add any document we need a contentPane that allows a user to add document this can be achieved by calling AddContentItemDialog object ,this can be created as follows

define("Add Content Item Dialog",function(adddialog))
       { 
               var current=this;
               current=new adddialog(); 
       }


//here current is an object of AddContentItemDialog ,now lets open dialog (you can do this by calling current.show())

current.show(repository,items[0],true,false,null,teamspace,false,null);

4.Once you had dialog content pane ,you can get all the properties of document by using  current.addContentItemPropertiesPane.getPropertiesJSON() call this gives you all properties of document.

then get the document title and search against repository the result will be ecm.model.ResultSet object representing set of Objects Items.if we had any objectItem  from the result it means there is a document presented already in repository so we need to give an alert to user .

//see the whole code that implemented for the Action.


require(["dojo/_base/declare",
         "dojo/_base/lang",
         "ecm/widget/dialog/AddContentItemDialog",
         "ecm/model/SearchQuery",
         "ecm/widget/AddContentItemPropertiesPane",
         "ecm/model/Request",
         "ecm/widget/dialog/ConfirmationDialog"], 
function(declare, lang,adddialog,searchquery,contentItem,requestobj,ConfirmationDialog) {
/**
* Use this function to add any global JavaScript methods your plug-in requires.
*/
lang.setObject("searchBase AddDocument", function(repository, items, callback, teamspace, resultSet, parameterMap) {
 /*
  * Add custom code for your action here. For example, your action might launch a dialog or call a plug-in service.
  */


var documentname;
var current=this;
current=new adddialog();
var content=new contentItem();

current.show(repository,items[0],true,false,null,teamspace,false,null);

current.onAdd=function(){
//get the item id


//get item document title
content.setAddContentItemDialog(current);
var properties = current.addContentItemPropertiesPane.getPropertiesJSON();
alert(properties);
for (var key in properties) {
 if (properties.hasOwnProperty(key)) {
   var val = properties[key];
   if(val.name==="DocumentTitle"){
    documentname=val.value;
   }
   console.log("Object name is"+val.name+"-- Its value is "+val.value);
   //console.log(val);
 }
}


if(documentname!=null && documentname!==""){

var selectedClass =current.addContentItemPropertiesPane._contentClass.id;


//create a search using this template
//var ceQuery="SELECT This, DocumentTitle FROM Document WHERE DocumentTitle="+"'"+documentname+"'";
var ceQuery="SELECT This, ClassDescription, DocumentTitle FROM "+selectedClass+" WHERE DocumentTitle="+"'"+documentname+"'";
console.log("sql query is "+ceQuery);
var searchQuery = new searchquery({"pageSize":0,"query":ceQuery,"retrieveAllVersions":false,"retrieveLatestVersion":false,"repository": repository});

searchQuery.search(function(resultSet){
            
                 if(resultSet.totalCount>0){
confirm=new ConfirmationDialog(
{text: "A Document is already added in repositoty.",
                         buttonLabel: "OK",
                         title: "Confirm",
                         cancelButtonDefault: true,
                         onExecute: function() {
                        callMethod();
                         },
                         onCancel: function() {
                        cancelMethod();
                         }
                     });
confirm.show();
}
                 else
                {
               
                callMethod();
                }

             },null,null,null,function(error){
                
                 console.debug("Search error::",error);                
             });

}
};
function cancelMethod(){

current.destroy();
}

function callMethod(){

current._multiAddCleanup();
current._titleProperty = null;
var addAsMinorVersion = !current.addContentItemGeneralPane.getAsMajorVersion();
var autoClassify = current.addContentItemGeneralPane.getAutoClassify();
var compoundDocument = current._entryTemplate && current._entryTemplate.enableCompoundDocuments && current._entryTemplate.enableCompoundDocuments.on ? true : false;
var documentType = current.addContentItemPropertiesPane.getDocumentType();
var properties = current.addContentItemPropertiesPane.getPropertiesJSON();
var childComponentValues = current.addContentItemPropertiesPane.getChildComponentValues();
var permissions = current.addContentItemSecurityPane.getPermissions();
var allowDuplicateFileNames = true;
current._multiAddProcessing = current.addContentItemGeneralPane.hasMultipleDocuments();
var teamspaceId;
if (current._entryTemplate) {
allowDuplicateFileNames = current._entryTemplate.allowDuplicateFileNames;
}
if (current._teamspace && current._useTeamspaceACL) {
// Pass the teamspace id so the ACL of the teamspace can be applied.
teamspaceId = current._teamspace.id;
}
if (current._typeDocument) {
var setSecurityParent = (current._teamspace && current.repository._isP8());
if (!setSecurityParent && current._entryTemplate && current._entryTemplate.inheritSecurityFromParentFolder) {
setSecurityParent = true;
}
var contentSourceType = current.addContentItemGeneralPane.getContentSourceType();
var inputFiles = current.addContentItemGeneralPane.getFileInputFiles();
// If there are input files... (HTML5 browser).
if (inputFiles) {
current._documentItems = [];
if ((contentSourceType == "Document") && (inputFiles.length > 0)) {
if (current._multiAddProcessing) {
// Start the batch status dialog as modal until the first add succeeds.
current._createBatchStatusDialog(false);

// When adding multiple documents, use the file name for the title - except if current is a CM repository 
// and the title property is not "ICM$NAME".
current._multiAddAssignFileNameProperty = null;
var titlePropertyName = current.addContentItemPropertiesPane.getTitlePropertyName();
if (titlePropertyName && (!current.repository._isCM() || (titlePropertyName == "ICM$NAME"))) {
array.some(properties, lang.hitch(current, function(property) {
if (property.name == titlePropertyName) {
current._multiAddAssignFileNameProperty = property;
return false;
}
}));
}
current._multiAddFileCount = 0;
current._multiAddNumFiles = inputFiles.length;
}

for (var i = 0; i < inputFiles.length; i++) {
var file = inputFiles[i];
var documentItem = {
"templateName": documentType,
"teamspaceId": teamspaceId,
"contentSourceType": contentSourceType,
"mimetype": file.type,
"filename": (file.fileName != null) ? file.fileName : file.name,
filePath: (file.filePath != null) ? file.filePath : null,
"securityPolicyId": current._getSecurityPolicyId(),
"addAsMinorVersion": addAsMinorVersion,
"autoClassify": autoClassify,
"compoundDocument": compoundDocument,
"allowDuplicateFileNames": allowDuplicateFileNames,
"setSecurityParent": setSecurityParent
};
// IE10 and IE11 will hang when uploading a zero size file unless an IFRAME is used.
// At current time, IE10 and IE11 will still hang if multiple documents are added and one is a zero-size file.
if (has("trident") && file.size == 0) {
// Retrieve references to the form fields and set the data.
var propertiesInput = current.addContentItemGeneralPane._getPropertiesInput();
propertiesInput.value = dojojson.toJson(properties);
var childComponentInput = current.addContentItemGeneralPane._getChildComponentInput();
childComponentInput.value = dojojson.toJson(childComponentValues);
var permissionsInput = current.addContentItemGeneralPane._getPermissionsInput();
permissionsInput.value = dojojson.toJson(permissions);

lang.mixin(documentItem, {
"inputForm": current.addContentItemGeneralPane.getFileInputForm()
});
} else {
lang.mixin(documentItem, {
"criterias": properties,
"content": file,
"childComponentValues": childComponentValues,
"permissions": permissions
});
}

current._documentItems.push(documentItem);
}
} else { // Adding without file content.
var fileName = (contentSourceType == "ExternalURL") ? current.addContentItemGeneralPane.getExternalURL() : null;
current._documentItems.push({
"templateName": documentType,
"teamspaceId": teamspaceId,
"criterias": properties,
"contentSourceType": contentSourceType,
"mimetype": null,
"filename": fileName,
"content": null,
"childComponentValues": childComponentValues,
"permissions": permissions,
"securityPolicyId": current._getSecurityPolicyId(),
"addAsMinorVersion": addAsMinorVersion,
"autoClassify": autoClassify,
"compoundDocument": compoundDocument,
"allowDuplicateFileNames": allowDuplicateFileNames,
"setSecurityParent": setSecurityParent
});
}
} else { // current browser does not support HTML5.
// Retrieve references to the form fields and set the data.
var propertiesInput = current.addContentItemGeneralPane._getPropertiesInput();
propertiesInput.value = dojojson.toJson(properties);
var childComponentInput = current.addContentItemGeneralPane._getChildComponentInput();
childComponentInput.value = dojojson.toJson(childComponentValues);
var permissionsInput = current.addContentItemGeneralPane._getPermissionsInput();
permissionsInput.value = dojojson.toJson(permissions);

var fileName;
if (contentSourceType == "Document") {
fileName = current.addContentItemGeneralPane.getInputFileName();
} else if (contentSourceType == "ExternalURL") {
fileName = current.addContentItemGeneralPane.getExternalURL();
} else {
fileName = null;
}

current._documentItems = [
{
"templateName": documentType,
"teamspaceId": teamspaceId,
"contentSourceType": contentSourceType,
"filename": fileName,
"inputForm": current.addContentItemGeneralPane.getFileInputForm(),
"securityPolicyId": current._getSecurityPolicyId(),
"addAsMinorVersion": addAsMinorVersion,
"autoClassify": autoClassify,
"compoundDocument": compoundDocument,
"allowDuplicateFileNames": allowDuplicateFileNames,
"setSecurityParent": setSecurityParent
}
];
}
current._addDocumentItemMultiple();
} else {
// Ignore additional attempts to add current folder if it is already being added.
if (current._addingFolder) {
return;
}
current._addingFolder = true;

var folderName;
var folderProperties = {};
folderName = current.addContentItemPropertiesPane.getItemName();
folderProperties.folderName = folderName;
folderProperties.docid = current.parentFolder ? current.parentFolder.id : "";
folderProperties.criterias = properties;
folderProperties.childComponentValues = childComponentValues;
folderProperties.documentType = documentType;

if (current._virtualItems) {
if (current._isValidFolderName(folderName)) {
current.addFolderInMemory(folderProperties);
}
delete current._addingFolder;
return;
}

var request;

var onAddFolderRequestCompletedHandler = aspect.after(ecm.model.desktop, "onRequestCompleted", lang.hitch(current, function(completedRequest) {
if (!request || !request.isSameRequestAs(completedRequest)) {
return;
}
delete current._addingFolder;
onAddFolderRequestCompletedHandler.remove();
}), true);

var parentFolder = current.parentFolder;
if (current.repository._isP8()) {
var objectStore = (parentFolder && parentFolder.objectStore);

if (!objectStore && current._objectStore) {
objectStore = current._objectStore;
}
request = current.repository.addFolderItem(parentFolder, objectStore, documentType, folderProperties, [], permissions, current._getSecurityPolicyId(), teamspaceId, lang.hitch(current, current._addFolderItemCallback));
} else {
request = current.repository.addFolderItem(parentFolder, null, documentType, properties, childComponentValues, permissions, null, teamspaceId, lang.hitch(current, current._addFolderItemCallback));
}

if (!request) {
if (onAddFolderRequestCompletedHandler) {
onAddFolderRequestCompletedHandler.remove();
}
delete current._addingFolder;
}
}
}

});
});



5.create a jar file using Ant or any other tool by which you are familiar.

6.Now add this plugin to navigator As shown in below

       6.1.Open your navigator desktop in Administrator mode



    6.2.open Plugins from context menu


    6.3.create a new plugin and give the jar file path thats it.you can able to see your custom action in Context Menus if you add these Action in Menus. (To test this Action i was added this action to Folder Context Menu)




Run ScreenShots:


1.Opening Folder Context Action


2.Am adding a document with title which is already presented in the repository.(You might expect a alert ).




Thank you very much if you like please share this link .

If you want to download entire source code please click on below add link that will automatically redirects you to download page.






































Wednesday, 27 January 2016

Creating user defined classes(Modules) in DOJO

This session will describe how to create  a user defined class.

Dojo provides a module to define user specific class ,the module name is "declare" .

now lets create a sample class using dojo.

define(["dojo/_base/declare"],function(declare){
return declare("UserDefinedClass",[],{

//define a name to be print
className:"",
constructor:function(){
console.log("Constructor is called !! ");
},
setClassName: function(className){
this.className=className;
},
getClassName:function(){
console.log("ClassName is "+this.className);
}

});

});

save file as UserDefinedClass.js ,means we created our own module with a name UserDefinedClass ,this class is available in your entire package from now and  we can include this module as like other predefined module provided by dojo for us.

using UserDefinedClass Module.

<html>
<head>
<!--Load Dojo Configurations-->
<script>
var djConfig={
isDebug:true,
parseOnLoad:true
};
</script>
<!--Load main dojo script-->
<script
type="text/javascript"
src="./dojo/dojo.js"
>
</script>
</head>
<body>
<span>This Code Example shows how to create a user defined classes using dojo.</span>
<!--Load Dojo Script-->
<script>
require(["UserDefinedClass","dojo/parser"],function(userdefined,parser){
parser.parse();//parses all widgets if there are any widgets
var object=new userdefined();
//set name of class form here
object.setClassName("sample class");
//log the name of class by calling getClassName method
object.getClassName();
});
</script>
</body>
</html>


In the above code we used our custom module using require() module provide by dojo for us.


OutPut:

Loading Dojo Example

Dojo is one of the fast growing Javascript technology,it is very easy to use and provides a feel for a programmer as we working with an object oriented programming.

In this tutorial we walk through how to load basic dojo script into your file(HTML).
Before we start programming with DOJO we need to get latest dojo modules ,you can get those modules form dojo official website or click here..

here is the HTML code :

<html>
<head>
<!--Load Dojo Configuration here-->
<script>
//before loading main module try to load initial configurations
var djConfig={
isDebug:true,
parseOnLoad:true
};
</script>

<!--Load main dojo script Assuming it has relative path to the main project-->

<script
type="text/javascript"
src="./dojo/dojo.js"

></script>

</head>

<body>

<span>Dojo Load Testing</span>
<!--Load scrpit-->
<script>

dojo.addOnLoad(function(){//calling script immediately once page is loaded takes an anonymous function.
alert("Hey,It's Me DOJO Loaded For You..");
});

</script>
</body>

</html>


Explanation:

First and foremost thing is we need to load dojo configurations that can be loaded by using djConfig.
djConfig is an object that have initial loader configuration parameters to be set.in our code we specified two configuration parameters 1.isDebug 2.parseOnLoad
isDebug parameter tells the browser we are going to debug the code while running it through firebug(firebug is a standard tool referred by DOJO)
parseOnLoad parameter tells the page will parse automatically for checking any widgets that are needed to instantiate.
once page is loaded i am calling method addOnLoad() that do all work for us.




OutPut: