crash.commands.base.jpa.groovy Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2012 eXo Platform SAS.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package crash.commands.base
import org.crsh.cli.Usage
import org.crsh.cli.Command
import org.crsh.command.InvocationContext
import org.crsh.util.Utils
import javax.naming.InitialContext
import org.crsh.cli.Argument
import org.crsh.text.renderers.EntityTypeRenderer
import org.crsh.cli.spi.Completer
import org.crsh.cli.spi.Completion
import org.crsh.cli.descriptor.ParameterDescriptor
import org.crsh.util.JNDIHandler
/**
* @author Alain Defrance
*/
@Usage("Java persistance API")
class jpa implements Completer {
@Usage("Open a JPA session")
@Command
Object open(@Argument(completer = jpa.EmfCompleter.class) String jndiName) {
if (em != null) {
throw new ScriptException("Already connected");
}
if (jndiName == null) {
throw new ScriptException("JNDI resource is required");
}
InitialContext ic = new InitialContext();
def emf = ic.lookup(jndiName);
if (emf == null) {
throw new ScriptException("${jndiName} doesn't exist");
}
em = emf.createEntityManager();
return "Using $jndiName entity manager factory\n";
}
@Usage("Close the current JPA session")
@Command
Object close() {
if (em == null) {
throw new ScriptException("Not connected");
} else {
em = null;
return "Connection closed\n";
}
}
@Usage("List JPA entities")
@Command
void entities(InvocationContext context) {
em.metamodel.entities.each { e ->
context.provide(new EntityTypeRenderer.EntityTypeData(e.name, e.javaType.name, e.persistenceType.toString()));
}
}
@Usage("Display JPA entity")
@Command
void entity(InvocationContext context, @Argument String name) {
def en;
em.metamodel.entities.each { e ->
if (e.name.equals(name)) {
en = e;
}
}
if (en == null) {
throw new ScriptException("${name} is not an entity");
}
def etd = new EntityTypeRenderer.EntityTypeData(en.name, en.javaType.name, en.persistenceType.toString(), true)
en.attributes.each { a ->
etd.add(new EntityTypeRenderer.AttributeData(a.name, a.javaType.name, a.association, a.collection, a.persistentAttributeType.toString()));
}
context.provide(etd);
}
@Usage("Execute select JPA query")
@Command
void select(InvocationContext
© 2015 - 2024 Weber Informatics LLC | Privacy Policy