Saturday, August 22, 2015

Empty String and NULL values in Oracle DB

If you Want to get empty vales(null values ) from the db. What you will do?

Consider the below example:

We have one Employee table.IT is having fields - Emp_id, Emp_name, Emp_salary and Emp_status
Possible values for the Emp_status are Settled – S
                                                                Cancelled – C
                                                                Not settled – ‘’ or NULL

insert into Employee (Emp_id, Emp_name, Emp_salary, Emp_status) VALUES ('1','A','10000','S');
insert into Employee (Emp_id, Emp_name, Emp_salary, Emp_status) VALUES ('2','B','5000','C');
insert into Employee (Emp_id, Emp_name, Emp_salary, Emp_status) VALUES ('3','C','8000','');
insert into Employee (Emp_id, Emp_name, Emp_salary, Emp_status) VALUES (4','D','10000',NULL);
insert into Employee (Emp_id, Emp_name, Emp_salary, Emp_status) VALUES ('5','E','10000', '  ');

Select * from Employee;

Emp_id
Emp_name
Emp_salary
Emp_status
1
A
10000
S
2
B
5000
C
3
C
8000

4
D
10000

5
E
10000


Emp_status, will be showing the status of employee Salary. S and C (Settled/Cancelled), if it is not settled they are not storing any values(’ ’).

If the user want to select the not settled records, the records that of NULL values need to select from DB. So the user will select
The Actual number of not settled records are 3 (last 3 rows – 3,4 and 5)

Select count(*) from Employee where Emp_status = ''; -- 0  
We are excepting to get count as one for the above query.
But it will not display any rows, since empty strings behave as null values in Oracle.

Select count(*) from Employee where Emp_status = NULL – 0

NULL has no datatype, Null is untyped. So that NULL can fit into any of the datatype column.
But we can't use usual operands (=,<,> ..) to compare null.
So Oracle is providing IS NULL and IS NOT NULL condtions to compare NULL values in DB.

Select count(*)  from Employee where Emp_status is null; -- 2
We have 5th row containing the space. But it will not display for the above query.

Select count(*)  from Employee where trim(Emp_status) is null; -- 3
To display all the rows which is not having any values we have to use above query.

We have one more way to compare NULL:-

NVL(expr1, expr2)

If expr1 contains a NULL value, then replace it with the value of expr2
NVL(trim(Emp_status),'N')  - All the Emp_status which is having NULL will be stored as ‘N’
select count(*) from Employee where NVL(trim(Emp_status),'N') = 'N';           -- 3



The above query will give correct count.

Monday, August 17, 2015

Setting up webserver to use with WCS

Here I will be explaining how to setup an IBM HTTP web server for use with a WCS local development environment. 
Setup steps:
Download the installables and execute the installation script

Keep all the defaults. Especially the directory name: C:\Program Files\IBM HTTP Server this will save you from altering the config files later on.



Do Typical




Uncheck Run as service and IBM HTTP Administration as Service



Update the web server configuration




 Edit the httpd.conf file by by replacing all occurrences of <IHSInstallDir> to the IHS install directory.
 Edit the plugin-cfg.xml by replacing all occurrences of <IHSInstallDir> to the IHS install directory.
 Start your Commerce instance
 Add port 1080 to the virtual host



- Goto the admin console ( http://localhost:9060/ibm/console/secure/logon.do ) - Goto Environment -> Virtual Hosts -> WC_default_host -> Host Aliases - Add a new Alias Host Name: * Port: 1080






Testing steps
1. Start the web server server
Run c:\Program Files\IBM HTTP Server\bin\Apache.exe
2. Start your Commerce Server
3. Test dynamic content
Add the 1080 port to any browsing page. Here are some examples

Finding Unique Exception Details from Logs in WCS

In WCS, all the log details will be generated in SystemOut.log and trace.log files, unless the logging feature is customized. It will be difficult to go through the logs and findout the list of unique exceptions in it, especially in a production environment which will be running in unix/aix servers.

The following unix commands can be used to findout unique exceptions in logs

Getting all the unique exception:

Direct your putty output to a text file and execute the following grep. The text file will have all the exception.

grep -io "[a-zA-Z.0-9]*exception" SystemOut.log | sort -i | uniq

Instead of grepping  exceptions alone we grep the entire lines. This will have little more details.

Direct your putty output to a text file and execute the following grep. The text file will have all the exception with whole line from exception trace.
               
 grep -i 'exception' SystemOut.log | sort -u | uniq –u


Enabling Recently Viewed Section in WCS

WCS supports inbuilt functionality to display recently viewed section to the user. It will display the products the customer has recently viewed. To enable the functionality, the following configuration changes need to be done in wc-server.xml file, which will be present under xml/config/wc-server.xml in WC project.

1. Open wc-server.xml file

2. Look for tag "<Marketing" and set the value of its "version" attribute to "Dialog"

3. Search for "<PersistentSession" element and set the value of the "cookieExpiry" attribute to "30"

4. Search for "<PersonalizationId" element, and set its "enable" attribute to "true"

5. Look for the following string compClassName="com.ibm.commerce.marketing.dialog.trigger.SensorEventListener" and set the value of "enable" attribute to "true"

6. Search for compClassName="com.ibm.commerce.marketingcenter.events.runtime.ExperimentEvaluationECEventListenerImpl". Change the value of its attribute "enable" to "true"


7. Save the changes and restart the server.

Sunday, June 14, 2015

Customizing SEO Title in WCS


WCS is using SEOPAGEDEFDESC table for maintaining the SEO related attributes such as meta tile, description and keywords. Google crawlers are using the values in meta description and keyword fields while calculating the ranking. Hence it is important to have meaningful & product/category specific values to be generated in each of the pages displayed to the user.

We can either insert values corresponding to each category and product pages in this table or we can insert substitution parameters defined by WCS, which will get evaluated during runtime. There is a pre-defined set of substitution parameters defined by WCS, a few them can be used only in category pages and others in product pages. For eg: <seo:productShortDescription> can be used only in product details pages.

By default, the SEOPAGEDEFDESC table will be populated with the substitution parameters defined by WCS.

There can be situations, where business want us to configure a different set of title and meta description by providing a template, which is applicable to all products/categories. This can be done by using a script/stored procedure to generate meta title and keyword for each product/category based on the template and insert it into the above table. In this case, there will be an entry in  SEOPAGEDEFDESC against each and every product/category. Each time, a new product gets added to WCS, this activity should happen.

Another option to handle this situation is by creating new substitution parameters. This can be done by inserting a new substitution parameter name in SEOPAGEDEFSUBPARAM table. Insert an entry in CMDREG table to override the default implementation class used for substitution parameter.

INSERT INTO CMDREG (storeent_id,interface,classname) VALUES (0, 'com.ibm.commerce.seo.pagedefinition.commands.ProcessSEOSubstitutionParameterCmd', 'com.test.commerce.CustomSubstitutionCmdImpl')

The business logic to generate meta title and description as per the template provided needs to be written in the CustomSubstitutionCmdImpl.









Generating Data Feeds from Solr


Solr is the default search engine which comes along with Websphere commerce installation. Apart from using it for implementing search functionality in your website, it can be used for feed generation also, when you don't have enough time to create a new feed generation utility. 

This will be done using the wt parameter passed in the Solr request. Given below is a sample Solr request.

http://localhost/solr/MC_10001_CatalogEntry_en_US/select?q=(*:*)&wt=csv

The wt parameter in the request specifies in which format the Solr response will be generated. When the call happen from WCS application, this field will have value javabin. We can also pass other values such as json, based on which it will generate the response in that particular format.

To generate the feed in csv format, what need to be done is to invoke the above URL from a wrapper application/script, which will generate a csv file, which can be ftp'ed to the required location.

Using Solr for feed generation will be ideal in situations when all the required attributes are already available in Solr search engine and the Solr servers have enough capability to handle this additional load.



SEO URLs in WCS


   When it comes to generating SEO URLs, there are mainly 2 things we need to give focus on. One is removing dynamic attributes (? ,= , & etc) from the URL and another one is shortening the url.

In the earlier versions of WCS, it was relying only on configuration files to support SEO URLs. From Websphere Commerce Server 7 Feature pack 3 onwards, we can also maintain SEO URL related data in 2 OOB tables named SEOURL and SEOURLKEYWORD. The keywords mentioned in this table will be used to construct SEO URLs. The configurations mentioned in SEOURLPatterns.xml will be used for the same.

The data can loaded in to these new tables by using seourlkeywordgen utility or it can be loaded along with data load process by configuring loadSEO property as true.

URL Shortening can be done by modifying the following files.

1. httpd.conf
2. wc-server.xml

Httpd.conf:

The following rewrite rule can be added to map the shortened URLs to WCS specific URLs.

 RewriteEngine on
 RewriteRule ^/(.*)$      /webapp/wcs/stores/servlet/$1 [PT,NC]

wc-server.xml:

The SEOConfiguration section in wc-server.xml needs to be modified to change the context root value.

 <SEOConfiguration defaultUrl="" dynamicUrl="true" enable="true">
        <context-root-rewrite value="/" />
 </SEOConfiguration>

To handle 404 and 301 redirects, WCS is also maintaining a table named SEOREDIRECT.

Enabling features in WCS


Once the WCS installation is completed and the required feature packs are installed, the following scripts should be executed to enable them. You need not execute all the features. It depends on what all features you are going to use in your application.

The scripts are present under <WCS_INSTALLATION_DIRECTORY>/bin

enableFeature.bat -DfeatureName=foundation
enableFeature.bat -DfeatureName=management-center
enableFeature.bat -DfeatureName=content-version
enableFeature.bat -DfeatureName=store-enhancements
enableFeature.bat -DfeatureName=social-commerce

enableFeature.bat -DfeatureName=location-services

Thursday, June 11, 2015

Character Functions


Common SQL queries


 
Purpose
SQL Query
O/p
Remarks
Database Name
select ora_database_name from dual;
SQLDB
Return the database name
Full Information of DB
select * from v$database;
 
DbName,creationdate ..
Full information about the database
Current Time
select SYSDATE from dual;
03-JUN-14
 
 
Single line comment
-- description
 
 
Multiline comments
/* …..
 ……  */
 
 

 Character Functions:

 
Function
SQL Query
Length
– No of characters in a string
Returns the Length (no of charaters ) of 'MALAYALAM' – 9
 
select LENGTH('MALAYALAM') from dual;
9
 
select LENGTH(ACCT_NAME) from ACCOUNTS where ACCT_ID='598934';
INITCAP
 – First letterer of each words to capital letter
 
select INITCAP('hello to all') from dual;
Hello To All
 
select INITCAP (ACCT_NAME) from ACCOUNTS where ACCT_ID='598934'
CONCAT
  Concatenate 2 values
 
select CONCAT('EmP No:','18236') from dual;
 
EmP No:18236
 
 
select CONCAT(ACCT_ID || ':',  ACCT_NAME)  from ACCOUNTS where ACCT_ID = '8734545';
||
( connate operator) Concatenate 2 values
 
 
select 'EmP No:' ||'18236'  EMPDetails rom dual;
 
EmP No:18236
 

UPPER
-Convert characters to upper case
select UPPER('MALAYALAM') from dual;
 
MALAYALAM
 
 
 
 
   
 
     select * from  ACCOUNTS where UPPER(acct_name) like '%VISHNU%'
 
LOWER
-Convert characters to lower case
 
 
select LOWER('MALAYALAM') from dual;
 
malayalam
 
 
select * from ACCOUNTS where LOWER(acct_name) like '%vishnu%'
 
ASCII
-Convert characters to ASCII value
 
 
select ASCII('0') from dual;
 
48
 
 
 
INSTR
-To find the position of the given letter
 
INSTR(Value, character to find, starting position from where we need to find Chara position, which occurrence position you need to find)
 
 
select INSTR('MALAYALAM','L')  from dual;
 
3
select INSTR('MALAYALAM','A')  from dual;
 
2
select INSTR('MALAYALAM','A',1)  from dual
2
select INSTR('MALAYALAM','A',1,2)  from dual;
 
4
select INSTR('MALAYALAM','A',3,2)  from dual;
 
6
 
select INSTR(acct_name,'I',8, 2)  from accounts  where acct_id = '124466'
 
LTRIM
LTRIM(String,Value)
 
-Trim the given Value from the left side of the String
 
 
select LTRIM('aaabcd','a') from dual;
 
bcd
 
select LTRIM('   MALAYALAM  ',' ') from dual;
 
MALAYALAM'
 
 
RTRIM
RTRIM(String,Value)
 
-Trim the given Value from the right  side of the String
 
select RTRIM('aaabcdddd','d') from dual;
 
Aaabc
select RTRIM(' MALAYALAM    ',' ') from dual;
 
‘MALAYALAM'
 
 
 
REPLACE
 
REPLACE(String,valuetofind,valuetoreplace)
 
-Find the valuetofind in the String and replace with valuetoreplace
 
 
 
select REPLACE('aaabcdddd','a','e') from dual;
 
eeebcdddd
 
select REPLACE('aaabcdddd','aa','e') from dual;
 
eabcdddd

 
SUBSTR
 
SUBSTR(String,startingposition,endingposition)
-substring will extract the String from startingposition to endingposition and return that extracted String
 
 
 
 
select SUBSTR('aaabcdddd',4,10) from dual;
 
bcdddd
 
 
 
TRANSLATE
 
TRANSLATE(String, series of characters to find, series of characters to replace)
 
select TRANSLATE('aaabcdddd','abd','efg') from dual;
 
 
eeefcgggg
 
In the  above example series of characters to find - 'abd'
                                series of characters to replace - 'efg'
So it will find every ‘a’ from String 'aaabcdddd' and replace it with ‘e’
Next, ‘b’ with ‘f’ next – ‘d’ with ‘g’