Skip to content

Instantly share code, notes, and snippets.

View MichaelAstreiko's full-sized avatar

Michael MichaelAstreiko

  • Belarus, Minsk
View GitHub Profile
@MichaelAstreiko
MichaelAstreiko / file1.groovy
Created March 29, 2012 18:15
BarCode renderer
import com.google.zxing.MultiFormatWriter
import com.google.zxing.client.j2se.MatrixToImageWriter
import com.google.zxing.common.BitMatrix
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
/**
* @author Michael Astreiko
*/
class BarCodeRendererService {
@MichaelAstreiko
MichaelAstreiko / file1.groovy
Created March 29, 2012 18:22
BarCode format
BarcodeFormat format = BarcodeFormat.valueOf(params.format)
File file = File.createTempFile("temp-barcode", "png")
barCodeRendererService.renderImageToFile(file, params.data, getSize(params.width), getSize(params.height), format)
@MichaelAstreiko
MichaelAstreiko / gist:2339348
Created April 8, 2012 19:11
Simple render REST
def user = User.get(params.id)
render(contentType:'text/json'){
user{
username(user.username)
birthday(user.birthday)
firstName(user.firstName)
lastName(user.lastName)
city(user.city)
....
@MichaelAstreiko
MichaelAstreiko / gist:2339395
Created April 8, 2012 19:16
API UrlMapping
"/api/${domainInPlural}"(controller: 'apiJsonRest', action:'list')
@MichaelAstreiko
MichaelAstreiko / gist:2339446
Created April 8, 2012 19:25
Process API call in Controller
def list = {
def domainNameInPluralForm = params.domainInPlural
def domainName = retrieveDomainName(domainNameInPluralForm)
def entity = grailsApplication.getArtefacts("Domain").find {
it.getShortName() == domainName
}?.clazz
def result = entity.list(params)
def converter = [domainNameInPluralForm:result] as JSON
@MichaelAstreiko
MichaelAstreiko / gist:2339498
Created April 8, 2012 19:35
New DomainClassMarshaller
class SimpleDomainClassMarshaller extends DomainClassMarshaller {
protected static final Log log = LogFactory.getLog(BaseDomainClassMarshaller.class)
protected ProxyHandler proxyHandler
private static List SKIPPED_FIELDS = Arrays.asList("lastUpdated", "dateCreated", "createdBy", "class", "id");
public SimpleDomainClassMarshaller() {
this(false, new HibernateProxyHandler());
}
@MichaelAstreiko
MichaelAstreiko / gist:2339545
Created April 8, 2012 19:47
UserClassMarshaller
class UserClassMarshaller extends SimpleDomainClassMarshaller {
private Map ALTERNATIVE_NAMES = ["username":"screenName"]
protected List getSkippedFields() {
return ["password", "email", "enabled", "accountExpired", "accountLocked",
"passwordExpired", "securityQuestion", "securityAnswer"]
}
protected String getAlternativeName(String originalName){
return ALTERNATIVE_NAMES[originalName]
@MichaelAstreiko
MichaelAstreiko / gist:2339580
Created April 8, 2012 19:56
Register Custom Marshallers
JSON.registerObjectMarshaller(new UserClassMarshaller(), 155)
JSON.registerObjectMarshaller(new SimpleDomainClassMarshaller(), 150)
@MichaelAstreiko
MichaelAstreiko / gist:2361373
Created April 11, 2012 18:55
DataSource properties
properties {
/** The maximum number of active connections that can be allocated from
* this pool at the same time, or zero for no limit.
* Make it '-1' to prevent performance problems with idle connections
*/
maxActive = -1
/**
* The maximum number of active connections that can remain idle in the
* pool, without extra ones being released, or zero for no limit.
*/
@MichaelAstreiko
MichaelAstreiko / file1.groovy
Created September 6, 2012 14:19
Haversine algorithmus
double EARTH_RADIUS = 6371
/**
* Calculate distance based on Haversine algorithmus
* (thanks to http://www.movable-type.co.uk/scripts/latlong.html)
*
* @param latitudeFrom
* @param longitudeFrom
* @param latitudeTo
* @param longitudeTo
* @return distance in Kilometers