Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* This file is a component of thundr, a software library from 3wks.
* Read more: http://3wks.github.io/thundr/
* Copyright (C) 2015 3wks,
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.threewks.thundr.gae.objectify.repository;
import static com.googlecode.objectify.ObjectifyService.ofy;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.reflect.FieldUtils;
import com.atomicleopard.expressive.EList;
import com.atomicleopard.expressive.ETransformer;
import com.atomicleopard.expressive.Expressive;
import com.atomicleopard.expressive.transform.CollectionTransformer;
import com.google.appengine.api.search.ScoredDocument;
import com.google.common.collect.Lists;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Result;
import com.threewks.thundr.exception.BaseException;
import com.threewks.thundr.logger.Logger;
import com.threewks.thundr.search.IndexOperation;
import com.threewks.thundr.search.Search;
import com.threewks.thundr.search.SearchException;
import com.threewks.thundr.search.gae.IdGaeSearchService;
import com.threewks.thundr.search.gae.SearchConfig;
import com.threewks.thundr.search.gae.SearchExecutor;
public abstract class AbstractRepository implements AsyncRepository {
protected IdGaeSearchService> searchService;
protected Class entityType;
protected Field idField;
protected boolean isSearchable;
protected ETransformer, Map, E>> toKeyLookup;
protected ETransformer> toOfyKey;
protected CollectionTransformer> toOfyKeys;
protected ETransformer toId;
protected CollectionTransformer toIds;
protected ETransformer> toKey;
protected CollectionTransformer> toKeys;
protected ETransformer> toKeyFromEntity;
protected CollectionTransformer> toKeysFromEntities;
protected ETransformer, K> fromKey;
protected CollectionTransformer, K> fromKeys;
public AbstractRepository(Class entityType, ETransformer> toKey, ETransformer, K> fromKey, SearchConfig searchConfig) {
this.entityType = entityType;
this.searchService = createIdGaeSearchService(searchConfig);
this.isSearchable = searchService != null && searchService.hasIndexableFields();
this.idField = idField(entityType);
this.toKey = toKey;
this.toKeys = new CollectionTransformer>(toKey);
this.fromKey = fromKey;
this.fromKeys = new CollectionTransformer, K>(fromKey);
this.toId = new ETransformer() {
@Override
public Object from(E from) {
try {
return idField.get(from);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RepositoryException(e, "Unable to access '%s.%s' - cannot extract an id: %s", AbstractRepository.this.entityType.getSimpleName(), idField.getName(), e.getMessage());
}
}
};
this.toIds = Expressive.Transformers.transformAllUsing(toId);
this.toKeyLookup = new ETransformer, Map, E>>() {
@Override
public Map, E> from(Iterable from) {
Map, E> lookup = new LinkedHashMap, E>();
for (E e : from) {
lookup.put(key(e), e);
}
return lookup;
}
};
this.toKeyFromEntity = new ETransformer>() {
@Override
public Key from(E from) {
return Key.create(from);
}
};
this.toKeysFromEntities = Expressive.Transformers.transformAllUsing(toKeyFromEntity);
}
@Override
public E put(E entity) {
return putAsync(entity).complete();
}
@Override
public AsyncResult putAsync(final E entity) {
boolean hasId = hasId(entity);
final Result> ofyFuture = ofy().save().entity(entity);
if (!hasId) {
// if no id exists - we need objectify to complete so that the id can be used in indexing the record.
ofyFuture.now();
}
final IndexOperation searchFuture = shouldSearch() ? index(entity) : null;
return new AsyncResult() {
@Override
public E complete() {
ofyFuture.now();
if (searchFuture != null) {
searchFuture.complete();
}
return entity;
}
};
}
@Override
public List put(@SuppressWarnings("unchecked") E... entities) {
return putAsync(entities).complete();
}
@Override
@SuppressWarnings("unchecked")
public AsyncResult> putAsync(E... entities) {
return putAsync(Arrays.asList(entities));
}
@Override
public List put(List entities) {
return putAsync(entities).complete();
}
@Override
public AsyncResult> putAsync(final List entities) {
List