Thursday, December 23, 2010

Grails Bean builder for Compass Search on Appengine using JPA

Need to put this down in case i forget

beans = {
    compass(org.compass.spring.LocalCompassBean){
mappingScan = "persisted"
compassSettings = ["compass.engine.connection":"gae://index",
"compass.executorManager.type":"disabled",
"compass.engine.store.gae.cacheMetaData":"true",
"compass.engine.store.gae.flushRate":"50"]
    }

    sameThreadParallelIndexExecutor(org.compass.gps.device.support.parallel.SameThreadParallelIndexExecutor){

    }

    jpaGpsDevice(org.compass.gps.device.jpa.JpaGpsDevice){
name="appengine"
entityManagerFactory = entityManagerFactory
parallelIndexExecutor = sameThreadParallelIndexExecutor
    }
  
    compassGps(org.compass.gps.impl.SingleCompassGps){
compass = compass
gpsDevices = [jpaGpsDevice]
    }
}

Sunday, December 5, 2010

Setting Solid Color to DrawableShape loaded from XML

So yea, tried to set a solid color to a ShapeDrawable resource loaded from XML
initially tried:

ShapeDrawable drawable = (ShapeDrawable)getResources().getDrawable(R.drawable.shape)
drawable.getPaint().setColor(color);

Didn't work,

So cast to GradientDrawable instead:
GradientDrawable drawable = (GradientDrawable)getResources().getDrawable(R.drawable.shape)
drawable.setColor(color);

Saturday, September 4, 2010

Working with TimeZones

import java.util.TimeZone;

def tz = TimeZone.getTimeZone("Asia/Kuala_Lumpur")
def date = new Date();
def time = date.getTime()+tz.getOffset(date.getTime())
date.setTime(time)

In GAE:

Wrap the above in a method to bypass Reflections which GAE doesn't support

TimeZone.getAvailableIDs() also doesn't work in GAE because of Reflections

Correction, below works

public Date getDateWithTimeZone(Date date, String timeZone){
def tz = TimeZone.getTimeZone(timeZone);
def cal = Calendar.getInstance()
cal.setTimeInMillis( date.getTime() )
cal.setTimeZone( tz )
def offset = cal.get(Calendar.ZONE_OFFSET)
date.setTime( date.getTime()+offset )
return date;
}

Wednesday, September 1, 2010

Localization i18n for Grails and App Engine

Getting localization working on Grails on GAE
see below for workaround

checkout this jira:
http://jira.codehaus.org/browse/GRAILSPLUGINS-1905

Tuesday, August 31, 2010

JPA query syntax

Within a controller:

def query = entityManager.createQuery("select from Account a where a.key=:key order by ${params.sort} ${params.order}")
query.setParameter("key",session.key)
query.setMaxResults( params.max )
query.setFirstResult( params.offset )
def accounts = query.resultList;

EntityManager isn't injected in Services but jpaTemplate is so:

private getEntityManager(){
jpaTemplate.execute({EntityManager entityManager->
return entityManager;
} as JpaCallback);
}

Thursday, July 22, 2010

Android Development

I know i've been neglecting this blog for a while, but i've recently picked up a Samsung Galaxy S and will be refocusing on developing apps on Android and posting what i learn here.

Friday, May 7, 2010

Resolving filename conflict

In case i forget
/*
*Returns a existing file if the filename given has already been used in a folder/root
**/
public FileResource getConflictedFile(String fileName, String licenseKey, Long parentDirectoryId){
 return FileResource.withCriteria(uniqueResult:true){
  ilike("name","%"+fileName+"%")
  eq("licenseKey",licenseKey)
  if(parentDirectoryId){
   parentDirectory{
    eq("id",parentDirectoryId)
   }
  }
  else{
   isNull("parentDirectory")
  }
  fetchMode("parentDirectory",FM.EAGER)
 }
}

public String createUniqueFileNameInFolder(String fileName, String licenseKey, Long parentDirectoryId){
 def file = getConflictedFile(fileName, licenseKey, parentDirectoryId)
 if( file ){
  def fName = file.name
  int dotPos = fName.lastIndexOf(".")
  def name = fName.substring(0,dotPos);
  def fileExt = fName.substring(dotPos+1);

  //check if there is a _numeric at the end of the file name
  if( name.lastIndexOf("_")==-1 ){
   fName = name+"_1."+fileExt;
  }
  else{
   int usPos = name.lastIndexOf("_");
   def copyNumber = name.substring( usPos+1 )
   if( copyNumber.isLong() ){
    copyNumber = copyNumber.toLong()+1
    name = name.substring(0,usPos)
    fName = name+"_"+copyNumber+"."+fileExt;
   }
   else{
    fName = name+"_1."+fileExt;
   }
  }
  //if yes then extract, increment, append and rerun again until no conflicts
  return createUniqueFileNameInFolder(fName,licenseKey,parentDirectoryId)
 }
 else{
  return fileName;
 }
}