Skip to content

Instantly share code, notes, and snippets.

@dhoss
Created May 25, 2016 18:17
Show Gist options
  • Save dhoss/09a3e3a074e975a91e9274a027b12d2c to your computer and use it in GitHub Desktop.
Save dhoss/09a3e3a074e975a91e9274a027b12d2c to your computer and use it in GitHub Desktop.
jooq base dao class issues
package com.lumos.service;
import com.lumos.ConfigReader;
import com.lumos.util.Data;
import com.lumos.util.Page;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.RecordMapper;
import com.lumos.db.tables.pojos.Images;
import org.jooq.Table;
import java.util.List;
import static com.lumos.db.Tables.IMAGES;
/**
* Created by devin on 3/30/16.
*/
public abstract class BaseService <R extends Record, T extends Table<R>, P>{
protected final int pageSize;
protected final ConfigReader config;
protected Data data;
protected final String uploadPath;
protected final String imageViewPath;
private DSLContext sql;
public BaseService(DSLContext s) {
this.data = new Data();
this.config = new ConfigReader();
this.pageSize = Integer.parseInt(this.config.get("pageSize"));
this.uploadPath = this.config.get("imageStorePath");
this.imageViewPath = this.config.get("imageViewPath");
this.sql = s;
}
protected int getPage(String pageParam) {
int page = 1;
if (pageParam != null) {
page = Integer.parseInt(pageParam);
}
return page;
}
public List<P> list(int pageNumber) {
Page pager = new Page(count());
return sql.select()
.from(table())
.orderBy(table().field("UPDATED_ON").desc(), table().field("CREATED_ON").desc())
.limit(pageSize)
.offset(pager.offsetFromPage(pageNumber))
.fetch()
.map(mapper());
}
public Integer count() {
return (Integer)sql.selectCount()
.from(table())
.fetchOne().getValue(0);
}
// to be implemented by the inheriting class
protected abstract T table();
protected abstract RecordMapper<Record, P> mapper();
}
package com.lumos.service;
import com.lumos.ConfigReader;
import com.lumos.db.tables.daos.GalleriesDao;
import com.lumos.db.tables.daos.ImagesDao;
import com.lumos.db.tables.pojos.Galleries;
import com.lumos.db.tables.pojos.Images;
import static com.lumos.db.Tables.IMAGES;
import static com.lumos.db.Tables.GALLERIES;
import org.joda.time.*;
import com.lumos.jooq.JodaDateTimeConverter;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import org.jooq.*;
import com.lumos.util.Data;
import com.lumos.util.DatabaseConnection;
import com.lumos.db.tables.records.GalleriesRecord;
import com.lumos.util.Page;
public class GalleryService extends BaseService<GalleriesRecord, com.lumos.db.tables.Galleries, com.lumos.db.tables.pojos.Galleries> {
// inject these
private GalleriesDao dao;
private ImagesDao imageDao;
private DSLContext sql;
private Page pager;
private DatabaseConnection dbc;
public GalleryService(DSLContext s) {
super(s);
sql = s;
dao = new GalleriesDao(sql.configuration());
imageDao = new ImagesDao(sql.configuration());
}
/*
public List<Galleries> list(int pageNumber) {
pager = new Page(count());
return sql.select()
.from(GALLERIES)
.orderBy(GALLERIES.UPDATED_ON.desc(), GALLERIES.CREATED_ON.desc())
.limit(pageSize)
.offset(pager.offsetFromPage(pageNumber))
.fetch(galleryMapper);
}*/
/*
public Integer count() {
return (Integer)sql.selectCount()
.from(GALLERIES)
.fetchOne().getValue(0);
}*/
public Galleries find(String slug) {
return dao.fetchOneBySlug(slug);
}
public List<Images> imagesFor(String slug, int pageNumber) {
int count = countImagesFor(slug);
pager = new Page(count);
return sql.select()
.from(GALLERIES)
.join(IMAGES)
.on(IMAGES.GALLERY.equal(GALLERIES.ID))
.where(GALLERIES.SLUG.equal(slug))
.orderBy(IMAGES.UPDATED_ON.desc(), IMAGES.CREATED_ON.desc())
.limit(pageSize)
.offset(pager.offsetFromPage(pageNumber))
.fetch(imageMapper);
}
public int countImagesFor(String slug) {
return (Integer)sql.selectCount()
.from(IMAGES)
.where(IMAGES.GALLERY.equal(
sql.select(GALLERIES.ID).from(GALLERIES).where(GALLERIES.SLUG.equal(slug)
)
))
.fetchOne().getValue(0);
}
public void add(Galleries gallery) {
GalleriesRecord g = buildGalleryRecord(gallery);
// set the cover photo to the default cover photo
g.setCoverPhoto(config.get("defaultCoverPhoto"));
g.store();
}
// either everything (insert, update) needs to be void, or it needs to return something
// check Images
public void update(Galleries gallery) {
GalleriesRecord g = buildGalleryRecord(gallery);
g.store();
}
public void delete(Galleries gallery) {
GalleriesRecord g = buildGalleryRecord(gallery);
g.delete();
}
public GalleriesRecord buildGalleryRecord(Galleries gallery) {
// try to fetch the record
GalleriesRecord g = sql.fetchOne(GALLERIES, GALLERIES.ID.equal(gallery.getId()));
if (g == null) {
g = sql.newRecord(GALLERIES);
}
g.setName(gallery.getName());
g.setDescription(gallery.getDescription());
g.setCoverPhoto(gallery.getCoverPhoto());
g.setSlug(gallery.getSlug());
g.setCreatedOn(gallery.getCreatedOn());
g.setUpdatedOn(gallery.getUpdatedOn());
return g;
}
/*@Override
RecordMapper<Record, Galleries> mapper = r -> {
return new Galleries(
r.getValue(GALLERIES.ID),
r.getValue(GALLERIES.NAME),
r.getValue(GALLERIES.DESCRIPTION),
r.getValue(GALLERIES.COVER_PHOTO) == null ? config.get("defaultCoverPhoto") : r.getValue(GALLERIES.COVER_PHOTO),
r.getValue(GALLERIES.SLUG),
r.getValue(GALLERIES.CREATED_ON),
r.getValue(GALLERIES.UPDATED_ON)
);
};*/
@Override
protected com.lumos.db.tables.Galleries table() {
return GALLERIES;
}
@Override
protected RecordMapper<GalleriesRecord, Galleries> mapper() {
return new RecordMapper<GalleriesRecord, Galleries>() {
@Override
public Galleries map(GalleriesRecord r) {
return new Galleries(
r.getValue(GALLERIES.ID),
r.getValue(GALLERIES.NAME),
r.getValue(GALLERIES.DESCRIPTION),
r.getValue(GALLERIES.COVER_PHOTO) == null ? config.get("defaultCoverPhoto") : r.getValue(GALLERIES.COVER_PHOTO),
r.getValue(GALLERIES.SLUG),
r.getValue(GALLERIES.CREATED_ON),
r.getValue(GALLERIES.UPDATED_ON)
);
}
};
}
}
package com.lumos.service;
import com.lumos.ConfigReader;
import com.lumos.db.tables.daos.GalleriesDao;
import com.lumos.db.tables.daos.ImagesDao;
import com.lumos.db.tables.pojos.Galleries;
import com.lumos.db.tables.pojos.Images;
import static com.lumos.db.Tables.IMAGES;
import static com.lumos.db.Tables.GALLERIES;
import com.lumos.util.Page;
import org.joda.time.*;
import com.lumos.jooq.JodaDateTimeConverter;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import org.jooq.RecordMapper;
import com.lumos.util.Data;
import com.lumos.util.DatabaseConnection;
import org.jooq.DSLContext;
import org.jooq.Record;
import com.lumos.db.tables.records.ImagesRecord;
/**
* Created by devin on 4/8/16.
*/
public class ImageService extends BaseService<ImagesRecord, com.lumos.db.tables.Images, com.lumos.db.tables.pojos.Images> {
// inject these
private ImagesDao dao;
private DSLContext sql;
private DatabaseConnection dbc;
private Page pager;
public ImageService(DSLContext sql) throws IOException {
super(sql);
this.sql = sql;
this.dao = new ImagesDao(sql.configuration());
}
/*// I want to figure out a way to put these in the base class
public List<Images> list(int pageNumber) {
pager = new Page(count());
return this.sql.select()
.from(IMAGES)
.orderBy(IMAGES.CREATED_ON.desc())
.limit(pageSize)
.offset(pager.offsetFromPage(pageNumber))
.fetch(imageMapper);
}*/
public Integer count() {
return (Integer)sql.selectCount()
.from(IMAGES)
.fetchOne().getValue(0);
}
public void add(Images image) {
ImagesRecord i = buildImageRecord(image);
i.store();
}
public Images update(Images image) {
ImagesRecord i = buildImageRecord(image);
i.store();
return image;
}
public Images find(String digest) {
return this.dao.fetchOneByDigest(digest);
}
public ImagesRecord buildImageRecord(Images image) {
// try to fetch the record
ImagesRecord i = this.sql.fetchOne(IMAGES, IMAGES.ID.equal(image.getId()));
if (i == null) {
i = this.sql.newRecord(IMAGES);
}
i.setName(image.getName());
i.setDescription(image.getDescription());
i.setDigest(image.getDigest());
i.setPath(image.getPath());
i.setGallery(image.getGallery());
i.setHeight(image.getHeight());
i.setWidth(image.getWidth());
i.setCreatedOn(image.getCreatedOn());
i.setUpdatedOn(image.getUpdatedOn());
return i;
}
@Override
protected com.lumos.db.tables.Images table() {
return IMAGES;
}
@Override
protected RecordMapper<ImagesRecord, Images> mapper() {
return new RecordMapper<ImagesRecord, Images>() {
@Override
public Images map(ImagesRecord r) {
return new Images(
r.getValue(IMAGES.ID),
r.getValue(IMAGES.NAME),
r.getValue(IMAGES.DESCRIPTION),
r.getValue(IMAGES.DIGEST),
r.getValue(IMAGES.PATH),
r.getValue(IMAGES.GALLERY),
r.getValue(IMAGES.HEIGHT),
r.getValue(IMAGES.WIDTH),
r.getValue(IMAGES.CREATED_ON),
r.getValue(IMAGES.UPDATED_ON)
);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment