Update the project for ORM Framework

This commit is contained in:
YuCheng Hu 2022-08-12 08:05:07 -04:00
parent 0785c630fc
commit 458e30c94a
9 changed files with 504 additions and 13 deletions

View File

@ -7,6 +7,7 @@
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="common" />
<module name="orm-factory-common" />
</profile>
</annotationProcessing>
</component>

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

6
.idea/thriftCompiler.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ThriftCompiler">
<compilers />
</component>
</project>

View File

@ -0,0 +1,40 @@
package com.ossez.framework.common.tasks;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang3.StringUtils;
public class Task1 {
public static int getClosestToZero(int[] a) {
/*
Please implement this method to
return the number in the array that is closest to zero.
If there are two equally far from zero elements like 2 and -2,
consider the positive element to be "closest" to zero.
Please add a main method which evaluates and displays one or more test cases.
*/
int index = Math.abs(0 - a[0]);
int result = a[0];
for (int i : a) {
int abs = Math.abs(0 - i);
if (abs <= index) {
index = abs;
result = i;
}
}
return result;
}
public static void main(String[] args) {
int[] in = {};
System.out.println(getClosestToZero(in));
}
}

View File

@ -0,0 +1,68 @@
package com.ukg.framework.common.tasks;
import org.apache.commons.lang.WordUtils;
public class Task2 {
public static String capitalizeFirstLetters(String s) {
/*
* Please implement this method to capitalize all first letters of the
* words in the given String. All other symbols shall remain intact. If
* a word does not start with a letter, it shall remain intact also.
* Assume that the parameter String can only contain spaces and
* alphanumeric characters.
*
* NOTE: please keep in mind that the words can be divided by single or
* multiple spaces. The spaces also can be found at the beginning or the
* end of the parameter string, and you need to preserve them.
*
* Please add a main method which evaluates and displays one or more
* test cases.
*/
char[] delimiters = null;
if (s == null || s.length() == 0) {
return s;
}
int strLen = s.length();
StringBuffer buffer = new StringBuffer(strLen);
boolean capitalizeNext = true;
for (int i = 0; i < strLen; i++) {
char ch = s.charAt(i);
if (isDelimiter(ch, delimiters)) {
buffer.append(ch);
capitalizeNext = true;
} else if (capitalizeNext) {
buffer.append(Character.toTitleCase(ch));
capitalizeNext = false;
} else {
buffer.append(ch);
}
}
return buffer.toString();
}
private static boolean isDelimiter(char ch, char[] delimiters) {
if (delimiters == null) {
return Character.isWhitespace(ch);
}
for (int i = 0, isize = delimiters.length; i < isize; i++) {
if (ch == delimiters[i]) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String in = " 2 drer drse 23 wer 1 ";
System.out.println(capitalizeFirstLetters(in));
// FYI: Apache WordUtils has capitalize function to do this.
// System.out.println(WordUtils.capitalize(in));
}
}

View File

@ -0,0 +1,32 @@
package com.ukg.framework.common.tasks;
public class Task3 {
// Please do not change this interface
interface ListNode {
int getItem();
ListNode getNext();
void setNext(ListNode next);
}
public static ListNode reverse(ListNode node) {
/*
Please implement this method to
reverse a given linked list.
*/
ListNode temp = null, prev = null;
while(node != null){
temp = node.getNext();
node.setNext(prev);
prev = node;
node = temp;
}
return prev;
}
}

View File

@ -0,0 +1,103 @@
package com.ukg.framework.common.tasks;
import org.apache.commons.lang3.ObjectUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.io.Serializable;
import java.time.DateTimeException;
import java.util.Date;
import java.util.Objects;
public class Task4 {
static class TimeRange implements Serializable {
private static final long serialVersionUID = 6474765081240948885L;
/*
* Design a class that will represent a time range: start time to end time
* One possible use for such a class is in scheduling or booking application.
* Example: Conference room will be used from Monday 10am to 12pm
* Create any code constructors, variables and methods that you feel are necessary.
* One such method should test for intersection or conflict
* in times between 2 instances of TimeRange
*
*/
private Date startDateTime;
private Date endDateTime;
public TimeRange(Date startDateTime, Date endDateTime) {
this.startDateTime = startDateTime;
this.endDateTime = endDateTime;
}
public Date getStartDateTime() {
return startDateTime;
}
public void setStartDateTime(Date startDateTime) {
this.startDateTime = startDateTime;
}
public Date getEndDateTime() {
return endDateTime;
}
public void setEndDateTime(Date endDateTime) {
this.endDateTime = endDateTime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TimeRange timeRange = (TimeRange) o;
return Objects.equals(startDateTime, timeRange.startDateTime) && Objects.equals(endDateTime, timeRange.endDateTime);
}
@Override
public int hashCode() {
return Objects.hash(startDateTime, endDateTime);
}
public Boolean isConflict(TimeRange tr2) throws DateTimeException {
if (this.isValidRange() && tr2.isValidRange()) {
if (tr2.startDateTime.before(this.endDateTime))
return true;
else
return false;
} else {
throw new DateTimeException("Range Error ");
}
}
/**
* @return
*/
public Boolean isValidRange() throws DateTimeException {
if (ObjectUtils.isNotEmpty(startDateTime) && ObjectUtils.isNotEmpty(endDateTime) && startDateTime.before(endDateTime)) {
return true;
} else {
throw new DateTimeException("Time Range Error ");
}
}
}
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
TimeRange tr1 = new TimeRange(DateTime.parse("2021-12-09 08:00:01", formatter).toDate(), DateTime.parse("2021-12-09 10:00:01", formatter).toDate());
TimeRange tr2 = new TimeRange(DateTime.parse("2021-12-09 08:00:01", formatter).toDate(), DateTime.parse("2021-12-09 08:30:01", formatter).toDate());
try {
System.out.println(tr1.isConflict(tr2));
} catch (DateTimeException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,253 @@
package com.ossez.framework.common.tests.dao;
import com.ossez.framework.common.dao.Factory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.hibernate.annotations.Synchronize;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.ValidationException;
import java.util.*;
/**
* ReservationTest Testing
*
* @author YuCheng Hu
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class SearchTest {
private static Logger logger = LoggerFactory.getLogger(SearchTest.class);
HashMap<Integer, List> tokenMap = new HashMap<>();
HashMap<String, Set> queryToken = new HashMap<>();
@BeforeEach
protected void setUp() throws Exception {
logger.debug("-OPEN Database Connection-");
// Factory.beginTransaction();
}
@AfterEach
protected void tearDown() throws Exception {
logger.debug("-CLOSE Database Connection-");
// Factory.commitTransaction();
}
/**
* Tests Index
*/
@Test
public void testIndex() throws ValidationException {
// DO INDEX
logger.debug(indexDocument("index 1 soup tomato cream salt"));
logger.debug(indexDocument("index 2 cake sugar eggs flour sugar cocoa cream butter"));
logger.debug(indexDocument("index 1 bread butter salt"));
logger.debug(indexDocument("index 3 soup fish potato salt pepper"));
logger.debug("Size of token map - [{}]", tokenMap.size());
// LOAD DATA TO RAM
convertToken();
// DO QUERY
// logger.debug(query("in: query butter"));
// logger.debug(query("in: query sugar"));
// logger.debug(query("in: query soup"));
logger.debug(query("in: query (butter | potato) & (salt | tea)"));
logger.debug("Size of query Token - [{}]", queryToken.size());
}
/**
* Tests Query
*/
@Test
public void testQuery() throws ValidationException {
}
/**
* Build reservationRequest
*
* @return
*/
private String indexDocument(String token) {
List<String> tokenList = Arrays.asList(StringUtils.split(token));
if (tokenList.size() < 3)
return "index error: The format of input error. Please using format: [index 1 soup tomato cream salt]";
if (!StringUtils.equalsIgnoreCase(tokenList.get(0), "Index"))
return "index error: The format of input error. Please using Index to start it";
if (!NumberUtils.isCreatable(tokenList.get(1)))
return "index error: The format of input error. The Doc Index must Number";
Integer indexId = NumberUtils.toInt(tokenList.get(1));
List<String> tokens = tokenList.subList(2, tokenList.size());
if (ObjectUtils.isNotEmpty(tokenMap.get(indexId)))
tokenMap.remove(indexId);
tokenMap.put(indexId, tokens);
return "index ok " + indexId;
}
/**
* Build reservationRequest
*
* @return
*/
private String query(String q) {
List<String> qList = Arrays.asList(StringUtils.split(q));
if (qList.size() < 3)
return "query error: The format of input error. Please using format: [in: query butter]";
if (!StringUtils.equalsIgnoreCase(qList.get(0), "in:"))
return "index error: The format of input error. Please using [in:] to start it";
if (!StringUtils.equalsIgnoreCase(qList.get(1), "query"))
return "index error: The format of input error. String of command is [query]";
q = StringUtils.replace(q, StringUtils.SPACE, StringUtils.EMPTY);
q = StringUtils.replace(q, "in:query", StringUtils.EMPTY);
List<String> queryList = processQueryString(q);
// ONLY SEARCH 1 TOKEN
if (queryList.size() == 1) {
return "query results " + Arrays.toString(queryToken.get(queryList.get(0)).toArray());
}
Set<Integer> result = new HashSet<>();
result.clear();
for (String qStr : queryList) {
qStr = StringUtils.remove(qStr, "(");
qStr = StringUtils.remove(qStr, ")");
logger.debug(">>>>>>>>>>{}", qStr);
// if (StringUtils.contains(qStr, "|") && !StringUtils.startsWith(qStr, "|")) {
// result = orCal(StringUtils.split(qStr, "|")[0], StringUtils.split(qStr, "|")[1]);
// }
//
// if (StringUtils.contains(qStr, "&") && !StringUtils.startsWith(qStr, "&")) {
// result = andCal(StringUtils.split(qStr, "&")[0], StringUtils.split(qStr, "&")[1]);
// }
// if (StringUtils.startsWith(qStr, "|") || StringUtils.endsWith(qStr,"|")) {
// result.addAll(queryToken.get(StringUtils.substringAfter(qStr, "|")));
// }
//
// if (StringUtils.startsWith(qStr, "&")|| StringUtils.endsWith(qStr,"&")) {
// result.retainAll(queryToken.get(StringUtils.substringAfter(qStr, "&")));
// }
}
return "query results " + Arrays.toString(result.toArray());
}
/**
* LOAD INDEX
*/
private void convertToken() {
queryToken = new HashMap<String, Set>();
tokenMap.forEach((k, v) -> {
Integer docId = k;
List<String> tokens = v;
for (String token : tokens) {
Set<Integer> docIdset = new HashSet<>();
if (ObjectUtils.isEmpty(queryToken.get(token))) {
docIdset.add(docId);
} else {
docIdset = queryToken.get(token);
docIdset.add(docId);
}
queryToken.put(token, docIdset);
}
});
}
/**
* @return
* @throws ValidationException
*/
private List<String> processQueryString(String q) throws ValidationException {
List<String> opList = Lists.newArrayList();
// String input = "(a|b)&c";
while (StringUtils.isNotEmpty(q)) {
String op = StringUtils.EMPTY;
if (StringUtils.contains(q, "(") && StringUtils.contains(q, ")")) {
String opRight = StringUtils.substringAfterLast(q, "(");
int startIndex = StringUtils.lastIndexOf(q, "(");
int endIndex = startIndex + StringUtils.indexOf(opRight, ")");
op = StringUtils.substring(q, startIndex, endIndex + 2);
} else {
op = q;
}
opList.add(op);
q = StringUtils.remove(q, op);
}
return opList;
}
/**
* OR Cal
*
* @param token1
* @param token2
* @return
*/
private Set<Integer> orCal(String token1, String token2) {
Set<Integer> result = new HashSet<>();
result.addAll(queryToken.get(token1));
result.addAll(queryToken.get(token2));
return result;
}
/**
* AND Cal
*
* @param token1
* @param token2
* @return
*/
private Set<Integer> andCal(String token1, String token2) {
Set<Integer> result = new HashSet<>();
result.addAll(queryToken.get(token1));
result.retainAll(queryToken.get(token2));
return result;
}
}

View File

@ -13,8 +13,6 @@
<description>Demo for Framework Orm Factory Query Data</description>
<url>https://github.com/honeymoose/Framework-Orm-Factory.git</url>
<developers>
<developer>
<name>YuCheng Hu</name>
@ -23,7 +21,7 @@
<timezone>-5</timezone>
<organization>Open Source</organization>
<roles>
<role>Sr. Java Developer</role>
<role>Developer</role>
</roles>
</developer>
</developers>