Skip to content

Commit

Permalink
Merge pull request #6591 from thurka/GCN-3261
Browse files Browse the repository at this point in the history
Micronaut Template for Data repository - add support for Pageable
  • Loading branch information
thurka authored Oct 24, 2023
2 parents a5c96b7 + 0c437f1 commit 4c6ab9a
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.NotifyDescriptor.QuickPick;
import org.openide.NotifyDescriptor.QuickPick.Item;
import org.openide.WizardDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
Expand All @@ -90,7 +92,12 @@ public static TemplateWizard.Iterator create() {

@NbBundle.Messages({
"MSG_SelectEntities=Select Entity Classes",
"MSG_NoEntities=No entity class found in {0}"
"MSG_NoEntities=No entity class found in {0}",
"MSG_Repository_Type=Select Repository Type",
"MSG_CrudRepository=CRUD Repository",
"DESC_CrudRepository=CrudRepository enables automatic generation of CRUD operations.",
"MSG_PageableRepository=Pageable Repository",
"DESC_PageableRepository=PageableRepository extends CrudRepository and adds methods for pagination."
})
public static CreateFromTemplateHandler handler() {
return new CreateFromTemplateHandler() {
Expand Down Expand Up @@ -132,6 +139,14 @@ protected List<FileObject> createFromTemplate(CreateDescriptor desc) throws IOEx
DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message(Bundle.MSG_NoEntities(sourceGroup.getRootFolder().getPath()), NotifyDescriptor.ERROR_MESSAGE));
return Collections.emptyList();
}
List<Item> repoType = new ArrayList<>();
Item pageableItem = new Item(Bundle.MSG_PageableRepository(), null /*Bundle.DESC_PageableRepository()*/);
repoType.add(new Item(Bundle.MSG_CrudRepository(), null /*Bundle.DESC_CrudRepository()*/));
repoType.add(pageableItem);
QuickPick qpt = new QuickPick(Bundle.MSG_Repository_Type(), Bundle.MSG_Repository_Type(), repoType, false);
if (DialogDescriptor.OK_OPTION != DialogDisplayer.getDefault().notify(qpt)) {
return Collections.emptyList();
}
NotifyDescriptor.QuickPick qp = new NotifyDescriptor.QuickPick(Bundle.MSG_SelectEntities(), Bundle.MSG_SelectEntities(), entities, true);
if (DialogDescriptor.OK_OPTION == DialogDisplayer.getDefault().notify(qp)) {
String dialect = getDialect(jpaSupported);
Expand All @@ -140,7 +155,7 @@ protected List<FileObject> createFromTemplate(CreateDescriptor desc) throws IOEx
if (item.isSelected()) {
String fqn = item.getDescription() != null ? item.getDescription() + '.' + item.getLabel() : item.getLabel();
String entityIdType = entity2idTypes.get(fqn);
FileObject fo = generate(folder, item.getLabel(), fqn, entityIdType, dialect);
FileObject fo = generate(folder, item.getLabel(), fqn, entityIdType, dialect, pageableItem.isSelected());
if (fo != null) {
generated.add(fo);
}
Expand Down Expand Up @@ -172,7 +187,7 @@ public Set<DataObject> instantiate(TemplateWizard wiz) throws IOException {
String fqn = entry.getKey();
int idx = fqn.lastIndexOf('.');
String label = idx < 0 ? fqn : fqn.substring(idx + 1);
FileObject fo = generate(targetFolder, label, fqn, entry.getValue(), dialect);
FileObject fo = generate(targetFolder, label, fqn, entry.getValue(), dialect, false);
if (fo != null) {
generated.add(DataObject.find(fo));
}
Expand Down Expand Up @@ -324,10 +339,13 @@ private static String getDialect(boolean jpaSupported) {
return null;
}

private static final String CRUD_REPOSITORY = "io.micronaut.data.repository.CrudRepository"; // NOI18N
private static final String PAGEABLE_REPOSITORY = "io.micronaut.data.repository.PageableRepository"; // NOI18N

@NbBundle.Messages({
"MSG_Repository_Interface=Repository interface {0}\n"
})
private static FileObject generate(FileObject folder, String entityName, String entityFQN, String entityIdType, String dialect) {
private static FileObject generate(FileObject folder, String entityName, String entityFQN, String entityIdType, String dialect, boolean pageable) {
try {
String name = entityName + "Repository"; // NOI18N
FileObject fo = GenerationUtils.createInterface(folder, name, Bundle.MSG_Repository_Interface(name));
Expand All @@ -342,7 +360,8 @@ private static FileObject generate(FileObject folder, String entityName, String
TreeMaker tm = copy.getTreeMaker();
TypeMirror entityIdTM = copy.getTreeUtilities().parseType(entityIdType, (TypeElement) copy.getTrees().getElement(new TreePath(new TreePath(copy.getCompilationUnit()), origTree)));
List<ExpressionTree> args = Arrays.asList(tm.QualIdent(entityFQN), entityIdTM != null && entityIdTM.getKind().isPrimitive() ? tm.QualIdent(copy.getTypes().boxedClass((PrimitiveType) entityIdTM)) : tm.QualIdent(entityIdType));
ParameterizedTypeTree type = tm.ParameterizedType(tm.QualIdent("io.micronaut.data.repository.CrudRepository"), args); //NOI18N
String repoFaq = pageable ? PAGEABLE_REPOSITORY : CRUD_REPOSITORY;
ParameterizedTypeTree type = tm.ParameterizedType(tm.QualIdent(repoFaq), args);
ClassTree cls = tm.addClassImplementsClause((ClassTree) origTree, type);
if (dialect == null) {
cls = gu.addAnnotation(cls, gu.createAnnotation("io.micronaut.data.annotation.Repository")); //NOI18N
Expand All @@ -352,10 +371,6 @@ private static FileObject generate(FileObject folder, String entityName, String
List<ExpressionTree> annArgs = Collections.singletonList(gu.createAnnotationArgument("dialect", "io.micronaut.data.model.query.builder.sql.Dialect", dialect)); //NOI18N
cls = gu.addAnnotation(cls, gu.createAnnotation("io.micronaut.data.jdbc.annotation.JdbcRepository", annArgs)); //NOI18N
}
ModifiersTree mods = tm.Modifiers(Collections.emptySet(), Arrays.asList(gu.createAnnotation("java.lang.Override"), gu.createAnnotation("io.micronaut.core.annotation.NonNull"))); //NOI18N
ParameterizedTypeTree retType = tm.ParameterizedType(tm.QualIdent("java.util.List"), Collections.singletonList(tm.QualIdent(entityFQN))); //NOI18N
MethodTree findAllMethod = tm.Method(mods, "findAll", retType, Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), (BlockTree)null, null); //NOI18N
cls = tm.addClassMember(cls, findAllMethod);
copy.rewrite(origTree, cls);
}
}).commit();
Expand Down

0 comments on commit 4c6ab9a

Please sign in to comment.