Thursday, June 30, 2011

SQL To Find Records That Do Not Have Association

SQL Server Query to determine specific records that do not have a specific association to them:

select distinct documentid, ownername from contenttextpub where documentid in (select distinct documentid from contenttext where contentid in (Select contentid from contentsecuritytag where tagid=(select recordid from tag where referencekey='NULLTAG')))

Feels like recursion, but its more tied to the SQL Query grammer and how the SQL Server interpreter parses the query to get the final query results.

Tuesday, June 28, 2011

Mathematical Proofs and Business Requirements for Product Design

In College I took advanced math classes that taught us about Symmetric, Transitive, and Reflexive Relations. I thought these relations in proofs was not required, but then as we went through different equations I then started to understand the importance of these concepts.

With product development, I think a lot of these concepts should be thought about when designing a product. Because users will always attempt to use your product in ways that you had never imagined. And to attempt to foresee these usages of the product can become cost prohibitive.

Thursday, June 9, 2011

URL link creation

// Create URL link that social media sites can use
String linkUrl = java.net.URLEncoder.encode(request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + request.getContextPath() + "/index?"
+ request.getQueryString());

Monday, April 4, 2011

Oracle Trigger

Interesting trigger I worked on that uses exception handling to not insert records until all the required tables have populated data.

CREATE OR REPLACE TRIGGER CONTENTTEXTTOKEN_TRIGGER_1
AFTER INSERT OR UPDATE ON CONTENTTEXT
FOR EACH ROW
DECLARE
content_contentid VARCHAR2(64);
token_from_xml VARCHAR2(100);
token_recordid VARCHAR2(64);
xml_counter NUMBER := 1;
xml_clob CLOB;
BEGIN
/* Check if there is a clob in CONTENTDATA that corresponds with this update */
GET_XML_CLOB_PROCEDURE(:NEW.RECORDID, xml_clob);
IF DBMS_LOB.GETLENGTH( xml_clob ) > 0 THEN
/* Check for first token in clob */
xml_counter := REGEXP_INSTR(xml_clob, '\{(\s*?.*?)*?\}', xml_counter+1);
WHILE xml_counter <> 0 LOOP
/* Found a token, now get the token from the clob */
token_from_xml := SUBSTR(xml_clob, xml_counter+1, INSTR(SUBSTR(xml_clob, xml_counter+1), '}', 1, 1)-1);
xml_counter := REGEXP_INSTR(xml_clob, '\{(\s*?.*?)*?\}', xml_counter+1);
BEGIN
GET_TOKEN_RECORDID_PROCEDURE(token_from_xml, token_recordid);
IF length(token_recordid) > 0 THEN
BEGIN
SELECT CONTENTTEXTID INTO content_contentid
FROM CONTENTTEXTTOKEN
WHERE CONTENTTEXTID = :NEW.CONTENTID
AND TOKENID = token_recordid
AND TOKENNAME = token_from_xml;
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO CONTENTTEXTTOKEN (CONTENTTEXTID, TOKENID, TOKENNAME) VALUES (
:NEW.CONTENTID,
token_recordid,
token_from_xml
);
END;

BEGIN
SELECT CONTENTID INTO content_contentid
FROM CONTENTLINK
WHERE CONTENTID = :NEW.CONTENTID;
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO CONTENTLINK (CONTENTID, DOCID) VALUES (
:NEW.CONTENTID,
:NEW.DOCUMENTID
);
END;
END IF;
END;
content_contentid := NULL;
END LOOP;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
/


CREATE OR REPLACE PROCEDURE GET_XML_CLOB_PROCEDURE (
ct_recordid IN VARCHAR2,
xml_clob OUT CLOB ) IS
BEGIN
SELECT XML INTO xml_clob FROM CONTENTDATA WHERE RECORDID = ct_recordid;
EXCEPTION
WHEN NO_DATA_FOUND THEN
xml_clob := EMPTY_CLOB();
END;
/

CREATE OR REPLACE PROCEDURE GET_TOKEN_RECORDID_PROCEDURE (
token_from_xml IN VARCHAR2,
token_recordid OUT VARCHAR2 ) IS
BEGIN
SELECT recordid INTO token_recordid FROM replacementtokens WHERE tokenname = token_from_xml;
EXCEPTION
WHEN NO_DATA_FOUND THEN
token_recordid := '';
END;
/

Friday, April 16, 2010

Notepad++

I like Notepad++ because:

Acts like tail -f command with text files

when I type notepad++ filename it brings the document into the same notepad++ session I have open

lightweight, open source

Thursday, December 17, 2009

Hibernate N+1 Problem

Good article for hibernate users:

http://www.realsolve.co.uk/site/tech/hib-tip-pitfall.php?name=n1selects

Solution

We solve this problem by making sure that the initial query fetches all the data needed to load the objects we need in their appropriately initialized state. One way of doing this is using an HQL fetch join. We use the HQL

"from Manufacturer manufacturer join fetch manufacturer.contact contact"

with the fetch statement. This results in an inner join:

select MANUFACTURER.id from manufacturer and contact ... from
MANUFACTURER inner join CONTACT on MANUFACTURER.CONTACT_ID=CONTACT.id

Using a Criteria query we can get the same result from

Criteria criteria = session.createCriteria(Manufacturer.class);
criteria.setFetchMode("contact", FetchMode.EAGER);

which creates the SQL

select MANUFACTURER.id from MANUFACTURER left outer join CONTACT on
MANUFACTURER.CONTACT_ID=CONTACT.id where 1=1

In both cases, our query returns a list of Manufacturer objects with the contact initialized. Only one query needs to be run to return all the contact and manufacturer information required for the example.

Friday, December 4, 2009

Very nice article regarding the history of scaling major websites.

http://natishalom.typepad.com/nati_shaloms_blog/2008/05/twitter-as-an-e.html

Monday, November 30, 2009

Java Generics and Recursive Assembly

I was working on a programming assignement, in which I used the following helper class:

private static ArrayList fromArrayToCollection(E[] a) {
ArrayList al = new ArrayList();
for (E o : a) {
if(o != null)
al.add(o);
}
return al;
}

but I wanted to write something that would recursively return an ArrayList for multi-dimensional arrays. Which I would have compile, but would receive various runtime issues with the values not being loaded into the return value.

Monday, April 7, 2008

Pimp My Ride - Geek Style

Went to Fry's Electronics Sunnyvale

Got:


PC Case: Apevia X-QPACK2


Graphics Card: BFG 9600GT



Main Memory: Corsair 2DMM 2GB

Processor: Intel Core2Quad 2.4GHz 8MB Cache

Hard Disk: Maxtor 500 GB HD

Motherboard: Intel DQ35JO

Monitor: Samsung 22" LCD 800:1 DC

OS: Windows XP Pro

Game Headset: Inland Vibration

Game Mouse: Razer DeathAdde Infrafred (Glowing Heartbeat!)

Game Keyboard: Saitek blue LED backlighting

Tuesday, February 12, 2008

Architect Questions

How do you ensure scalability in J2EE environment?


what version of hibernate have you used?

What parts of J2EE architecture do you recommend to avoid?

Friday, February 8, 2008

Interview Questions that were interesting...

Have you used Distributed Cache Product

http://www.oracle.com/products/middleware/coherence/index.html

The Leading Distributed In-Memory Data Grid Solution
Oracle Coherence is a component of Oracle Fusion Middleware that enables organizations to predictably scale mission-critical applications by providing fast and reliable access to frequently used data. By automatically and dynamically partitioning data in memory across multiple servers, Oracle Coherence enables continuous data availability and transactional integrity, even in the event of a server failure. As a shared infrastructure, Oracle Coherence combines data locality with local processing power to perform real-time data analysis, in-memory grid computations, and parallel transaction and event processing. Oracle Coherence comes in three editions. Find the edition that's right for you.

Announcing Oracle Coherence 3.3
Among many enhancements in the new release are improved performance, Quality-of-Service and clustering capabilities, and support for Microsoft's .NET Framework.

Do you know what a Covariant return type?

In J2SE 5 a method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass. This feature removes the need for excessive type checking and casting.

More specifically, covariance of the return type refers to a situation where the return type of the overriding method is changed to a type related to (but different from) the return type of the original overridden method. The relationship between the two covariant return types is usually one which allows substitution of the one type with the other, following the Liskov substitution principle. This usually implies that the return types of the overridding methods will be subtypes of the return type of the overridden method. The above example specifically illustrates such a case.

http://en.wikipedia.org/wiki/Covariant_return_type

What is an anonymous class? What is it used for?

An anonymous class can be particularly useful in those cases where
There is no reason for an object of the anonymous class to exist in the absence of an object of the enclosing class.
There is no reason for an object of the anonymous class to exist outside a method of the enclosing class.
Methods of the object of the anonymous class need access to members of the object of the enclosing class.
Methods of the object of the anonymous class need access to final local variables and method parameters belonging to the method in which the anonymous class is defined.
Only one instance of the anonymous class is needed.
There is no need for the class to have a name that is accessible elsewhere in the program.

Write a bean class that utilizes generics in its syntax?

public class GenericsBean <T> {

private T t; // T stands for "Type"

public void set(T t) {
this.t = t;
}

public T get() {
return t;
}
}

Type Parameter Naming Conventions

E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types


Hibernate version have you used?

EJB Version?

CDN experience content delivery networks

Easy Homework Assignment

Instructions

The following questions will help us assess your Java Programming skills. In all questions below, please make sure that the code you write is syntactically and semantically correct. Ideally, we should be able to compile and execute your code. Psuedo-code is not acceptable. Please read all instructions and directions before proceeding.

1. Is there a memory leak in the code below? If yes, can you spot the memory leak and fix the code to not leak?

public class Stack {
private Object[] elements;
private int size = 0;

public Stack(int initialCapacity) {
this.elements = new Object[initialCapacity];
}

public void push(Object e) {
ensureCapacity();
elements[size ++] = e;
}

public Object pop() {
if(size == 0)
throw new EmptyStackException();
return elements[--size];
}

public void ensureCapacity() {
if(elements.length == size) {
Object[] oldElements = elements;
elements = new Object[2 * elements.length + 1];
System.arraycopy(oldElements, 0, elements, 0, size);
}
}
}

If the stack grows and then shrinks, the objects will not be garbage collected that are being popped off the stack. The method should be re-written as:

public Object pop() {
If (size > 0) {
Object returnVal = elements[--size];
elements[size] = null;
return returnVal;
}
return null;
}


2. Write a class StringUtil that has a method reverseElements that accepts a String array and which returns the SAME String array with the array elements in reverse order. The signature of reverseElements should be

Void reverseElements(String[] strArr)


If strArr is { “dog”, “cat”, “mouse”, “cow” } before calling reverseElements, it should be {“cow”, “mouse”, “cat”, “dog”} after the call.

Using any other extra collection/array should also be avoided. We want you to manipulate the contents of the strArr itself.

public class StringUtil {
public StringUtil() { }

public void reverseElements(String[] strArr) {
int left, right;

for (left=0, right=strArr.length-1; left < right; left++, right--) {
String temp = strArr[left];
strArr[left] = strArr[right];
strArr[right] = temp;
}
}
}

3. Write a unit test for StringUtil#reverseElements.

public void testReverseElements() {
String myStrArr[] = { "dog", "cat", "mouse", "cow" };
StringUtil mySU = new StringUtil();
mySU.reverseElements(myStrArr);
assertEquals("cow", myStrArr[0]);
assertEquals("mouse", myStrArr[1]);
assertEquals("cat", myStrArr[2]);
assertEquals("dog", myStrArr[3]);
}


4. Define class Foo such that there can at any time be exactly one instance of Foo.

public class Foo {
private static Foo myInstance;

private Foo() { }

public static Foo getInstance() {
if (myInstance == null)
myInstance = new Foo();

return myInstance;
}
}


5. class Bar {
private String _name;
private int _age;

Bar(String inName, int inAge) {
_name = inName;
_age = inAge;
}
}

Identify the base class of Bar. Bar is its own base class
Determine the method signature of equals in Bar’s base class equals(String, int)
Override and re-implement it such that the equals method returns true only if name and age match. In other words we want to be able to compare two Bar instances.

class FooBar extends Bar {
FooBar(String inName, int inAge) {
super(inName, inAge);
}

@Override
public boolean equals(String inName, int inAge) {
return super.equals(inName, inAge);
}
}


6. In the question above, if you override the equals method, is there any other method of class Object that needs to be overridden? If so, which one?

It is a general rule that if you override the equals() method you always override the hashCode() method also.

Wednesday, January 30, 2008

Perl Scripts

Perl script that I wrote

#!/usr/bin/perl
# validHPFF.pl

# Command Line Arguement processing
$numArgs = $#ARGV + 1;
if($numArgs == 1) {
# print "Input Directory = $ARGV[0]\n";
} else {
print "Not enough command line arguements were provided\n";
die "Usage: validHPFF.pl \n";
}

#Now get input directory and start to generate files into output directory
$currentPath = `pwd`;
chop($currentPath);
$inputDir = $currentPath . "/" . $ARGV[0];
# print "Input Directory = " . $inputDir . "\n";
#opendir(DIRHANDLE,$inputDir);
opendir(DIRHANDLE,$ARGV[0]);
@files = readdir(DIRHANDLE);
foreach $file (@files) {
if ( index($file, '.OUT') != -1) {
$infile = $ARGV[0] . '/' . $file;
open(INFILE, "$infile") or die "could not open $infile\n";
print "Validating file: " . $file . "\n";
$firstTime = 1;
# This while statement will load each line from infile
# and place its contents into a variable called $_
# and iterate through the file until EOF
$fileLineCount = 0;
while( ) {
$fileLineCount = $fileLineCount + 1;

# Header Section
if(m/^H/) {

# Validation section
@headerFields = split(/\/, $_);
$hfCount = 0;
foreach my $field (@headerFields) {
$hfCount = $hfCount + 1;
# print "header field " . $hfCount . ": $field\n";
if ( length($field) == 0) {
print "Error: Header field " . $hfCount . " is empty\n";
print "Error: Line number " . $fileLineCount . "\n";
}
}
if ( $hfCount != 2) {
print "Error: Header didn't have enough fields: " . $hfCount . " expected 2\n";
print $_ . "\n";
}
}

# Now check Account
if(m/^A/) {

# Validation section
@accountFields = split(/\/, $_);
$afCount = 0;
foreach my $field (@accountFields) {
$afCount = $afCount + 1;
# print "account field " . $afCount . ": $field\n";
if ( length($field) == 0) {
print "Error: Account field " . $afCount . " is empty, line number " . $fileLineCount . "\n";
}
}
if ( $afCount != 3) {
print "Error: Account didn't have enough fields: " . $afCount . " expected 3, line number: " . $fileLineCount . "\n";
print $_ . "\n";
}
}

# Billsummary section
if(m/^B/) {

# Validation section
@billsummFields = split(/\/, $_);
$bfCount = 0;
foreach my $field (@billsummFields) {
$bfCount = $bfCount + 1;
# print "billsumm field " . $bfCount . ": $field\n";
if ( length($field) == 0) {
print "Error: Billsumm field " . $bfCount . " is empty, line number " . $fileLineCount . "\n";
}
}
if ( $bfCount != 9) {
print "Error: Billsumm didn't have enough fields: " . $bfCount . " expected 9, line number: " . $fileLineCount . "\n";
print $_ . "\n";
}
}

# P Member section
if(m/^PM/) {
# $reportHash{"PM"} = $reportHash{"PM"} + 1;

# Validation section
@pmFields = split(/\/, $_);
$pfCount = 0;
foreach my $field (@pmFields) {
$pfCount = $pfCount + 1;
# print "pm field " . $pfCount . ": $field\n";
if ( length($field) == 0) {
if ( ($pfCount != 7) && ($pfCount != 9) && ($pfCount != 6) ) {
print "Error: PM field " . $pfCount . " is empty, line number " . $fileLineCount . "\n";
}
}
}
if ( $pfCount != 10) {
print "Error: PM didn't have enough fields: " . $pfCount . " expected 10, line number: " . $fileLineCount . "\n";
print $_ . "\n";
}
}

# PR section
if(m/^PR/) {
# $reportHash{"PR"} = $reportHash{"PR"} + 1;

# Validation section
@pmFields = split(/\/, $_);
$pfCount = 0;
foreach my $field (@pmFields) {
$pfCount = $pfCount + 1;
# print "pr field " . $pfCount . ": $field\n";
if ( length($field) == 0) {
if ( ($pfCount != 6) && ($pfCount != 8) && ($pfCount != 9) ) {
print "Error: PR field " . $pfCount . " is empty, line number " . $fileLineCount . "\n";
}
}
}
if ( $pfCount != 10) {
print "Error: PR didn't have enough fields: " . $pfCount . " expected 10 fields, line number: " . $fileLineCount . "\n";
print $_ . "\n";
}
}

# RA section
if(m/^RA/) {
# $reportHash{"RA"} = $reportHash{"RA"} + 1;

# Validation section
@raFields = split(/\/, $_);
$rfCount = 0;
foreach my $field (@raFields) {
$rfCount = $rfCount + 1;
# print "RA field " . $rfCount . ": $field\n";
if ( length($field) == 0) {
if ( ($rfCount != 6) && ($rfCount != 7) && ($rfCount != 8) &&
($rfCount != 10) && ($rfCount != 11) && ($rfCount != 12) &&
($rfCount != 13) ) {
print "Error: RA field " . $rfCount . " is empty, line number " . $fileLineCount . "\n";
}
}
}
if ( $rfCount != 15) {
print "Error: RA didn't have enough fields: " . $rfCount . " expected 15, line number: " . $fileLineCount . "\n";
print $_ . "\n";
}
}

# RD section
if(m/^RD/) {
# $reportHash{"RD"} = $reportHash{"RD"} + 1;

# Validation section
@rdFields = split(/\/, $_);
$rfCount = 0;
foreach my $field (@rdFields) {
$rfCount = $rfCount + 1;
# print "RD field " . $rfCount . ": $field\n";
if ( length($field) == 0) {
if ( ($rfCount != 7) && ($rfCount != 8) && ($rfCount != 11) ) {
print "Error: RD field " . $rfCount . " is empty, line number " . $fileLineCount . "\n";
}
}
}
if ( $rfCount != 13) {
print "Error: RD didn't have enough fields: " . $rfCount . " expected 13, line number: " . $fileLineCount . "\n";
print $_ . "\n";
}
}

# P section
if(m/^P\/) {
# $reportHash{"P"} = $reportHash{"P"} + 1;

# Validation section
@pFields = split(/\/, $_);
$pfCount = 0;
foreach my $field (@pFields) {
$pfCount = $pfCount + 1;
# print "P field " . $pfCount . ": $field\n";
if ( length($field) == 0) {
print "Error: P field " . $pfCount . " is empty, line number " . $fileLineCount . "\n";
}
}
if ( $pfCount != 13) {
print "Error: P didn't have enough fields: " . $pfCount . " expected 13, line number: " . $fileLineCount . "\n";
print $_ . "\n";
}
}

# Footer section
if(m/^F/) {
# $reportHash{"F"} = $reportHash{"F"} + 1;

# Validation section
@fFields = split(/\/, $_);
$fCount = 0;
foreach my $field (@fFields) {
$fCount = $fCount + 1;
# print "F field " . $pfCount . ": $field\n";
if ( length($field) == 0) {
print "Error: F field " . $fCount . " is empty, line number " . $fileLineCount . "\n";
}
}
if ( $fCount != 2) {
print "Error: F didn't have enough fields: " . $fCount . " expected 2, line number: " . $fileLineCount . "\n";
print $_ . "\n";
}
}
}

close (INFILE);
}
}

Friday, January 25, 2008

Eclipse Plugins

I have found a number of interesting articles over the years regarding eclipse that I think it would be useful to document for later referal:

Sangam Eclipse Plugin: http://sangam.sourceforge.net/
Useful for pair programming