Sorting In Turkish

If you compare strings in Turkish by default string comparator, the result is not what you want. I will give you a small code that sorts strings in Turkish. The below code sorts a list of Turkish cities by name. CollationKey class has better compare performance so if you sort a large list it is logical to use collation key class. I put the keys in a map so I only create the keys once. The code can be used for any locale, just create the locale that is used and give it to Collator class.

Locale trLocale = new Locale("tr","TR);

Collator myCollator = Collator.getInstance(trLocale);

final Map<Long, CollationKey> collationKeyMap = new HashMap<Long, CollationKey>();

for (City city : cityList) {

        CollationKey cityNameCollatorKey = myCollator.getCollationKey(city.getName());

        collationKeyMap.put(city.getId(), cityNameCollatorKey);

}

Collections.sort(cityList,new Comparator<City>(){

public int compare(City city1, City city2){

       CollationKey key1 = collationKeyMap.get(city1.getId());

       CollationKey key2 = collationKeyMap.get(city2.getId());

        return key1.compareTo(key2);

}});

 

Unavaliable Google Services In Turkey - 2

For a couple of days I am able to reach Youtube by using Google DNS. I think Google have changed some ip adresses so that new ip adresses is out of the range of banned Google ip addressees. Suprisingly, nobody writes anything about it in Turkey. May be it is beacuse we have a referendum this weekend and everyone is interested in nothing but the referendum.

Once again Google didn't try to solve the problem with Turkish authorities but instead tried to find a work around for the ban. It is very interesting to see that Google don't obey Turkish court decisions. I wonder why Google trade Turkish market this way. Maybe Google don't find Turkish market valuable enough to make eny effort to actually solve the problem. 

TWTTR

Today, I came cross an article about Twitter by Micheal Arrington of TechCruch. The interesting is that it was written when the site launched in 2006. Here is the article:

"Odeo released a new service today called Twttr, which is a sort of “group send” SMS application. Each person controls their own network of friends. When any of them send a text message to “40404,” all of his or her friends see the message via sms. This launched officially today, and a few select insiders were playing with the service at the Valleyschwag party in San Francisco last night.

People are using it to send messages like “Cleaning my apartment” and “Hungry”. You can also add friends via text message, nudge friends, etc. It really a social network around text messaging – and is very similar to another service called Dodgeball

Users can also post and view messages on the Twttr website, turn off text messages from certain people, turn off messages altogether, etc. There is also a status widget available that can be placed on a website.

I like the service although I was not able to sign up for it myself (someone added me at the same time I tried to register directly, the result was lockout of my phone number). I’ve been playing around with someone else’s account until I can figure out how to get my number sorted out.

There is also a privacy issue with Twttr. Every user has a public page that shows all of their messages. Messages from that person’s extended network are also public. I imagine most users are not going to want to have all of their Twttr messages published on a public website.

If this was a new startup, a one or two person shop, I’d give it a thumbs up for innovation and good execution on a simple but viral idea.

But the fact that this is coming from Odeo makes me wonder – what is this company doing to make their core offering compelling? How do their shareholders feel about side projects like Twttr when their primary product line is, besides the excellent design, a total snoozer?"

It was very interesting to see how Twitter looked in its early days and how the "twit idea" changed in time. It was called a group sms application by that time but now it is developed to a totally new type of application and inspires so many new generation sites. Here is the link to original article.

Unavaliable Google Services In Turkey

At the beginning of this week, some Google services in Turkey got exteremly slow or totally unavailable. Everyone got suprised because there was no complaints from other countries. Authorities in Turkey didn't comment about the situation so no one really know what was happening. Eventually we got a comment about the situation. The comment states that in an attemp to update blocked ip list to cover new ip adresses of Youtube some ip adresses of Google services is banned because they share ip ranges with Youtube. It states that it is up to Google to fix the problem.

This ban has very serious consequences. The sites including the one I am working for that are using Google services like Analytics, Maps etc. loads slowly and have some availability problems. The ban also angried regular people because now changing dns server configuration to reach Youtube is not working.  

There are some interesting comments from Abdullah Gül, president of republic of Turkey, about the ban. Actually he tweeted about the situation and let us know that he does not approve the ban to Youtube or baning any sites. I think this tweets show that some new legal changes may be applied in near future .

Spring Roo With Google Web Toolkit

After watching Google I/O from youtube, I wanted to try the integration of Spring Roo with Google Web Toolkit. I created an example project and after three hours of struggling and surfing through the internet to solve the problems I faced, I succesfully run the project. The first thing you should now is that STS 2.3.3 release which is the first release taking advantage of integration of Google Web Toolkit and Spring Roo has a major bug. If you try to add a maven dependency you see that the search always returns no results. At first I should say this release has a major update, STS won't index dependency list at startup by default. You should activate this feature after installing STS by going Window -> Preferences -> Maven and selecting  "Download repository index updates on startup" option. After activating the feature, the dependency search still returns no results. There is already a jira issue about this.  I can't remember the link of this issue but if you search you can find it. In order to solve this problem I opened "Maven Repositories" view and clicked the "Global Repositories" option. After doing this, STS may not responde for a while because the indexing takes a little while. When it is done indexing, the dependency search feauture will work.

In order to try Google Web Toolkit with Spring Roo, you’ll need to install DataNucleus Eclipse Plugin and Google Plugin for Eclipse. To do so, go the the dashboard and select the extensions tab. Select the plugins, install them and restart the STS.  Now, create a Spring Roo project. Then run the command below by opening Roo Shell view and typing it to console. The command will create persistence layer for the project with DataNucleus support.

persistence setup --provider DATANUCLEUS --database HYPERSONIC_IN_MEMORY

Then create an entity and fields by running the commands below or simply creating the java classes.

entity --class ~.server.domain.Employee --testAutomatically

field string --fieldName userName --sizeMin 3 --sizeMax 30

 

The last step is adding gwt support, to do so run the below command.

 

gwt setup

 Now by rigth clicking the project and selecting the Run -> Web Application option you can see it running.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Note To Self: Don't Use Hibernate POJOs As Information Holders

I lost two hours of my life trying to find how a POJO in my code got saved today. In one part of the code, I used a POJO to hold a state temporarily. That POJO never got saved and it has no harm. At least I thought that way. Later I spent two hours trying to fix a bug. Then I realised I fetched another POJO related to it at another execution point and then I saved it. Then of course I got a bug becuase the earlier one also saved because of the relationship. They are at very different places and it took me so long to figure it out.

Connecting MsSQLServer With Hibernate

I had a lot of problems connecting MSSqlServer with the Microsoft driver. Instead I used jTDS driver. It can be added from maven repository. I added jtds-1.2.4.jar. The connection url is like:

 connection.url = jdbc:jtds:sqlserver:/127.0.0.1:1433/test

 I have only one instance on my computer. I only supplied database name "test" in the connection string and it is enough. Database dirver class is

 connection.driver = net.sourceforge.jtds.jdbc.Driver

 I had no problem with these settings to connecting MsSQLServer 2008.

Using Multiple Databases With Hibernate and Spring

Edit: I had problems with the configuration in this post but I was able to fix it. The problem is when I tried to make a two-phase commit the second transaction didn't begin with annotation based transaction management. But I was able to make a two-phase commit by using programmatic transaction management with second transaction. That means adding below code to the service where second transaction is started. I thing to use transactions with different databases JTA transact.on management should be used.

 

@Qualifier("txManager2")

@Autowired

private PlatformTransactionManager transactionManager;

...

...

...

DefaultTransactionDefinition def = new DefaultTransactionDefinition();

TransactionStatus status = transactionManager.getTransaction(def);

try {

doSomething();

}

catch (Exception ex) {

transactionManager.rollback(status);

        throw ex;

}

transactionManager.commit(status);

 

Recently, I needed to add a second database connection to the project I currently develop for. After searching through the internet I had some ideas in my head but I couldn't not find an exact solution. After a bit struggling, I found a fairly easy solution. At first in ApplicationContext.xml of the project, there was one data source and one session factory which is created with this data source. There was an abstract hibernate dao which is created with this session factory. All dao classes in the project is a subclass of this class. I created a second abstract hibernate class and use this class as parent class for dao classes of new database. Of course I needed a second session factory to create this class so I added a new session factory to ApplicationContext.xml. In order to inject right session factory to rigth abstract hibernate class I added @Qualifier annotation followed by session factory name to the session factory setter  methods of abstract hibernate classes. And of course I added a second data source to ApplicationContext.xml to be used by new session factory. After doing these, the only problem was transaction management and it has also a fairly easy solution. I added a second transaction manager to ApplicationContext.xml to be used with the new session factory.

Now, after all these work, the system has the ability to work with two different databases. I put the resulting classes and ApplicationContext.xml below.

 

  ApplicationContext.xml

      ....
      ....
      ....

 <bean id="txManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory1" />
 </bean>

 <bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource1" />
      ....
      ....
      ....     
  
 </bean>

 <bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
      ....
      ....
      ....
 </bean>
 
 <bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource2" />
      ....
      ....
      ....
 </bean>

 <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
      ....
      ....
      ....
 </bean>

 <bean id="txManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory2" />
 </bean>
 
      ....
      ....
      ....

 

AbstractHibernateDAO1

public abstract class AbstractHibernateDAO1 {
      
      ....
      ....
      ....
   
 protected HibernateTemplate hibernateTemplate;

 @Autowired
 @Qualifier("sessionFactory1")
 public void setHibernateTemplate(SessionFactory sessionFactory) {
  this.hibernateTemplate = new HibernateTemplate(sessionFactory);
 }

      ....
      ....
      ....
      
}

 

AbstractHibernateDAO2

public abstract class AbstractHibernateDAO2 {
      
      ....
      ....
      ....
   
 protected HibernateTemplate hibernateTemplate;

 @Autowired
 @Qualifier("sessionFactory2")
 public void setHibernateTemplate(SessionFactory sessionFactory) {
  this.hibernateTemplate = new HibernateTemplate(sessionFactory);
 }

      ....
      ....
      ....