-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from codeconsole/scaffold-annotations
@scaffold annotation for Controllers and Services
- Loading branch information
Showing
6 changed files
with
234 additions
and
12 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/groovy/grails/plugin/scaffolding/GormService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package grails.plugin.scaffolding | ||
|
||
import grails.artefact.Artefact | ||
import grails.gorm.api.GormAllOperations | ||
import grails.gorm.transactions.ReadOnly | ||
import grails.gorm.transactions.Transactional | ||
import grails.util.GrailsNameUtils | ||
import groovy.transform.CompileStatic | ||
import org.grails.datastore.gorm.GormEntity | ||
|
||
@Artefact("Service") | ||
@ReadOnly | ||
@CompileStatic | ||
class GormService<T extends GormEntity<T>> { | ||
|
||
GormAllOperations<T> resource | ||
String resourceName | ||
String resourceClassName | ||
boolean readOnly | ||
|
||
GormService(Class<T> resource, boolean readOnly) { | ||
this.resource = resource.getDeclaredConstructor().newInstance() as GormAllOperations<T> | ||
this.readOnly = readOnly | ||
resourceClassName = resource.simpleName | ||
resourceName = GrailsNameUtils.getPropertyName(resource) | ||
} | ||
|
||
protected T queryForResource(Serializable id) { | ||
resource.get(id) | ||
} | ||
|
||
T get(Serializable id) { | ||
queryForResource(id) | ||
} | ||
|
||
List<T> list(Map args) { | ||
resource.list(args) | ||
} | ||
|
||
Long count() { | ||
resource.count() | ||
} | ||
|
||
@Transactional | ||
void delete(Serializable id) { | ||
if (readOnly) { | ||
return | ||
} | ||
queryForResource(id).delete flush: true | ||
} | ||
|
||
@Transactional | ||
T save(T instance) { | ||
if (readOnly) { | ||
return instance | ||
} | ||
instance.save flush: true | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/groovy/grails/plugin/scaffolding/RestfulServiceController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package grails.plugin.scaffolding | ||
|
||
import grails.artefact.Artefact | ||
import grails.gorm.transactions.ReadOnly | ||
import grails.rest.RestfulController | ||
import grails.util.Holders | ||
|
||
@Artefact("Controller") | ||
@ReadOnly | ||
class RestfulServiceController<T> extends RestfulController<T> { | ||
|
||
RestfulServiceController(Class<T> resource, boolean readOnly) { | ||
super(resource, readOnly) | ||
} | ||
|
||
protected def getService() { | ||
Holders.grailsApplication.getMainContext().getBean(resourceName + 'Service') | ||
} | ||
|
||
protected T queryForResource(Serializable id) { | ||
getService().get(id) | ||
} | ||
|
||
protected List<T> listAllResources(Map params) { | ||
getService().list(params) | ||
} | ||
|
||
protected Integer countResources() { | ||
getService().count() | ||
} | ||
|
||
protected T saveResource(T resource) { | ||
getService().save(resource) | ||
} | ||
|
||
protected T updateResource(T resource) { | ||
getService().save(resource) | ||
} | ||
|
||
protected void deleteResource(T resource) { | ||
getService().delete(resource) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/groovy/grails/plugin/scaffolding/annotation/Scaffold.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package grails.plugin.scaffolding.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface Scaffold { | ||
Class<?> value() default Void.class; | ||
Class<?> domain() default Void.class; | ||
boolean readOnly() default false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/groovy/org/grails/compiler/scaffolding/ScaffoldingServiceInjector.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.grails.compiler.scaffolding | ||
|
||
import grails.compiler.ast.AstTransformer | ||
import grails.compiler.ast.GrailsArtefactClassInjector | ||
import grails.plugin.scaffolding.GormService | ||
import grails.plugin.scaffolding.annotation.Scaffold | ||
import groovy.transform.CompileStatic | ||
import org.codehaus.groovy.ast.ClassHelper | ||
import org.codehaus.groovy.ast.ClassNode | ||
import org.codehaus.groovy.ast.expr.ConstantExpression | ||
import org.codehaus.groovy.classgen.GeneratorContext | ||
import org.codehaus.groovy.control.SourceUnit | ||
import org.grails.compiler.injection.GrailsASTUtils | ||
import org.grails.core.artefact.ServiceArtefactHandler | ||
import org.grails.io.support.GrailsResourceUtils | ||
import org.grails.plugins.web.rest.transform.ResourceTransform | ||
|
||
import java.util.regex.Pattern | ||
|
||
/** | ||
* Transformation that turns a service into a scaffolding service at compile time if '@ScaffoldService' | ||
* is specified | ||
* | ||
* @author Scott Murphy Heiberg | ||
* @since 5.1 | ||
*/ | ||
@AstTransformer | ||
@CompileStatic | ||
class ScaffoldingServiceInjector implements GrailsArtefactClassInjector { | ||
|
||
final String[] artefactTypes = [ServiceArtefactHandler.TYPE] as String[] | ||
public static Pattern SERVICE_PATTERN = Pattern.compile(".+/" + | ||
GrailsResourceUtils.GRAILS_APP_DIR + "/services/(.+)Service\\.groovy"); | ||
|
||
@Override | ||
void performInjection(SourceUnit source, GeneratorContext context, ClassNode classNode) { | ||
performInjectionOnAnnotatedClass(source, classNode) | ||
} | ||
|
||
@Override | ||
void performInjection(SourceUnit source, ClassNode classNode) { | ||
performInjectionOnAnnotatedClass(source, classNode) | ||
} | ||
|
||
@Override | ||
void performInjectionOnAnnotatedClass(SourceUnit source, ClassNode classNode) { | ||
def annotationNode = classNode.getAnnotations(ClassHelper.make(Scaffold)).find() | ||
if (annotationNode) { | ||
ClassNode serviceClassNode = annotationNode?.getMember("value")?.type | ||
ClassNode superClassNode = ClassHelper.make(serviceClassNode?.getTypeClass()?:GormService).getPlainNodeReference() | ||
ClassNode currentSuperClass = classNode.getSuperClass() | ||
if (currentSuperClass.equals(GrailsASTUtils.OBJECT_CLASS_NODE)) { | ||
def domainClass = annotationNode.getMember("domain")?.type | ||
if (!domainClass) { | ||
domainClass = ScaffoldingControllerInjector.extractGenericDomainClass(serviceClassNode) | ||
} | ||
if (!domainClass) { | ||
GrailsASTUtils.error(source, classNode, "Scaffolded service (${classNode.name}) with @Scaffold does not have domain class set.", true) | ||
} | ||
classNode.setSuperClass(GrailsASTUtils.nonGeneric(superClassNode, domainClass)) | ||
def readOnlyExpression = (ConstantExpression) annotationNode.getMember("readOnly") | ||
new ResourceTransform().addConstructor(classNode, domainClass, readOnlyExpression?.getValue()?.asBoolean()?:false) | ||
} else if (!currentSuperClass.isDerivedFrom(superClassNode)) { | ||
GrailsASTUtils.error(source, classNode, "Scaffolded services (${classNode.name}) cannot extend other classes: ${currentSuperClass.getName()}", true) | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
boolean shouldInject(URL url) { | ||
return url != null && SERVICE_PATTERN.matcher(url.getFile()).find() | ||
} | ||
} |