Skip to content

Instantly share code, notes, and snippets.

@xeviknal
Last active May 1, 2016 01:27
Show Gist options
  • Save xeviknal/7179f2ae80c06fedf753ebb16614f575 to your computer and use it in GitHub Desktop.
Save xeviknal/7179f2ae80c06fedf753ebb16614f575 to your computer and use it in GitHub Desktop.
Easy and handmade serializing for java (influenced from Rails Active Model Serializer)
package com.et.models;
import com.et.util.EventGalleryParam;
import com.et.util.airbrake.AirbrakeLogFactory;
import com.et.util.persistence.ORM;
import org.apache.log4j.Logger;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
public class Event extends ORM {
private static Logger logger = Logger.getLogger(Event.class.getName(), new AirbrakeLogFactory());
public long id;
public long version;
public boolean activo;
public String name;
public long venue_id;
public String description;
public String door_policies;
public long tbl_event_type_id;
public String time;
public String slug;
public Date create_date;
private Venue venue;
public Venue getVenue() {
if(venue == null && venue_id > 0)
this.venue = Venue.find(this.venue_id);
return this.venue;
}
public EventSerializer serialize() {
return new EventSerializer(this);
}
}
package com.et.service;
import com.et.models.Event;
import com.et.util.RequestParam;
import com.et.util.airbrake.AirbrakeLogFactory;
import com.et.util.serializers.web.EventSerializer;
import org.apache.log4j.Logger;
public class EventController {
private static Logger logger = Logger.getLogger(EventController.class.getName(), new AirbrakeLogFactory());
private RequestParam params;
public EventController(RequestParam requestParam) {
this.params = requestParam;
}
public EventSerializer show() {
return Event.findBySlug(params.slug).serialize();
}
}
package com.et.util.serializers.web;
import com.et.models.Event;
import com.et.models.EventType;
import com.et.util.EventGalleryParam;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
public class EventSerializer {
public Long id = null;
public String name = null;
public String description = null;
public String door_policies = null;
public String time = null;
public String date = null;
public EventType event_type = null;
public VenueShortSerializer venue = null;
public ArrayList<EventGalleryParam> gallery = new ArrayList<EventGalleryParam>();
public EventSerializer(Event event){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
this.id = event.id;
this.name = event.name;
this.description = event.description;
this.date = formatter.format(event.create_date);
this.time = event.time;
this.door_policies = event.door_policies;
this.gallery = event.getGallery();
this.event_type = event.getType();
this.venue = event.getVenue().serialize();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment