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.
package play.test;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
import org.yaml.snakeyaml.introspector.BeanAccess;
import org.yaml.snakeyaml.scanner.ScannerException;
import play.Logger;
import play.Play;
import play.classloading.ApplicationClasses;
import play.data.binding.As;
import play.data.binding.Binder;
import play.data.binding.ParamNode;
import play.data.binding.RootParamNode;
import play.data.binding.types.DateBinder;
import play.db.DB;
import play.db.DBPlugin;
import play.db.Model;
import play.db.SQLSplitter;
import play.db.jpa.JPAPlugin;
import play.exceptions.DatabaseException;
import play.exceptions.UnexpectedException;
import play.exceptions.YAMLException;
import play.libs.IO;
import play.templates.TemplateLoader;
import play.vfs.VirtualFile;
import javax.persistence.Entity;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@As(Fixtures.PROFILE_NAME)
public class Fixtures {
/** Name of the profile use when loading fixture
* Allow to define the behavior when loading fixtures
*/
public static final String PROFILE_NAME = "Fixtures";
static Pattern keyPattern = Pattern.compile("([^(]+)\\(([^)]+)\\)");
// Allows people to clear the cache, so Fixture is not stateful
public static Map idCache = new HashMap();
public static void executeSQL(String sqlScript) {
for(CharSequence sql : new SQLSplitter(sqlScript)) {
final String s = sql.toString().trim();
if(s.length() > 0) {
DB.execute(s);
}
}
}
public static void executeSQL(File sqlScript) {
executeSQL(IO.readContentAsString(sqlScript));
}
/**
* Delete all Model instances for the given types using the underlying persistence mechanisms
* @param types Types to delete
*/
public static void delete(Class extends Model>... types) {
idCache.clear();
disableForeignKeyConstraints();
for (Class extends Model> type : types) {
try {
Model.Manager.factoryFor(type).deleteAll();
} catch(Exception e) {
Logger.error(e, "While deleting " + type + " instances");
}
}
enableForeignKeyConstraints();
Play.pluginCollection.afterFixtureLoad();
}
/**
* Delete all Model instances for the given types using the underlying persistence mechanisms
* @param classes Types to delete
*/
public static void delete(List> classes) {
@SuppressWarnings("unchecked")
Class extends Model>[] types = new Class[classes.size()];
for (int i = 0; i < types.length; i++) {
types[i] = classes.get(i);
}
delete(types);
}
/**
* Delete all Model instances for the all available types using the underlying persistence mechanisms
*/
@SuppressWarnings("unchecked")
public static void deleteAllModels() {
List> classes = new ArrayList>();
for (ApplicationClasses.ApplicationClass c : Play.classes.getAssignableClasses(Model.class)) {
if( c.javaClass.isAnnotationPresent(Entity.class) ) {
classes.add((Class extends Model>)c.javaClass);
}
}
disableForeignKeyConstraints();
Fixtures.delete(classes);
}
/**
* Use deleteDatabase() instead
* @deprecated use {@link #deleteDatabase()} instead
*/
@Deprecated
public static void deleteAll() {
deleteDatabase();
}
static String[] dontDeleteTheseTables = new String[] {"play_evolutions"};
/**
* Flush the entire JDBC database
*/
public static void deleteDatabase() {
try {
idCache.clear();
List names = new ArrayList();
ResultSet rs = DB.getConnection().getMetaData().getTables(null, null, null, new String[]{"TABLE"});
while (rs.next()) {
String name = rs.getString("TABLE_NAME");
names.add(name);
}
disableForeignKeyConstraints();
for (String name : names) {
if(Arrays.binarySearch(dontDeleteTheseTables, name) < 0) {
if (Logger.isTraceEnabled()) {
Logger.trace("Dropping content of table %s", name);
}
DB.execute(getDeleteTableStmt(name) + ";");
}
}
enableForeignKeyConstraints();
Play.pluginCollection.afterFixtureLoad();
} catch (Exception e) {
throw new RuntimeException("Cannot delete all table data : " + e.getMessage(), e);
}
}
/**
* @param name
* @deprecated use {@link #loadModels(String...)} instead
*/
@Deprecated
public static void load(String name) {
loadModels(name);
}
/**
* Load Model instances from a YAML file and persist them using the underlying persistence mechanism.
* The format of the YAML file is constrained, see the Fixtures manual page
* @param name Name of a YAML file somewhere in the classpath (or conf/)
*/
public static void loadModels(String name) {
loadModels(true, name);
}
/**
* Load Model instances from a YAML file and persist them using the underlying persistence mechanism.
* The format of the YAML file is constrained, see the Fixtures manual page
* @param name Name of a YAML file somewhere in the classpath (or conf/)
* @param loadAsTemplate : indicate if the file must interpreted as a Template
*/
public static void loadModels(boolean loadAsTemplate, String name) {
VirtualFile yamlFile = null;
try {
for (VirtualFile vf : Play.javaPath) {
yamlFile = vf.child(name);
// Check that the vf exist and isn't a directory
if (yamlFile != null && yamlFile.exists() && !yamlFile.isDirectory()) {
break;
}
}
// Check again the vf exist and isn't a directory
if (yamlFile == null || !yamlFile.exists() || yamlFile.isDirectory()) {
throw new RuntimeException("Cannot load fixture " + name + ", the file was not found");
}
String renderedYaml = null;
if(loadAsTemplate){
renderedYaml = TemplateLoader.load(yamlFile).render();
}else{
renderedYaml = yamlFile.contentAsString();
}
Yaml yaml = new Yaml();
Object o = yaml.load(renderedYaml);
if (o instanceof LinkedHashMap, ?>) {
Annotation[] annotations = Fixtures.class.getAnnotations();
@SuppressWarnings("unchecked") LinkedHashMap