Skip to content

Instantly share code, notes, and snippets.

@lincolnluiz
Last active September 3, 2017 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lincolnluiz/2d20620ca02f696ac582a1100c914d89 to your computer and use it in GitHub Desktop.
Save lincolnluiz/2d20620ca02f696ac582a1100c914d89 to your computer and use it in GitHub Desktop.
Simple java class to create the basic files of your application, within the structure of your architecture.
public class SimpleCreateCRUD {
private static final Charset charset = StandardCharsets.UTF_8;
private static final String ROOT = System.getProperty("user.dir")
+ "//src//main//java//";
private static final String MODELS_FOLDER = ROOT + "models//";
public static void main(String[] args) throws IOException {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("****************************************************************");
System.out.println("*********** Simple CRUD CLI ****************");
System.out.println("****************************************************************");
System.out.println();
System.out.println("Type the name your Entity/Model:");
String entity = sc.next();
for (EntityFileType fileType : getEntityFileTypeList()) {
System.out.println(String.format("*********** Creating file from the template: '%s'", fileType.getTemplate()));
Path path = Paths.get(MODELS_FOLDER + fileType.getTemplate());
String content = replaceTextInFile(path, entity);
String fileName = entity + fileType.getType();
Path pathResource = Paths.get(ROOT + fileType.getRelativePath() + fileName);
if (!createFile(pathResource, content))
continue;
System.out.println(String.format("*********** File '%s' created success", fileName));
System.out.println(String.format("*********** Location: '%s'", ROOT + fileType.getRelativePath() + fileName));
System.out.println();
}
}
public static String replaceTextInFile(Path pathTemplate, String entity) throws IOException {
String content = new String(Files.readAllBytes(pathTemplate), charset);
content = content.replaceAll(":ENTITY:", entity);
content = content.replaceAll(":ENTITY_LOWER:", entity.substring(0, 1).toLowerCase() + entity.substring(1));
return content;
}
public static boolean createFile(Path path, String content) throws IOException {
File file = new File(path.toString());
if (file.exists()) {
System.out.println(String.format("*********** ALERT! File '%s' alredy exists!", path));
System.out.println();
return false;
}
Files.write(path, content.getBytes(charset));
return true;
}
public static List<EntityFileType> getEntityFileTypeList() {
List<EntityFileType> list = new ArrayList<>();
list.add(new EntityFileType("model//", ".java", "Template"));
list.add(new EntityFileType("repository//", "Repository.java", "TemplateRepository"));
list.add(new EntityFileType("repository//", "RepositoryQuery.java", "TemplateRepositoryQuery"));
list.add(new EntityFileType("repository//impl//", "RepositoryImpl.java", "TemplateRepositoryImpl"));
list.add(new EntityFileType("resource//", "Resource.java", "TemplateResource"));
list.add(new EntityFileType("service//", "Service.java", "TemplateService"));
list.add(new EntityFileType("service//impl//", "ServiceImpl.java", "TemplateServiceImpl"));
return list;
}
static class EntityFileType {
private String relativePath; // -- //resource//
private String type; // -- Resource.java
private String template; // -- //TemplateResource
public EntityFileType(String relativePath, String type, String template) {
super();
this.relativePath = relativePath;
this.type = type;
this.template = template;
}
public String getRelativePath() {
return relativePath;
}
public String getType() {
return type;
}
public String getTemplate() {
return template;
}
}
}
@Entity
@Table(name = ":ENTITY_LOWER:")
public class :ENTITY: implements Serializable {
private static final long serialVersionUID = 1L;
private Long id:ENTITY:;
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "sequencial")
@TableGenerator(initialValue = 100, name = "sequencial", table = "sequencial", valueColumnName = "valor", pkColumnName = "nome", pkColumnValue = ":ENTITY_LOWER:", allocationSize = 1)
@Column(name = "id_:ENTITY_LOWER:")
public Long getId:ENTITY:() {
return id:ENTITY:;
}
public void setId:ENTITY:(Long id:ENTITY:) {
this.id:ENTITY: = id:ENTITY:;
}
}
public interface :ENTITY:Repository extends JpaRepository<:ENTITY:, Long>, :ENTITY:RepositoryQuery {
}
public class :ENTITY:RepositoryImpl extends AbstractRepository<:ENTITY:, :ENTITY:QueryFilter> implements :ENTITY:RepositoryQuery {
@Override
public Class<:ENTITY:> getEntityClass() {
return :ENTITY:.class;
}
@Override
public List<Predicate> getRestrictions(:ENTITY:QueryFilter filter, CriteriaBuilder builder, Root<:ENTITY:> root) {
return null;
}
}
public interface :ENTITY:RepositoryQuery extends Repository<:ENTITY:, :ENTITY:QueryFilter> {
}
@RestController
@RequestMapping("/api/:ENTITY_LOWER:")
public class :ENTITY:Resource extends AbstractResource<:ENTITY:, :ENTITY:QueryFilter> {
@Autowired
:ENTITY:Service :ENTITY_LOWER:Service;
@Override
public Service<:ENTITY:> getService() {
return :ENTITY_LOWER:Service;
}
}
public interface :ENTITY:Service extends Service<:ENTITY:> {
}
@Service
public class :ENTITY:ServiceImpl extends AbstractService<:ENTITY:> implements :ENTITY:Service {
@Autowired
private :ENTITY:Repository :ENTITY_LOWER:Repository;
@Override
public JpaRepository<:ENTITY:, Long> getRepository() {
return :ENTITY_LOWER:Repository;
}
}
@lincolnluiz
Copy link
Author

With this simple class you can create automatic files in your project structure, this files i used in Gist, represent a simple architecture of a project Spring Framework, however it is possible adapt in other project, as in a Android project or in a Angular aplication.

You only have to create the Template files, in template file you can use two variables :ENTITY: and :ENTITY_LOWER:, during process these variabels will be replace by your Entity/Model name.

variable :ENTITY_LOWER: will convert the name model with first letter to lower case.

To end, use method getEntityFileTypeList() to add yours Template files, the method has three parameters: relativePath, type and template:

  1. relativePath: location where will new file be save;
  2. type: name of file with extension;
  3. template: name of template file;

to create entity file only use the extension, for exemple .java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment