Skip to content

Commit

Permalink
1.3.7a: fix #117
Browse files Browse the repository at this point in the history
  • Loading branch information
greenlaw110 committed Nov 16, 2013
1 parent 7d0f971 commit 4533783
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 369 deletions.
4 changes: 3 additions & 1 deletion conf/dependencies.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# History
# 1.3.7a - https://github.com/greenlaw110/play-morphia/issues/117
# 1.3.7 - https://github.com/greenlaw110/play-morphia/issues/115
# 1.3.6b - Fix https://github.com/greenlaw110/play-morphia/issues/114
# 1.3.5 - Merge pull requests: https://github.com/greenlaw110/play-morphia/pull/113
# 1. NullPointerException when creating a Model which has an @Embeded entity
Expand Down Expand Up @@ -38,6 +40,6 @@
# - trim value when processing where statement in Factory.fetch
# 1.2.5 - mavenize project
# 1.2.4d - Add BlobGsonAdapter and ModelFactoryGsonAdapter utilities
self: play -> morphia 1.3.6b
self: play -> morphia 1.3.7a
require:
- play 1.2
35 changes: 35 additions & 0 deletions documentation/manual/history.textile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@ h1. PlayMorphia version history
h2. <a name="roadmap">TODO list</a>

* MongoDB multi-tenant support
# 1.3.7a - https://github.com/greenlaw110/play-morphia/issues/117
# 1.3.7 - https://github.com/greenlaw110/play-morphia/issues/115
# 1.3.6b - https://github.com/greenlaw110/play-morphia/issues/114

h2. <a name="1.3.7a">Updates in 1.3.7a</a>

Bug fixed:

* "Hide generated fields for CRUD":https://github.com/greenlaw110/play-morphia/issues/115
* "File name not correctly retrieved when using fs(File system) as storage service":https://github.com/greenlaw110/play-morphia/issues/117

h2. <a name="1.3.6b">Updates in 1.3.6b</a>

Bug fixed:

* "CRUD add model generate empty String as _id":https://github.com/greenlaw110/play-morphia/issues/114

h2. <a name="1.3.5">Updates in 1.3.5</a>

Merge pull requests: https://github.com/greenlaw110/play-morphia/pull/113

* NullPointerException when creating a Model which has an @Embeded entity
* Added Play! custom class loader to SnakeYAML

h2. <a name="1.3.4a">Updates in 1.3.4a</a>

Add storage migrate controller and viewers

h2. <a name="1.3.3">Updates in 1.3.3</a>

Migrate even earlier blob version which name is composed by modelClass_fieldName_oid

h2. <a name="1.3.2">Updates in 1.3.2</a>

Add blob migrate controller action method

h2. <a name="1.3.1">Updates in 1.3.1</a>

Expand Down
361 changes: 0 additions & 361 deletions documentation/manual/home - Copy.textile

This file was deleted.

2 changes: 1 addition & 1 deletion documentation/manual/home.textile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
h1. PlayMorphia module Documentation

Welcome to PlayMorphia module documentation. This documentation is intended for the *1.3.1 release* and may significantly differ from previous module version's documentation.
Welcome to PlayMorphia module documentation. This documentation is intended for the *1.3.7a release* and may significantly differ from previous module version's documentation.

Check the "version history":history

Expand Down
9 changes: 7 additions & 2 deletions src/play/modules/morphia/Blob.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package play.modules.morphia;

import org.bson.types.ObjectId;
import org.osgl.exception.UnexpectedIOException;
import org.osgl.storage.ISObject;
import org.osgl.storage.IStorageService;
Expand All @@ -8,10 +9,10 @@
import org.osgl.util.E;
import org.osgl.util.S;
import org.osgl.util._;
import org.bson.types.ObjectId;
import play.cache.Cache;
import play.db.Model.BinaryField;
import play.libs.F;
import play.modules.morphia.utils.MimeTypes;
import play.mvc.Router;

import java.io.File;
Expand Down Expand Up @@ -115,13 +116,17 @@ public Blob(InputStream is, String type) {

public Blob(File inputFile, String type) {
sobj = SObject.valueOf(NULL_KEY, inputFile);
if (S.empty(type)) {
type = MimeTypes.probe(inputFile);
}
if (S.notEmpty(type)) {
type(type);
}
sobj.setAttribute(FILENAME, inputFile.getName());
}

public Blob(File inputFile) {
sobj = SObject.valueOf(NULL_KEY, inputFile);
this(inputFile, null);
}

public Blob(ISObject sobj, BlobStorageService ss) {
Expand Down
27 changes: 27 additions & 0 deletions src/play/modules/morphia/CRUDFieldEnhancer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package play.modules.morphia;

import javassist.CtClass;
import javassist.CtField;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.annotation.Annotation;

/**
* Created with IntelliJ IDEA.
* User: luog
* Date: 16/10/13
* Time: 9:07 PM
* To change this template use File | Settings | File Templates.
*/
public class CRUDFieldEnhancer implements MorphiaEnhancer.FieldEnhancer {
@Override
public void enhance(CtField field, CtClass clz) {
AnnotationsAttribute aa = (AnnotationsAttribute)field.getFieldInfo().getAttribute(AnnotationsAttribute.visibleTag);
if (null == aa) {
aa = new AnnotationsAttribute(clz.getClassFile().getConstPool(),
AnnotationsAttribute.visibleTag);
field.getFieldInfo().addAttribute(aa);
}
Annotation ann = new Annotation("controllers.CRUD$Hidden", clz.getClassFile().getConstPool());
aa.addAnnotation(ann);
}
}
22 changes: 20 additions & 2 deletions src/play/modules/morphia/MorphiaEnhancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.google.code.morphia.annotations.*;
import com.google.code.morphia.utils.IndexDirection;
import org.osgl.storage.KeyGenerator;
import org.osgl.util.*;
import javassist.*;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
Expand All @@ -12,6 +10,8 @@
import javassist.bytecode.annotation.ArrayMemberValue;
import javassist.bytecode.annotation.EnumMemberValue;
import javassist.bytecode.annotation.MemberValue;
import org.osgl.storage.KeyGenerator;
import org.osgl.util.*;
import play.Logger;
import play.classloading.ApplicationClasses.ApplicationClass;
import play.classloading.enhancers.Enhancer;
Expand All @@ -30,8 +30,23 @@
*/
public class MorphiaEnhancer extends Enhancer {

public static interface FieldEnhancer {
void enhance(CtField field, CtClass clz);
}

public static final String PACKAGE_NAME = "play.modules.morphia";

private static FieldEnhancer crud = null;

private static void enhanceCRUDField(CtField fld, CtClass clz) {
if (MorphiaPlugin.crud) {
if (null == crud) {
crud = new CRUDFieldEnhancer();
}
crud.enhance(fld, clz);
}
}

@Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
//Logger.debug("Morphia> start to enhance class:" + applicationClass.name);
Expand Down Expand Up @@ -177,6 +192,7 @@ private void enhance_(CtClass ctClass, ApplicationClass applicationClass, boolea
AnnotationsAttribute.visibleTag);
Annotation idAnn = new Annotation(Id.class.getName(), ctClass.getClassFile().getConstPool());
aa.addAnnotation(idAnn);
enhanceCRUDField(idField, ctClass);
idField.getFieldInfo().addAttribute(aa);
ctClass.addField(idField);
Logger.trace("ID field added to entity[%2$s]: %1$s", idField.getSignature(), ctClass.getName());
Expand Down Expand Up @@ -223,11 +239,13 @@ private void enhance_(CtClass ctClass, ApplicationClass applicationClass, boolea
CtField createdField = new CtField(CtClass.longType, "_created", ctClass);
createdField.getFieldInfo().addAttribute(attribute);
createdField.setModifiers(Modifier.PUBLIC);
enhanceCRUDField(createdField, ctClass);
ctClass.addField(createdField);

CtField modifiedField = new CtField(CtClass.longType, "_modified", ctClass);
modifiedField.getFieldInfo().addAttribute(attribute);
modifiedField.setModifiers(Modifier.PUBLIC);
enhanceCRUDField(modifiedField, ctClass);
ctClass.addField(modifiedField);

CtMethod persistTs = CtMethod.make("void _updateTimestamp() { long now = System.currentTimeMillis(); if (0 == _created) {_created = now;} ;_modified = now;}", ctClass);
Expand Down
27 changes: 25 additions & 2 deletions src/play/modules/morphia/MorphiaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
*/
public final class MorphiaPlugin extends PlayPlugin {

public static final String VERSION = "1.3.6b";
public static final String VERSION = "1.3.7";

public static void info(String msg, Object... args) {
Logger.info(msg_(msg, args));
Expand Down Expand Up @@ -216,6 +216,7 @@ public void enhance(ApplicationClass applicationClass) throws Exception {
//onConfigurationRead(); // ensure configuration be read before
// enhancement
initIdType_();
initCrud_();
e_.enhanceThisClass(applicationClass);
}

Expand All @@ -225,7 +226,6 @@ public void enhance(ApplicationClass applicationClass) throws Exception {
public static synchronized void registerGlobalEventHandler(IMorphiaEventHandler handler) {
if (null == handler) throw new NullPointerException();
if (!globalEventHandlers_.contains(handler)) globalEventHandlers_.add(handler);

}

public static synchronized void unregisterGlobalEventHandler(IMorphiaEventHandler handler) {
Expand Down Expand Up @@ -405,6 +405,29 @@ public static Map<String, String> getDefaultStorageConf() {
return ssConfs.get(defaultStorage);
}

public static Boolean crud = null;

private void initCrud_() {
if (null != crud) {
return;
}
if (!Boolean.parseBoolean(Play.configuration.getProperty("morphia.crud", "false"))) {
crud = false;
return;
}
try {
//Class.forName("controllers.CRUD");
crud = true;
} catch (Exception e) {
throw new ConfigurationException("Cannot find CRUD class. Please make sure CRUD module is enabled for your application; or disable 'morphia.crud' option");
}
}

@Override
public void onLoad() {
initCrud_();
}

@Override
public void onConfigurationRead() {
if (configured_)
Expand Down
74 changes: 74 additions & 0 deletions src/play/modules/morphia/utils/MimeTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package play.modules.morphia.utils;

import org.osgl.util.S;

import java.io.File;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

/**
* Created with IntelliJ IDEA.
* User: luog
* Date: 16/11/13
* Time: 8:33 PM
* To change this template use File | Settings | File Templates.
*/
public enum MimeTypes {
_;

private Map<String, String> m = new HashMap<String, String>();

private MimeTypes() {
p(".doc:application/msword");
p(".dot:application/msword");
p(".docx:application/vnd.openxmlformats-officedocument.wordprocessingml.document");
p(".dotx:application/vnd.openxmlformats-officedocument.wordprocessingml.template");
p(".docm:application/vnd.ms-word.document.macroEnabled.12");
p(".dotm:application/vnd.ms-word.template.macroEnabled.12");
p(".ppt:application/vnd.ms-powerpoint");
p(".pot:application/vnd.ms-powerpoint");
p(".pps:application/vnd.ms-powerpoint");
p(".ppa:application/vnd.ms-powerpoint");
p(".potx:application/vnd.openxmlformats-officedocument.presentationml.template");
p(".ppsx:application/vnd.openxmlformats-officedocument.presentationml.slideshow");
p(".pptx:application/vnd.openxmlformats-officedocument.presentationml.presentation");
p(".sldx:application/vnd.openxmlformats-officedocument.presentationml.slide");
p(".ppam:application/vnd.ms-powerpoint.addin.macroEnabled.12");
p(".pptm:application/vnd.ms-powerpoint.presentation.macroEnabled.12");
p(".potm:application/vnd.ms-powerpoint.template.macroEnabled.12");
p(".ppsm:application/vnd.ms-powerpoint.slideshow.macroEnabled.12");
p(".xla:application/vnd.ms-excel");
p(".xls:application/vnd.ms-excel");
p(".xlt:application/vnd.ms-excel");
p(".xlam:application/vnd.ms-excel.addin.macroEnabled.12");
p(".xlsb:application/vnd.ms-excel.sheet.binary.macroEnabled.12");
p(".xlsm:application/vnd.ms-excel.sheet.macroEnabled.12");
p(".xltm:application/vnd.ms-excel.template.macroEnabled.12");
p(".xlsx:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
p(".xltx:application/vnd.openxmlformats-officedocument.spreadsheetml.template");
}

private void p(String s) {
String[] sa = s.split(":");
m.put(sa[0], sa[1]);
}

private String get(String k) {
return m.get(k);
}

public static String probe(File file) {
return probe(file.getName());
}

public static String probe(String fileName) {
// try JDK util first
String type = URLConnection.guessContentTypeFromName(fileName);
if (null == type) {
String suffix = "." + S.str(fileName).afterLast(".").get();
type = _.get(suffix);
}
return type;
}
}

0 comments on commit 4533783

Please sign in to comment.