Git Data Access Example
This example demonstrates the usage of the j-framework to create a project which communicates with a Git repository as persistence layer.
Prerequisites:
-
You should have JDK 17+ Installed. (Click Here).
-
You should have a Git Repository created. (Click Here).
-
You should create a Maven Project.
-
You should enable snapshot versions. (Click Here).
Project Content:
This section contains what you should add to your code structure for this example.
-
Maven Project with pom.xml that has the following contents:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jalalkiswani</groupId>
<artifactId>j-app</artifactId>
<version>7.0.0-SNAPSHOT</version>
</parent>
<artifactId>j-framework-data-git-example</artifactId>
</project>
-
Repository configurations file located at
src/main/resources/config.properties
that has the following content, make sure to replace this with your repository information:
git-url=yourRepositoryUrl
git-username=yourRepositoryUserName
git-password=
git-password-plain=yourRepositoryPassword
git-branch=master
git.localpath=${env:user.home}/git-test
-
Java Model class located at
src/main/app/person/Model.java
that has the following content:
package com.app.person;
import java.io.Serializable;
public class Model implements Serializable {
private String id;
private String nationalId;
private String name;
private String email;
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setNationalId(String nationalId){
this.nationalId=nationalId;
}
public String getNationalId(){
return this.nationalId;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setEmail(String email){
this.email=email;
}
public String getEmail(){
return this.email;
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return this.address;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
return this.getId().equals(((Model) obj).getId());
}
@Override
public int hashCode() {
if(this.id==null) {
return toString().hashCode();
}
return this.id.hashCode();
}
@Override
public String toString(){
StringBuffer buf=new StringBuffer();
buf.append(this.id).append(",");
buf.append(this.nationalId).append(",");
buf.append(this.name).append(",");
buf.append(this.email).append(",");
buf.append(this.address).append(",");
return buf.toString();
}
}
-
Data Access class located at
src/main/app/person/DataAccess.java
which has the following content:
package com.app.person;
import java.util.List;
import java.util.Vector;
import org.eclipse.jgit.api.Git;
import com.jk.core.config.JKConfig;
import com.jk.core.scm.JKGitWrapper;
import com.jk.core.util.JKIOUtil;
import com.jk.core.util.JKObjectUtil;
public class DataAccess {
private static final String ID_FIELD_NAME = "id";
private static JKGitWrapper gw = new JKGitWrapper();
private static Git git= gw.password(JKConfig.getDefaultInstance().getProperty("git-password-plain")).cloneRepo();
public <T> void insert(T record) {
List<T> list = getList((Class<T>) record.getClass());
list.add(record);
save((Class<T>)record.getClass(), list);
}
public <T> T find(Class<T> clas, Object id) {
List<T> list = getList(clas);
for (T t : list) {
if (JKObjectUtil.getFieldValue(t, ID_FIELD_NAME).equals(id)) {
return t;
}
}
return null;
}
public <T> void update(T record) {
List<T> list = (List<T>) getList(record.getClass());
for (int i=0;i<list.size();i++) {
T t = list.get(i);
Object sourceId = JKObjectUtil.getFieldValue(record, ID_FIELD_NAME);
Object currentId = JKObjectUtil.getFieldValue(t, ID_FIELD_NAME);
if (currentId.equals(sourceId)) {
list.set(i, record);
save((Class<T>)record.getClass(), list);
return;
}
}
}
public <T> void delete(T record) {
List<T> list = (List<T>) getList(record.getClass());
for (int i=0;i<list.size();i++) {
T t = list.get(i);
Object sourceId = JKObjectUtil.getFieldValue(record, ID_FIELD_NAME);
Object currentId = JKObjectUtil.getFieldValue(t, ID_FIELD_NAME);
if (currentId.equals(sourceId)) {
list.remove(i);
save((Class<T>)record.getClass(), list);
return;
}
}
}
public <T> List<T> getList(Class<T> clas) {
String filePath = getFilePath(clas);
String fileContents = JKIOUtil.readFile(filePath);
if(fileContents==null) {
return new Vector<>();
}
List<T> all = (List<T>) JKObjectUtil.jsonToObjectList(fileContents, clas);
return all;
}
protected <T> void save(Class<T> clas, List<T> list) {
String contents = JKObjectUtil.toJson(list);
JKIOUtil.writeDataToFile(contents, getFilePath(clas));
gw.addCommitPush();
}
protected <T> String getFilePath(Class<T> clas) {
String fileName = clas.getName() + ".json";
String filePath = gw.getLocalPath() + "/" + fileName;
return filePath;
}
}
-
Main java class located at
src/main/java/com/app/App.java
which contains the following:
package com.app;
import java.util.List;
import java.util.UUID;
import com.app.person.DataAccess;
import com.app.person.Model;
import com.jk.core.util.JK;
public class App {
private static String id;
public static void main(String[] args) {
DataAccess da = new DataAccess();
add(da);
find(da);
update(da);
getModelList(da);
delete(da);
}
public static void add(DataAccess da) {
Model model=new Model();
id = UUID.randomUUID().toString();
model.setId(id);
da.insert(model);
}
public static void update(DataAccess da) {
Model model = find(da);
da.update(model);
}
public static Model find(DataAccess da) {
Model model = da.find(Model.class, id);
return model;
}
public static void delete(DataAccess da) {
da.delete(find(da));
}
public static void getModelList(DataAccess da) {
List<Model> modelList = da.getList(Model.class);
JK.print("List", modelList);
}
}
Alternatively, you can clone or download the tutorial repository then import the project into your IDE. |
How to run Project:
-
Set up the project with the content shown above.
-
Inside your IDE, go to the
src/main/resources/config.properties
file.
-
Change the repository properties to describe your repository:
-
Then, go to the
src/main/java/com/app/App.java
class.
-
Next, run it as a Java Application.
-
Your program will start running and the output result will show in the console.