The Artima Developer Community
Sponsored Link

Java Buzz Forum
Kodo Sequence Generator for SimpleClassNames as primary key

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Marc Logemann

Posts: 594
Nickname: loge
Registered: Sep, 2002

Marc Logemann is founder of www.logentis.de a Java consultancy
Kodo Sequence Generator for SimpleClassNames as primary key Posted: Oct 8, 2006 9:39 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Marc Logemann.
Original Post: Kodo Sequence Generator for SimpleClassNames as primary key
Feed Title: Logemann Blog
Feed URL: http://www.logemann.org/blojsom/blog/default/?flavor=rss2
Feed Description: Marc's thoughts on Java and more
Latest Java Buzz Posts
Latest Java Buzz Posts by Marc Logemann
Latest Posts From Logemann Blog

Advertisement


ATTENTION. WIKI SYNTAX. PLEASE VIEW ON ORIGINAL BLOG URL

I convinced myself to write the custom sequenceFactory for kodo because i wanna be on the safe side when refactoring again before i move to kodo 4. For all all kodo users out there still relying on "db-class" as sequence factory in kodo, this might be useful.

{code}
package de.logentis.kodo;

import kodo.jdbc.schema.ClassDBSequenceFactory;
import kodo.jdbc.meta.ClassMapping;

/**
* Enhanced Sequence Factory capable of having simple Classnames as primary key
* Date: 08.10.2006
* Time: 15:32:14
*
* @author Marc Logemann - Logentis e.K
* @version $Id$
*/
public final class SimpleClassDBSequenceFactory extends ClassDBSequenceFactory {

private String useSimpleNames;

public String getUseSimpleNames() {
return useSimpleNames;
}

public void setUseSimpleNames(String useSimpleNames) {
this.useSimpleNames = useSimpleNames;
}

protected Object getPrimaryKey(ClassMapping classMapping) {
if (getIgnoreVirtual())
for (; classMapping.getPCSuperclass() != null &&
classMapping.getNonVirtualPCSuperclassMapping() != null;
classMapping = classMapping.getNonVirtualPCSuperclassMapping())
;
else
for (; classMapping.getPCSuperclass() != null;
classMapping = classMapping.getPCSuperclassMapping())
;

if (Boolean.valueOf(useSimpleNames).booleanValue()) {
String completeName = classMapping.getDescribedType().getName();
String[] parts = completeName.split("\\.");
if(parts != null && parts.length > 0)
return parts[parts.length - 1];
else
return classMapping.getDescribedType().getName();
} else {
return classMapping.getDescribedType().getName();
}
}
}
{code}

This is the Java 1.4 version. If you are on 1.5, you should replace the last if block with the following code:

{code}
if (Boolean.valueOf(useSimpleNames))
return classMapping.getDescribedType().getSimpleName();
else
return classMapping.getDescribedType().getName();
{code}

I also introduced a parameter which controls if simpleNames should be used or not. You can configure this SequenceFactory via normal kodo configuration style syntax. I am exposing it in XML here:

{code}
<prop key="kodo.jdbc.SequenceFactory">
de.logentis.kodo.SimpleClassDBSequenceFactory(Increment=1, useSimpleNames=true)
</prop>
{code}

If you use Kodo4. You can use internal factories which are capable of using simple classnames as i read it. But i am quite sure most kodo users are still on 3.x

Read: Kodo Sequence Generator for SimpleClassNames as primary key

Topic: PeopleOverProcess.com: SlideShare Previous Topic   Next Topic Topic: The Craftsman articles from Uncle Bob

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use