Home | Tutorials | Smart-Cloud | Smart-EIS | License | FAQ | Contact

Last Updated: March 12, 2021

The requirement of having Git Data Access is is limited to very few use-cases and not designed to be scalable. Use-cases includes configuration management and data that needs to be versioned and rollbacked, so be sure to know when to use it.

Pre-Requisite:

  1. Create a Git repository

  2. Have the repository url, branch, username,and password (or preferably access token) ready.

If you have enabled 2-factor authentication on your Git account, you need to create a Personal Access Token and use it as your password.

Steps:

  1. Create Maven project with the following pom.xml:

<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>
	<groupId>com.jalalkiswani</groupId>
	<version>4.0.4</version>
	<artifactId>smart-cloud-data-git-example</artifactId>
	<dependencies>
		<dependency>
			<groupId>com.jalalkiswani</groupId>
			<artifactId>smart-cloud-data</artifactId>
			<version>4.0.8</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

If you are using Eclipse, be sure to refresh your project (select your project→ right click→ Maven→ Update Project)

  1. Create config file at src/main/resources/config.properties with the following contents:

git-url=yourRepositoryUrl
git-username=yourRepositoryUserName
git-password=
git-password-plain=yourRepositoryPassword
git-branch=master
git.localpath=${env:user.home}/git-test
  1. Create Model with the following contents:

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();
	}
}
  1. Create DataAccess class with the following contents:

package com.app.person;

import java.util.*;

import org.eclipse.jgit.api.Git;

import com.jk.util.JKIOUtil;
import com.jk.util.JKObjectUtil;
import com.jk.util.activedirectory.JKGitWrapper;
import com.jk.util.config.JKConfig;

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(git);
	}

	/////////////////////////////////////////
	protected <T> String getFilePath(Class<T> clas) {
		String fileName = clas.getName() + ".json";
		String filePath = gw.getLocalPath() + "/" + fileName;
		return filePath;
	}

}
  1. Create Main class with the following contents:

package com.app;

import java.util.List;
import java.util.UUID;

import com.app.person.DataAccess;
import com.app.person.Model;
import com.jk.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);
	}


}

Thats it, now run your App class.

Full example source-code can be found at https://github.com/smartapi-com/smart-cloud-data-git


Home | Tutorials | Smart-Cloud | Smart-EIS | License | FAQ | Contact