All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.threewks.thundr.gae.objectify.ObjectifyModule Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * 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;

import com.google.appengine.api.datastore.GeoPt;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.impl.translate.opt.joda.JodaTimeTranslators;
import com.threewks.thundr.gae.objectify.transformers.DatastoreKeyToLongTransformer;
import com.threewks.thundr.gae.objectify.transformers.DatastoreKeyToStringTransformer;
import com.threewks.thundr.gae.objectify.transformers.KeyToLongTransformer;
import com.threewks.thundr.gae.objectify.transformers.KeyToStringTransformer;
import com.threewks.thundr.gae.objectify.transformers.StringToDatastoreKeyTransformer;
import com.threewks.thundr.gae.objectify.transformers.StringToKeyTransformer;
import com.threewks.thundr.injection.BaseModule;
import com.threewks.thundr.injection.UpdatableInjectionContext;
import com.threewks.thundr.module.DependencyRegistry;
import com.threewks.thundr.search.SearchModule;
import com.threewks.thundr.search.gae.meta.IndexType;
import com.threewks.thundr.search.gae.meta.IndexTypeLookup;
import com.threewks.thundr.transformer.TransformerManager;
import com.threewks.thundr.transformer.TransformerModule;

public class ObjectifyModule extends BaseModule {
	@Override
	public void requires(DependencyRegistry dependencyRegistry) {
		dependencyRegistry.addDependency(TransformerModule.class);
		dependencyRegistry.addDependency(SearchModule.class);
	}

	@Override
	public void initialise(UpdatableInjectionContext injectionContext) {
		ObjectifyFactory objectifyFactory = ObjectifyService.factory();
		runBasicConfiguration(objectifyFactory);
		injectionContext.inject(objectifyFactory).as(ObjectifyFactory.class);
	}

	@Override
	public void configure(UpdatableInjectionContext injectionContext) {
		TransformerManager transformerManager = injectionContext.get(TransformerManager.class);
		enhance(transformerManager);

		IndexTypeLookup indexTypeLookup = injectionContext.get(IndexTypeLookup.class);
		enhance(indexTypeLookup);
	}

	public static void runBasicConfiguration(ObjectifyFactory objectifyFactory) {
		// register Objectify converter to convert between java.uil.Date and org.joda.time.DateTime
		JodaTimeTranslators.add(objectifyFactory);
		ObjectifyService.factory().getTranslators().add(new UUIDTranslatorFactory());
	}

	public static TransformerManager enhance(TransformerManager transformerManager) {
		transformerManager.register(Key.class, String.class, new KeyToStringTransformer());
		transformerManager.register(Key.class, Long.class, new KeyToLongTransformer());
		transformerManager.register(String.class, Key.class, new StringToKeyTransformer());
		transformerManager.register(com.google.appengine.api.datastore.Key.class, String.class, new DatastoreKeyToStringTransformer());
		transformerManager.register(com.google.appengine.api.datastore.Key.class, Long.class, new DatastoreKeyToLongTransformer());
		transformerManager.register(String.class, com.google.appengine.api.datastore.Key.class, new StringToDatastoreKeyTransformer());
		return transformerManager;
	}

	public static IndexTypeLookup enhance(IndexTypeLookup indexTypeLookup) {
		indexTypeLookup.addMapping(Key.class, IndexType.Identifier);
		indexTypeLookup.addMapping(com.google.appengine.api.datastore.Key.class, IndexType.Identifier);
		indexTypeLookup.addMapping(GeoPt.class, IndexType.GeoPoint);
		return indexTypeLookup;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy