com.jk.data.dataaccess.git.JKGitDataAccess Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jk-framework-data Show documentation
Show all versions of jk-framework-data Show documentation
This contains a set of API's that ease the database programming with Java, in both: JDBC and JPA Persisitnce).
/*
* Copyright 2002-2022 Dr. Jalal Kiswani.
* Email: [email protected]
* Check out https://smart-api.com for more details
*
* All the opensource projects of Dr. Jalal Kiswani are free for personal and academic use only,
* for commercial usage and support, please contact the author.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jk.data.dataaccess.git;
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;
// TODO: Auto-generated Javadoc
/**
* The Class JKGitDataAccess.
*/
public class JKGitDataAccess {
/** The Constant ID_FIELD_NAME. */
private static final String ID_FIELD_NAME = "id";
/** The gw. */
private static JKGitWrapper gw = new JKGitWrapper();
/** The git. */
private static Git git= gw.password(JKConfig.getDefaultInstance().getProperty("git-password-plain")).cloneRepo();
/**
* Insert.
*
* @param the generic type
* @param record the record
*/
/////////////////////////////////////////
public void insert(T record) {
List list = getList((Class) record.getClass());
list.add(record);
save((Class)record.getClass(), list);
}
/**
* Find.
*
* @param the generic type
* @param clas the clas
* @param id the id
* @return the t
*/
/////////////////////////////////////////
public T find(Class clas, Object id) {
List list = getList(clas);
for (T t : list) {
if (JKObjectUtil.getFieldValue(t, ID_FIELD_NAME).equals(id)) {
return t;
}
}
return null;
}
/**
* Update.
*
* @param the generic type
* @param record the record
*/
/////////////////////////////////////////
public void update(T record) {
List list = (List) getList(record.getClass());
for (int i=0;i)record.getClass(), list);
return;
}
}
}
/**
* Delete.
*
* @param the generic type
* @param record the record
*/
/////////////////////////////////////////
public void delete(T record) {
List list = (List) getList(record.getClass());
for (int i=0;i)record.getClass(), list);
return;
}
}
}
/**
* Gets the list.
*
* @param the generic type
* @param clas the clas
* @return the list
*/
/////////////////////////////////////////
public List getList(Class clas) {
String filePath = getFilePath(clas);
String fileContents = JKIOUtil.readFile(filePath);
if(fileContents==null) {
return new Vector<>();
}
List all = (List) JKObjectUtil.jsonToObjectList(fileContents, clas);
return all;
}
/**
* Save.
*
* @param the generic type
* @param clas the clas
* @param list the list
*/
/////////////////////////////////////////
protected void save(Class clas, List list) {
String contents = JKObjectUtil.toJson(list);
JKIOUtil.writeDataToFile(contents, getFilePath(clas));
gw.addCommitPush();
}
/**
* Gets the file path.
*
* @param the generic type
* @param clas the clas
* @return the file path
*/
/////////////////////////////////////////
protected String getFilePath(Class clas) {
String fileName = clas.getName() + ".json";
String filePath = gw.getLocalPath() + "/" + fileName;
return filePath;
}
}