Wednesday, November 30, 2016

REST Example

Continuation of the the Previous Post.

REST data transfer will mostly will happen through JSON.
We can get the JSON as JSON object directly and parse it in the connector itself or we can get that JSON as string and map to string property, then in activity we can parse the JSON object to any desired class.

In the implementation which we implemented last time will return the response as String.
we can map that string to clipboard Property as below:

After running the data page “D_LocationsV1Adminareas”, it will give the result as below.
Data page will have source of REST Connector.

"For REST we will get the JSON string, In Connector directly we can set to Clipboard property or we can assign as JSON in response tab so that it will form the clipboard structure once it receives."

"If JSON is starting as Array without the starting element, then we cannot use Directly set as JSON. we need to set to clipboard property and parse using java function and use it.
If Starting is having single page, then directly we can use JSON in the Response Tab."

Connector Rule which is created is:


in this case JSON string we are getting and we are mapping to Clipboard and
from that JSON we need to parse to get the desired result.


After executing the data page, 


After this u can parse this JSON string to create a page list property.
And it will form a structure below. And can be used for any purpose.

how to convert JSON String to Clipboard structure:

Step 1: call the data page in step 1 with the corresponding parameters.
and get the Json string in Local Variable.
D_LocationsV1Adminareas[CountryCode:"IN",apikey:"QzV2D2TGt9AyQHvzKRqbMXXXXX"]

Step 2: Using java need to parse the JSON to XML structure.
try
{
  myStepPage.adoptJSONObject(GetResponse);
  //myStepPage.adoptJSONObject(GetResponse, "GetStateList");
  
}catch(InvalidStreamError e)
  {
  oLog.infoForced("Got Error While Parsing");
}
so that it will be easy for Other Process in Pega.

Where Reponse Output is mapped to JSON.



sample data of JSON which we will get as below:
"{"success":true,"terms":"https:\/\/currencylayer.com\/terms","privacy":"https:\/\/currencylayer.com\/privacy","historical":true,"date":"2016-10-01","timestamp":1475366399,"source":"USD","quotes":{"USDAED":3.672904,"USDAFN":65.550003,"USDALL":122.239998}} 
"

Tuesday, November 29, 2016

About Rest Wizard

For Learning REST Wizard:

REST is light weight connector. Data transfers for REST happens through HTTP protocol. And Authentication also uses same as HTTP authentication.

From Designer Studio àIntegrationàConnectorsàConnect REST integration

End Point URL used in this demo is 
http://dataservice.accuweather.com/locations

Rules will get created thru this wizard are as below:
Int-Classes, and REST rule will get created in Int-Class
And Data Classes and Data transform to set the values which we got from Int Class.
and DataPage will get created.













Thursday, November 17, 2016

Javascripts used in Pega - I

Some Javascripts are given below which are used in pega:

Javascripts use to open in new window:
<script>
function ExportExcel(){
var windowProps = 'toolbar=no,location=no,menubar=no,status=yes,scrollbars=yes,resizable=yes,width=750,height=600';
var URL = new SafeURL("ClassName.ActivityName"); //Replace ClassName with name of the Applies To class and ActivityName with name of your export activity
url = URL.toURL();
window.open(url,"_self", windowProps);
}
</script>


To Call Activity from Java Script:
For calling activity from Javascript, there is already a thread ..You have to use 'SafeUrl' and httpRequestAsync function in script.

<script>
function openActivity()
{
var oSafeURL = new SafeURL("Ecorp-Report.TestActivity");
openUrlInWindow(oSafeURL, 'popup', 'width=850,height=350,left=100,scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no');
}
</script>

(or)

<script>
function openActivity()
{
var oSafeURL = new SafeURL("Link-Attachment.Test");
openUrlInWindow(oSafeURL.toURL(), 'popup', 'width=850,height=350,left=100,scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no');
}
</script>

(or)

To call an activity using java script : 

1) var bSafeURL= new SafeURL("A-B-C.ActivityName");
    bSafeURL.put("param1",value1);
    bSafeURL.put("param1",value2);
    pega.util.Connect.asyncRequest('GET', bSafeURL.toURL(),'');

2) var oSafeURL = new SafeURL("@baseclass.testActivity");
oSafeURL.put("ClassName", insName);
var strRuleXml = httpRequestAsynch( oSafeURL.toURL(), null, 50, 100);

apart from there there lot other ways to do it. 

Some ways that we can achive a  call to activity and open popup window / model window:

1) 
var pxReqURI = pxRequestor.pxReqURI;
openUrlInWindow(pxReqURI + "?pyActivity=A-B-C.ActivityName&Param1=" + value1, "InteractionData", "status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,left=192,top=51.2,height=500,width=800");

2)
activityURL = "<pega:reference name="pxRequestor.pxReqURI" mode="normal"/>?pyActivity=Work-myActivity&myParameterName=" + valueToPass;
windowOptions = "width:500;height:600;resizable: yes; scroll: yes; help: no";
openUrlInWindow(activityURL,"WINDOWNAME",windowOptions,false);

3) var strWindowName = "Popup window title";

 var objSafeUrl = new SafeURL("A-B-C.Activity Name");
 var contentWin = showModalDialog(objSafeUrl.toURL(), strWindowName , "width:500;height:600;resizable: yes; scroll: yes; help: no;");

4)   var objSafeUrl = new SafeURL("A-B-C.Activity Name");
windowOptions = "width:500;height:600;resizable: yes; scroll: yes; help: no";
openUrlInWindow(objSafeUrl .toURL(),"WINDOWNAME",windowOptions,false);


Sunday, November 13, 2016

Send Email with Attachments

Sample Code for Sending Email with Attachments:

Screen shot in sample activity implementation:


Sample java code for reading the Attachment:
ClipboardPage page_Attachment = null,
              page_EmbedAttachment = null,
              page_AttachmentPage = tools.findPage("AttachmentPage");
ClipboardProperty prop_pyAttachment = null;
      prop_pyAttachment = page_AttachmentPage.getProperty(".pyAttachments(<append>)");
      page_EmbedAttachment = prop_pyAttachment.getPageValue();

java.io.DataInputStream dis = null;

String strFileName = "C://EmployeeDetails1.xls";
String strTheFileName="";
java.util.HashMap hmFileInfo = new java.util.HashMap();
java.io.File objFile = new java.io.File(strFileName);
byte buffer [] = null;
java.lang.StringBuilder strFileData = null;

try {


String sCurrentLine;

dis = new java.io.DataInputStream(new java.io.FileInputStream(objFile));
            int bufferSize = 2048001;
            int fileSize = dis.available();
System.out.println("fileSize"+fileSize);
            strFileData = new StringBuilder((int) (fileSize * 1.4));
            int noOfIterations = fileSize / bufferSize;
            for (int j = 0; j < noOfIterations; j++) {
                            buffer= new byte[bufferSize];                                                  
                            dis.read(buffer, 0, bufferSize);
                            //bytes.toString()
                            System.out.println("Buffer Size "+buffer);
                            strFileData.append(Base64Util.encodeToString(buffer)); //to encode to Base64
               //strFileData.append(buffer);
            }
            int remaining = fileSize % bufferSize;
            buffer = new byte[remaining];                                                  
            dis.read(buffer, 0, remaining);
            strFileData.append(Base64Util.encodeToString(buffer));
          //strFileData.append(buffer);
          //strFileData.append(Base64Util.encodeToString(strFileData));
            dis.close();

if (strFileData == null)

{
            //throw new PRRuntimeException("Can't continue with file upload. Couldn't encode the file to Base64 so that we can store it on the database");                                           
}
 strTheFileName = objFile.getName();
String strFileExtension = "";
int nIdx = strTheFileName.lastIndexOf(".");
if (nIdx > -1){
            strFileExtension = strTheFileName.substring(nIdx + 1);
}
String decodedFileName = StringUtils.decodeCrossOSString(strTheFileName);

System.out.println("Extension "+strFileExtension+","+strTheFileName+",file date"+strFileData);


page_EmbedAttachment.putString(".pyReference", "");

page_EmbedAttachment.putString(".pyData", strFileData.toString());
page_EmbedAttachment.putString(".pyName",decodedFileName);
page_EmbedAttachment.putString(".pyDecode","true"); 
          
          oLog.infoForced("string"+strFileData.toString());

System.out.println(strFileData);

} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null)dis.close();
} catch (java.io.IOException ex) {
ex.printStackTrace();
}
}

Saturday, November 5, 2016

To Get Absolute Path of Pega Temp Directory

To get the Absolute path in Pega of the Temp Directory:

Whatever the file we uploaded, initially it will be stored in temp directory.
And then it will be saved to database.

to get the Absolute path of the Temp Directory, we can use below code:

String   explicitTmpDir  = tools.getSystemSettings().getFSSetting("initialization/explicittempdir", "" , true, false);

In Windows - sample structure will be like C:\Pega72\PRPCPersonalEdition\temp

In Linux - Sample Structure will be like /apps/pbm/pega712/weblogic/prcache

please find the below URL to get from the PDN Link
https://pdn.pega.com/community/pega-product-support/question/finding-absolute-file-path

Activity Name where u can find sys details: SysWebInfo