/**
* Autogenerated by Jack
*
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
*/
package com.rapleaf.jack.test_project.database_1.mock_impl;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.util.HashSet;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Date;
import java.sql.Timestamp;
import com.rapleaf.jack.AbstractMockDatabaseModel;
import com.rapleaf.jack.queries.ModelQuery;
import com.rapleaf.jack.ModelWithId;
import com.rapleaf.jack.queries.WhereConstraint;
import com.rapleaf.jack.queries.QueryOrder;
import com.rapleaf.jack.queries.LimitCriterion;
import com.rapleaf.jack.queries.OrderCriterion;
import com.rapleaf.jack.test_project.database_1.models.Comment;
import com.rapleaf.jack.test_project.database_1.models.Comment.Id;
import com.rapleaf.jack.test_project.database_1.iface.ICommentPersistence;
import com.rapleaf.jack.test_project.database_1.query.CommentQueryBuilder;
import com.rapleaf.jack.test_project.IDatabases;
public class BaseMockCommentPersistenceImpl extends AbstractMockDatabaseModel<Comment, IDatabases> implements ICommentPersistence {
private final IDatabases databases;
private static AtomicInteger curId = new AtomicInteger(1);
public BaseMockCommentPersistenceImpl(IDatabases databases) {
super(databases);
this.databases = databases;
}
@Override
public Comment create(Map<Enum, Object> fieldsMap) throws IOException {
String content = (String) fieldsMap.get(Comment._Fields.content);
int commenter_id = (Integer) fieldsMap.get(Comment._Fields.commenter_id);
long commented_on_id = (Long) fieldsMap.get(Comment._Fields.commented_on_id);
Long created_at_tmp = (Long) fieldsMap.get(Comment._Fields.created_at);
long created_at = created_at_tmp == null ? 28800000 : created_at_tmp;
return create(content, commenter_id, commented_on_id, created_at);
}
public Comment create(final String content, final int commenter_id, final long commented_on_id, final long created_at) throws IOException {
long __id = curId.getAndIncrement();
Comment newInst = new Comment(__id, content, commenter_id, commented_on_id, created_at, databases);
records.put(__id, newInst);
clearForeignKeyCache();
return newInst.getCopy();
}
public Comment create(final int commenter_id, final long commented_on_id, final long created_at) throws IOException {
long __id = curId.getAndIncrement();
Comment newInst = new Comment(__id, null, commenter_id, commented_on_id, created_at, databases);
records.put(__id, newInst);
clearForeignKeyCache();
return newInst.getCopy();
}
public Comment createDefaultInstance() throws IOException {
return create(0, 0L, 0L);
}
public Set<Comment> find(Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(fieldsMap);
}
public Set<Comment> find(Set<Long> ids, Map<Enum, Object> fieldsMap) throws IOException {
return super.realFind(ids, fieldsMap);
}
public Set<Comment> find(ModelQuery query) throws IOException {
Set<Comment> allResults = super.realFind(query);
LimitCriterion limitCriterion = query.getLimitCriterion();
if(limitCriterion == null) {
return allResults;
}
int i = 0;
Set<Comment> truncatedSet = new HashSet<Comment>();
Iterator<Comment> iterator = allResults.iterator();
while(iterator.hasNext() && i < limitCriterion.getNResults()) {
truncatedSet.add(iterator.next());
i++;
}
return truncatedSet;
}
public List<Comment> findWithOrder(ModelQuery query) throws IOException {
List<Comment> allResults = sortUnorderedMockQuery(super.realFind(query), query);
LimitCriterion limitCriterion = query.getLimitCriterion();
if(limitCriterion == null) {
return allResults;
}
return allResults.subList(limitCriterion.getOffset(), limitCriterion.getNResults() + limitCriterion.getOffset());
}
private List<Comment> sortUnorderedMockQuery(Set<Comment> unorderedResult, ModelQuery query) {
final List<OrderCriterion> orderCriteria = query.getOrderCriteria();
List<Comment> result = new ArrayList<Comment>(unorderedResult);
Collections.sort(result, new Comparator<Comment>() {
public int compare(Comment t1, Comment t2) {
for (OrderCriterion orderCriterion : orderCriteria) {
int compareResult;
Enum field = orderCriterion.getField();
String fieldName = field != null ? field.toString() : "id";
Object o1 = field != null ? t1.getField(fieldName) : t1.getId();
Object o2 = field != null ? t2.getField(fieldName) : t2.getId();
if (o1 instanceof java.lang.Comparable) {
compareResult = ((Comparable) o1).compareTo(o2);
} else {
compareResult = Integer.valueOf(o1.hashCode()).compareTo(o2.hashCode());
}
int orderDirection = (orderCriterion.getOrder() == QueryOrder.ASC) ? 1 : -1;
compareResult = compareResult * orderDirection;
if (compareResult != 0) {
if (compareResult < 0) {
return -1;
} else {
return 1;
}
}
}
return 0;
}
});
return result;
}
public Set<Comment> findByContent(final String value) throws IOException {
return find(new HashMap<Enum, Object>(){{put(Comment._Fields.content, value);}});
}
public Set<Comment> findByCommenterId(final int value) throws IOException {
return find(new HashMap<Enum, Object>(){{put(Comment._Fields.commenter_id, value);}});
}
public Set<Comment> findByCommentedOnId(final long value) throws IOException {
return find(new HashMap<Enum, Object>(){{put(Comment._Fields.commented_on_id, value);}});
}
public Set<Comment> findByCreatedAt(final long value) throws IOException {
return find(new HashMap<Enum, Object>(){{put(Comment._Fields.created_at, value);}});
}
public CommentQueryBuilder query() {
return new CommentQueryBuilder(this);
}
}