Created
May 26, 2016 18:09
-
-
Save dhoss/d40edcbfc111ad7604bc8ce0d3c9bdec to your computer and use it in GitHub Desktop.
recordmapper issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, E>{ | |
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; | |
} | |
// I don't want to add a new RecordMapper for each table | |
protected RecordMapper<Record, Images> imageMapper = 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) | |
); | |
}; | |
protected int getPage(String pageParam) { | |
int page = 1; | |
if (pageParam != null) { | |
page = Integer.parseInt(pageParam); | |
} | |
return page; | |
} | |
public List<E> 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<R, E> mapper(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment