Thursday, August 30, 2012

T-SQL - Coding - check table column exists in the database

Option-01: using SYS.COLUMNS 


IF EXISTS(SELECT * FROM SYS.COLUMNS WHERE NAME = N'account_id'  
            AND OBJECT_ID = OBJECT_ID(N'account'))
BEGIN
PRINT 'EXISTS';
END            
;

Option-02: using INFORMATION_SCHEMA.COLUMNS

IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE TABLE_NAME = 'account' 
           AND  COLUMN_NAME = 'account_id')
BEGIN
PRINT 'EXISTS';
END            

Tuesday, August 14, 2012

LDAP - Error - Unable to open log file

Description:
It's a good idea to log the progress of a LDAP import/export to see what entities have successfully imported/exported and what entities are not. To enable logging, we need to use '-j' option in the command with a valid location where log files will be created.

Command:


ldifde -i -f export.ldif -s localhost -t 10389 -a "uid=admin,ou=system" secret -k -j c:\logs

Explanation:
-i = enables import (default is export)
-f = indicate import/export file. After -f user has to provide a valid file to import data from 
-s = server info. Value after -s is server info
-t = port info after the option
-a = execute the command using supplied distinguish name and password
-k = to ignore 'Constraint Violation' and 'Object Already Exists' errors. This is import specific       
       option.
-j = enable logging. Value after this option has to be a valid location

Log Location:
If log location is valid, the command will create two log files ldif.err and ldif.log. First one includes all the errors, second one is for more verbose information


Errors:
1. Log file location must follow -j
2. Unable to open log file

Solution of errors:
1. -j must follow with valid log location and need to make sure no file name
2. Need to make sure file name not provided as log location


Friday, August 10, 2012

Spring - Coding - Intercept a method call using AOP concept

Environment:
1. JDK 1.6
2. Spring 3.1
3. Log4j 1.2.16

Steps:
1. Create a main class which will implement a interface
2. Create a method interceptor/advice class which will intercept the method of class defined in step-01 
3. Setup 'Application Context' configuration with main bean and the bean which will intercept the method call

Application Context:



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

 
 
  RongBorno
 
 
 
  
 
      
 
      class="org.springframework.aop.framework.ProxyFactoryBean">
   
     
        com.spring.interceptcall.MainClassInterface
     
   
   
     
        advice
     
   
   
 


Interface and class of callee class:

package com.spring.interceptcall;
public interface MainClassInterface {
public void aMethod();
}


package com.spring.interceptcall;

public class MainClass implements MainClassInterface {
private String name;

public MainClass(String name){
this.setName(name);
}

public void aMethod(){
//Do something
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Method Interceptor/advice class:

package com.spring.interceptcall;

import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;

public class AdviceClass implements MethodBeforeAdvice {

@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
MainClass mc = (MainClass) target;

Logger log = Logger.getLogger(target.getClass());
log.debug("Class Message= "+ mc.getName() + ", method= "+ method.getName());
}
}

Class to test the sample code:

package com.spring.interceptcall;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ExecuteClass {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("MainClass.xml");

Logger.getLogger(ExecuteClass.class).debug("Before calling method");
((MainClassInterface)context.getBean("mainClass")).aMethod();
Logger.getLogger(ExecuteClass.class).debug("After calling method");
}

}

Log4j configuration:

### direct messages to file ###
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p %c{3}:%-4L - %m%n
log4j.appender.file.File=test.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, file
log4j.logger.com.spring.interceptcall=debug









Thursday, August 9, 2012

Visual Studio - Error - Visual Studio Setup cannot be run in Program Compatibility Mode

Error Description
I have captured screenshot of the error instead putting description (seeing is believing)















Solution-01Check Program Compatibility Assistant registry settings

Steps:
1. Run regedit. 'Start > Run > regedit'
2. Browse to each of the following keys and delete any value that refers to setup.exe in the Visual Studio install path:
    - HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags \Compatibility Assistant\Persisted

    - HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows NT\CurrentVersion \AppCompatFlags\Layers















3. Rerun setup

Additional Information:
Sometimes setup.exe is set to OS compatible mode which might prevent it to run on existing OS. In that case clear out compatibility setting and rerun the setup.

Steps:
1. locate setup.exe.
2. Right-click setup.exe and then click Properties.
3. On the Compatibility tab, clear "Run this program in compatibility mode for" 

4. Then click OK


























Resources:
1. http://go.microsoft.com/fwlink/?linkid=143397