String & Math functions in Java

Math functions:

java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

Math.min(num1, num2):

float minVal = Math.min(float a, float b);   
// data types allowed: int, long, float, double.

Math.max(num1, num2):

float maxVal = Math.max(float a, float b);  
// data types allowed: int, long, float, double.

Math.random(): This method returns a pseudo-random number of double data type greater than or equal to 0.0 and less than 1.0.

double randomVal = Math.random();  
// data type returns is double (o/p: 0.15726642068533314)

Math.sqrt(num1): This method returns the square root of a number.

double sqrtVal1 = Math.sqrt(25); // data type returns is double.
double sqrtVal2 = Math.sqrt(10); 
(o/p: 5.0)
(o/p: 3.1622776601683795)

Math.cbrt(num1): This method returns the square root of a number.

double sqrtVal1 = Math.cbrt(27); // data type returns is double.
double sqrtVal2 = Math.cbrt(125); 
(o/p: 3.0)
(o/p: 5.0)

Math.round(num1):

float roundVal = Math.round(float a); 
// data types allowed: float, double

Math.floor(num1): This method returns the floor value of given number.

double sqrtVal1 = Math.floor(27.89034); // data type returns is double.
(o/p: 27.0)

Math.ceil(num1): This method returns the ceiling value of given number.

double sqrtVal1 = Math.ceil(27.89034); // data type returns is double.
(o/p: 28.0)

String functions/Methods:

java.lang.String class provides 40+ methods to work with the strings.

A sample java class is also given at the bottom, for a better understanding of string functions.

equals(Str), equalsIgnoreCase(): To compare two strings. Returns true/false.

System.out.println("ravi".equals("ravi"));   // true 
System.out.println("ravi".equals("raviteja")); // false

compareTo(), compareToIgnoreCase(): To compare two strings. Returns true/false.

System.out.println("ravi".compareTo("ravi")); // 0
System.out.println("ravi".compareTo(null)); // NullPointerException

Note: For comparing two strings use the equals() method. comapreTo() method can also be used, but prefer to use equals() method as compareTo() method throws NullPointerException when comparing string with another string with null value.

contentEquals(Str): To compare a string with String Buffer.

String str1 = "StringValue";
StringBuffer str2 = new StringBuffer( "StringBuffer");
System.out.println(str1.contentEquals(str2 ));   // false

contains(Str): To check if the string contains the given string value.

String str1 = "StringValue";
String str2 = "Val";
System.out.println(str1.contains(str2));   // true

length(): To find the length of a given string.

String str1 = "StringValue"; 
str1.length(); // o/p: 11  
[Note that an array length can be found using 'arr.length;']

trim(): It’ll trim the white spaces at leading and trailing of given string.

String str1 = "     StringValue  ";  
str1.trim();  // o/p: StringValue

valueOf():  Irrespective of the data type by using this method you can convert any data type variable to string variable.

String str1 = String.valueOf(anyDataType_value);  // o/p: string format

substring(int beginIndex), substring(int beginIndex, int endIndex):  Used to get the required substring from the given string using given beginIndex, endIndex values.  Here you have to notice one thing that when beginIndex and endIndex are given, the result will include the char at start Index value and excludes (won’t include) the char endIndex value of string.

String str1 = "StringValue"
str1.substring(3);  // o/p: ingValue
str1.substring(3,8); // o/p: ingVa

split(String regex), split(String regex, int limit):  Splits the string as per given regular expression. The result will be stored as an array. You can pass the limit value also, which will splits the given string into given limit values.

 String Str = new String("Welcome-to-Hyderabad");
 String[] splitVals = Str.split("-");
 System.out.println(":"+Arrays.toString(splitVals)); 
 o/p: [Welcome, to, Hyderabad]
 
 String Str = new String("Welcome-to-Hyderabad-City");
 String[] splitVals = Str.split("-",2);
 String[] splitVals = Str.split("-",4);
 System.out.println(":"+Arrays.toString(splitVals)); 
 o/p: [Welcome, to-Hyderabad-City]
 o/p: [Welcome, to, Hyderabad, City]  

matches(String regex):  returns true if the given string matches with the given regular expression.

 String Str = "Welcome to Hyderabad City";
 System.out.println(Str.matches("(.*)Hyderabad(.*)"));  // o/p: true
 System.out.println(Str.matches("(.*)Hyderabad"));  //o/p: false

concat(Str):  returns true if the given string matches with the given regular expression.

 String Str = "Welcome to";
 System.out.println(Str.concat("Hyderabad"));
 // o/p: Welcome to Hyderabad

chatAt(int index):  returns the character present in the given string at specified location index. The return type will character.

 String Str = "Welcome to";
 char result = Str.charAt(5);
 System.out.println(result);  // o/p: m
Note: If char contains int value, use below way to convert char to int.
String Str = "137654";
int num= Character.getNumericValue(Str.charAt(3));
System.out.println(num); // o/p: 6
Advertisements

Arrays In Java

You already know that Array is a container which stores multiple values and the length of array will be fixed during declaration itself.

In this post let’s discuss Arrays of single dimensional, multi dimensional, Array’s functions etc.

One dimensional Array:  

long type:   long[] lng_arr = new long[3];
String type: String[] str_arr = new String[3];

similarly we can have arrays of other data types also. [byte[] anArrayOfBytes; short[]  anArrayOfShorts;  long[] anArrayOfLongs; float[] anArrayOfFloats;  double[] anArrayOfDoubles;  boolean[] anArrayOfBooleans;
char[] anArrayOfChars; String[] anArrayOfStrings;

Below is how we insert values to Array.

lng_arr[0] = 123; lng_arr[1] = 342; lng_arr[2] = 23;
str_arr [0] = "This"; str_arr [1] = "is"; str_arr [2] = "java_post"; 

You can also insert values to Array like given below. [giving values at the time of declaration itself]

long[] lng_arr = {123, 342, 23};
String[] str_arr = {"This", "is", "java_post"};

Now let’s see how we can print entire array itself. [using Arrays.toString(arr) we can achieve this]. For multi dimensional arrays we should use  Arrays.deepToString(arr).

System.out.println("lng_arr :"+lng_arr);  o/p: [J@3d04a311
System.out.println("lng_arr :"+Arrays.toString(lng_arr)); o/p: [123, 342, 23]
System.out.println("str_arr :"+Arrays.toString(str_arr)); o/p: [This,  is,  java_post]

Now let’s print single required value from the array: (for printing single value from the array, using of Arrays.toString(arr) is not required, which is used to print entire array in string foramt.

System.out.println("lng_arr[0] :"+lng_arr[0]);  o/p: 123
System.out.println("str_arr[2]:"+str_arr[2]);  o/p: java_post

Multi dimensional Array:  

long type:   long[][] lng_arr = new long[3][2];  
// [[a1, b1], [a2, b2], [a3, b3]]
(3 * 2) matrix would be like given below.
[a1, b1]
[a2, b2]
[a3, b3]
where [0][0] = a1;  [0][1] = b1;  [1][0] = a2;  [1][1] = b2;  [2][0] = a3;  [2][1] = b3; 

String type: String[][] str_arr = new String[2][3];  
// [[a1, b1,c1], [a2, b2, c2]]

similarly we can have arrays of other data types also. [byte[][] anArrayOfBytes; short[][]  anArrayOfShorts;  long[][] anArrayOfLongs; float[][] anArrayOfFloats;  double[][] anArrayOfDoubles;  boolean[][] anArrayOfBooleans;
char[][] anArrayOfChars; String[][] anArrayOfStrings;

Below is how we insert values to Array.

lng_arr[0][0] = 123;   lng_arr[0][1] = 342;    lng_arr[1][0] = 23;  lng_arr[1][1] = 223;  lng_arr[2][0] = 13;     lng_arr[2][1] = 323; 

str_arr [0][0] = “Mr. “;  str_arr [0][1] = “Mrs. “;    str_arr [0][2] = “Ms. “;  str_arr [1][0] = “Swamy “;  str_arr [1][1] = “Kumar”;    str_arr [1][2] = “Sita”; 

You can also insert values to Array like given below. [giving values at the time of declaration itself]

long[][] lng_arr = {{123, 342}, {23, 223}, {13, 323}};
String[][] str_arr = {{"Mr. ", "Mrs. ","Ms. "},{"Swamy","Kumar","Sita"}};

Now let’s see how we can print entire array itself. [using Arrays.deepToString(arr) we can achieve this].

System.out.println("lng_arr :"+Arrays.deepToString(lng_arr ));  
o/p: [[123, 342], [23,223], [13,323]]

System.out.println("str_arr :"+Arrays.deepToString(str_arr )); 
o/p: [[Mr. , Mrs. , Ms], [Swamy, Kumar, Sita]]

Now let’s print single required value from the array: (for printing single value from the array, using of Arrays.toString(arr) is not required, which is used to print entire array in string foramt.

System.out.println("lng_arr[0][1]:"+lng_arr[0][1]);  o/p: 342
System.out.println("str_arr[1][2]:"+str_arr[1][2]);  o/p:  sita

Arrays functions:  

Sort:   Sorting of Array can be achieved using Arrays.sort() method.

Given your array like below

long[] lng_arr = {18, 13, 23, 16, 20};
Arrays.sort(lng_arr);  
System.out.println("lng_arr:"+  Arrays.toString(lng_arr));  
o/p:  [13, 16, 18, 20, 23]

Sort some portion of Array: 

Sorting of Array can be achieved using Arrays.sort() method.

Given your array like below

long[] lng_arr = {18, 13, 23, 16, 20, 34, 12, 7};
Arrays.sort(lng_arr, 2, 5);  
System.out.println("lng_arr:"+  Arrays.toString(lng_arr)); 
o/p:  [18, 13, 16, 20, 23, 34  ,12, 7]

Get a value from array at specified index:

long[] lng_arr1 = {18, 13, 23, 16, 20, 34, 12, 7};
System.out.println(lng_arr1[4]);  // o/p:  20

Note: For list you need to use the get function.
List<Long> lst = new List<>();  
lst.add(27); lst.add(34); lst.add(23);
lst.get(1); //o/p: 34

To Print Entire Array: use Arrays.toString for one dimensional array and use  Arrays.deepToString for multi dimensional arrays and arrays converted from list.

long[] lng_arr1 = {18, 13, 23, 16, 20};
long[][] lng_arr2 = {{123, 342}, {23, 223}, {13, 323}};
System.out.println(Arrays.toString(lng_arr1)); 
System.out.println(Arrays.deepToString(lng_arr2));  
o/p:  [18, 13, 23, 16, 20]
[[123, 342], [23,223], [13,323]]

length: use length to find the length of an array.

long[] lng_arr = {18, 13, 23, 16, 20};
System.out.println(lng_arr.length);   o/p:  5

Note: To find the length of a list use size function.
List<Long> lst = new List<>();  
lst.add(27); lst.add(34); lst.add(23); 
lst.size(); //o/p: 3

To find the length of a string use length() function.
String val = "ravi";
System.out.println(val.length()); //o/p: 4

Happy Coding !

 

 

While loop in SOA

In this post let’s see how we can use While loop in SOA BPEL.

In the example below, we pass input as a number (n) and the response would be sum of all numbers till n (0+1+…n).

  • Create SOA Application (SOA_Appln)
  • Create SOA Project (SOAproj_While)
  • Create BPEL (Synchronous BPEL 2.0)
  • Edit the XSD like below.

 

  • Create two scope variables at BPEL scope level (‘sum’, ‘i’) of type ‘xml schema simple type – int’.

  • Now add Assign activity below receive activity and double click on it. drag an expression and give the value 0 and map it to the scope variable ‘sum’ which we created before. similarly create another expression, give value as 0 and map it to scope variable ‘i’.

  • Now drag the while activity below the Assign1 activity.  Double click on it and give the condition as ($i < $inputVariable.payload/client:input) which means while this condition satisfies the loop will be executed.

  • Now we need to increment the value right, so drag the assign activity inside while loop and double click on it.  drag the expression builder and give the value as $i+1 and map it to scope variable ‘i’. similarly again drag expression builder and give value as $i+$sum and map it to scope variable ‘$sum’ which means we are summing the sum value with the current loop count. (If loop is 4 then sum would be sum+4. Till the loop3, sum will be 1+2+3 =>6. So at loop4 sum would be sum+4 => 6+4 => 10.

  • We are done with the while loop. After the loop exits, now we need to assign the final sum value which we stored in the scope variable ‘sum’ to the output variable. So create an assign activity after while loop and double click on it.

  • The final BPEL will look like below.

  • Deploy the application to SOA server.
  • Let’s test the project. Give input as ‘4’ and the output would be ’10’.

Happy Looping 🙂

 

AM Service Interface (Expose ADF BC as web services/ service Enabled data objects SDOs)

======================================================
PART – 1:
AppModule Service Interface webservice genearation [Expose ADF BC as SDOs service enabled daat objects]:
-> create ADF application with business components (EO, VO, AM)
-> write two custom methods in AppModuleImpl class and expose those methods to Client Interface.
-> double click AM -> Service Interface tab -> you can see three sections ‘service Interface for custom methods’, ‘service interface for view instances’, ‘Genearated files for Service interface’.
-> select required methods/view Instances of datamodel in respective sections. When you select a view instance, operations for that view isntance are displayed jsut beside to it and we can select what operations to enable for the view instance [oeprations as ‘update’ to edit the record of that view instance, ‘Find’ to retrieve the records from that view instance, Create, Delete, Merge, GetByKey, Process, etc..].
-> After selection, under AM a folder named ‘service interface’ will be created which contains webservice artifacts [AppModuleService.java, AppModuleServiceImpl.java, AppModuleService.wsdl, AppModuleService.xsd]
-> Deployment: create deployment profile for ‘[Business Components Service Interface]’. deploy [check Model projetc is checked and under which both ‘common’, ‘middletier’ selected for integrated weblogic, and only ‘middletier’ selected for deployment on servers]
-> Test the webservices from Weblogic EM console like the same way you test SOA webservices.
======================================================
Sample custom methods which you can expose in AM:
public void updateSalary(Number employeeId, Number salary){
Key key = new Key(new Object[] {employeeId});
Row row = this.getEmployeesView1().getRow(key);
row.setAttribute(“Salary”, salary);

try{
this.getDBTransaction().commit();
}
catch(Exception ex){
ex.printStackTrace();
}
// The above methods accepts EmpId, Salary as input and updates the salary for the given EmpId in DB
======================================================
Note: we need Oracle web services manager to define the security policy for AppModule Webservices(according to documentation B31974-05 section 11.2.13). So need to license Oracle SOA suite cos webservices manager comes under SOA suite. So without using SOA, if you want to expose webservice from ADF business components you can not proivde security to them.
These exposed websrevices will creates new AM configuration and uses AppModule pool for DB connection when invoked.
======================================================
References:
http://andrejusb.blogspot.in/2009/08/web-service-interface-for-adf-bc.html

For secured WS you can check this post – http://andrejusb.blogspot.com/2012/11/adf-mobile-secured-web-service-access.html This is about ADF Mobile, but Web Service part is about secured AM Web Service
====================================================== ======================================================
====================================================== ======================================================
====================================================== ======================================================
PART – 2:
In above we have exposed the ADF Business component services as webservice (service enabled data objects/SDO).
Now we can consume those ADF BC services from SOA applicaiton or from another ADF application also.
Now we are discussing on How to consume remote ADF BC services[AM methods, VOs] which are exposed as webservice in other ADF applicaiton.

Create a new ADF applicaiton. [In this applicaiton we create Service Enable Entity Objects, whcih means EOs are created based on SDOs (service enabled objects)]
create Entity object-> when creating EO uncheck defalut datasource selection ‘DB schema object’, check ‘Service Interface’, give the WSDL URL and select the Service ViewInstance from dropdown.
Run the AM, now you can create a record, update record, delete record.
EO based on remote VO exposed though webservice (Service enabled EOs) will be similar to EO based on DB. But Joins doesn’t work for this EO when you create EO based on VO exposed as webservice.
We can declare Business Events on Service-Enabled Entity Objects, it works same as declared on normal Entity Object.
You can see the attributes but you can see the query, as this EO will get data dynamically as this is based on webservice.
So this is how you can crete a EO from a VO based on remote applicaiton which is exposed through webservice.
Deployment: Here first you need to add the ‘applnname_common.jar’ library of exposed applicaion to this consuming applicaiton.
Give the service connection information in connectons.xml. Now you can deploy and test from EM console.
Note: The operations which you select for a view isntance during exposing AM, those will only be available for applications when consuming them.
———————————–
References: http://andrejusb.blogspot.in/2009/08/service-enabled-entity-objects-in.html
====================================================== ======================================================
====================================================== ======================================================
====================================================== ======================================================
PART – 3:
Creating Business Event and subscribing to that event in Oralce SOA.
Busieness Events allows the loose coupling. Busieness Events can be created from ADF BC or from SOA.
These Business Events can be subscribed in SOA composite using Mediator.

References:
http://andrejusb.blogspot.in/2009/08/business-events-and-adf-business.html
http://biemond.blogspot.in/2008/05/events-in-adf-bc-and-handled-by-soa.html

————–

-> Oracle ADF Functional Patterns and Best Practices
http://www.oracle.com/technetwork/developer-tools/adf/index-100668.html

-> Customizing and Personalizing an Application
http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_18/jdtut_11r2_18.html

-> Oracle Fusion Applications
12c: https://docs.oracle.com/applications/falcm12/OADEE/title.htm
11.1.8: https://docs.oracle.com/cd/E48434_01/fusionapps.1118/e15524/gs_gs_e.htm#CDEHADGG

 

ADF Customization

================== Customization: ========================

-> Oracle ADF allows us to create customizable web applications.
-> customizable applications means there will be one base application. On top of that application we develop multiple versions of application using multiple customizable layers and their contexts.
-> One customization layer can have multiple customization contexts (values)
-> At a time only one customizable layer value (context) will be applied on the base application (we can give one cust layer as default in properties file). Note that all the customization layers can be applied but only one context from each layer.
-> We select one customizable layer’s context from the ‘customization context window’ and we can work on the base application.
-> Similarly we can select another customizable layer’s context and work on the base application. Here you need to note that the customizations that you have done in one customizable layer’s context won’t appear in other customizable layer’s context.
Customization layers could be: ‘industry’. ‘site’Customization contrexts could be (need to input like below in proeprties file) industry=financial #industry=healthcare #site=remoteoffices site=headquarters So from the above settings if we run the application industry cust layer’s financial context will be applied on base appln along with site cust layer’s headquarters context.
How to do it? ‘Open Jdeveloepr with Default Role’
-> prerequisite: create one sample ADF web applicaiton.
-> 1) Create Customization Layers in ‘customizationLayerValues.xml’ file (which is present under <JDEVELOPER_HOME>/jdeveloper/jdev) This applies to all applications.If you want to aplly these customization layers to only this applicaiton then open adf-config.xml and udner the MDS tab, click on the link ‘Configure Design Time customizations’ which creates a file ‘customizationLayerValues.xml’ under ApplicaitonResources -> MDS DT  [Create cust layers as ‘site’, ‘industry’]
-> 2) Now define the Customization Contexts for each customization layer. So create a proeprties file ‘customization.proeprties’ under the modal project and give entries like below. industry=financial #industry=healthcare #site=remoteoffices site=headquarters

-> 3) Now create the customization class for each Customization Layer. [SiteCC, IndustryCC]. paste the belwo code into these class files. These classes will pick up the properties file and applies the customization layer’s context on the base applicaiton.
-> 4) Now defiene the created customization classes in adf-config.xml file under MDS tab.
-> 5) right click on viewContrller project -> ADF View -> check ‘Enable Seeded customizations’.
-> 6) Now work on Taskflows and UI. #So far we discussed about base applicaiton. #Now we disucss how to customize this base appln.
-> 7) Close Jdev and Reopen with ‘Fusion  apps developer Role/ Fusion apps Admstr Customization Role’ to start customization.
-> 8) First open with ‘Fusion  apps developer Role’ and create a ‘Fusion Apps customization application’. Name: XxCustomizationDemoApplicationpackage prefix: com.oracleDeployed EAR: baseAppln.earNext  -> Project Name: XxCustomizationDemoProject -> FinishThe created application contains Project ‘XxCustomizationDemoProject’ and folders under it are ‘Appln sources’,’Resources’,’Web content’.The base applicaton war files are available under ‘Application Resources ->  customizable Archive -> Base Appln Archive’.
-> 9) Now close and Reopen jdev with ‘Fusion apps Admstr Customization Role’.Now select the customization layer and its context. These latyers and their contexts are defined in the base application.For Fusion SCM we have the below cust layers.Global -> value/context: GlobalSite -> value/context: Site
-> 10) Under Application Resources -> customizable Archive -> Base Appln Archive ->  Right click and Filter.search for the jspff page/ taskflow (that should present in base applicaiton).Now under  ‘Base Appln Archive’ you can see only filtered files. Right click on that file and click on ‘customize’ option.It’ll promt to Add library. click on Add Library. When you do this under the project a folder ‘ADF Library Customizations’ will be created. And under this folder you see the page/taskflow which you selected to customize. [Note: the page/taskflow will be maintained its full path as in base applicaiton under the ‘ADF Library Customizations’ folder]-> Now you can customize the jsff page. [its not editable and only properties can be modified from property inspector]when you customize any component, under the project a folder ‘ADF Library Customization Sources’ will be created. Under this folder you can see the page which you customized with XML extension (eg: Invoicecreate.jsff.xml) The original full path of the page wil be maintained and under that path again few folders will be created (mdssys -> cust -> Global/Site (cust layer) -> GLOBAL/SITE (cust context) -> Invoicecreate.jsff.xmlFor any New component (jsff page, taskflow, VO etc) you need to switch to ‘Fusion  apps developer Role’.
cont…
=================================================

-> Let’s take an example. Create a base application with ‘EmplApp’ and create business comopnents (EmployeeEO, DeptEO, LocationEO, EmployeeVO, AM). -> Now create a view (employees.jsf) and display the Employee details like a form.So far we have developed the base application.

-> Now suppose our requirement is one customizable layer (LocationLayer) we want to show employee details as table with attributes only related to location.->  Now suppose our requirement is one customizable layer (DepartmentLayer) we want to show employee details as table with attributes only related to department.
-> To fulfill above requirement first we need to close jdev. And reopen with customization role.

-> Now right click on viewcontroller project and select ADF View -> Enable Seeded customizations.

-> Give entries of the requried customization layers in the customizationLayerValues.xml file (which is present under <JDEVELOPER_HOME>/jdeveloper/jdev) This applies to all applications. If you want to aplly these customization layers to only this applicaiton then open adf-config.xml and udner the MDS tab, click on the link ‘Configure Design Time customizations’ which creates a file ‘customizationLayerValues.xml’ under ApplicaitonResources -> MDS DT

-> Now we created customization layers, so we need to define them in the proeprties file. create a properties file in the Modal project ‘customization.proeprties’.  In the properties file define the different contexts to a single cutomization layer.

=====================================