Skip to content

Commit

Permalink
Feature/show settings (#15)
Browse files Browse the repository at this point in the history
* added show timezone command
* updated thorntail to 2.3.0.Final
* updated what to support up to 255 characters
* internal code restructuring and refactoring
  • Loading branch information
HoodElliot authored and JChrist committed Feb 14, 2019
1 parent b0f4a16 commit 98d102f
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ hs_err_pid*
.idea/
cmake-build-debug/
cmake-build-release/
.idea/**/mongoSettings.xml
*.iws
*.iml
*.ipr
Expand All @@ -66,3 +65,4 @@ buildNumber.properties
keys/
.classpath
.project
.vscode/
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
`@bot set global timezone to Paris`
d) By default it uses GMT

3) Show my reminders
a) For each user shows reminders that will notify him.
3) Show my reminders and timezone
a) For each user shows reminders that will notify him.
`@bot list`
Example:
`1) ID:23 what:' Something to do ' When: 23/01/2019 18:20 Europe/Athens`
`1) ID:23 what:' Something to do ' When: 23/01/2019 18:20 Europe/Athens`
b) To show your timezone and global timezone simply do
`@bot timezones`
4) Delete a reminder
a) For each user, using a reminders id.
`@bot delete 323 `
15 changes: 11 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<groupId>gr.cytech</groupId>
<artifactId>chatreminderbot</artifactId>
<name>Chat Reminder Bot</name>
<version>1.2.0</version>
<version>1.3.0</version>
<packaging>war</packaging>

<properties>
<version.thorntail>2.2.1.Final</version.thorntail>
<version.thorntail>2.3.0.Final</version.thorntail>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
Expand Down Expand Up @@ -56,10 +56,9 @@
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
Expand Down Expand Up @@ -147,6 +146,14 @@
<groupId>io.thorntail</groupId>
<artifactId>microprofile-restclient</artifactId>
</dependency>
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>jaxrs-jsonb</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ public String checkRemindMessageFormat() {
whoPart.remove(0);
}

if (splitMsg.get(1).length() > 50) {
return "Part what can not be more than 50 chars.";
if (splitMsg.get(1).length() >= 255) {
return "Part what can not be more than 255 chars.";
} else if (splitMsg.size() != 3) {
return "Use quotation marks `'` only two times. One before and one after what, type Help for example.";
} else if (whoPart.size() < 2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package gr.cytech.chatreminderbot.rest.controlCases;

import gr.cytech.chatreminderbot.rest.message.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

public class CaseShowTimezones {
private final static Logger logger = LoggerFactory.getLogger(CaseShowReminders.class.getName());

@PersistenceContext(name = "wa")
public EntityManager entityManager;

private Request request;

public Request getRequest() {
return request;
}

public CaseShowTimezones() {

}
public boolean defaultTimezoneExists() {
return entityManager.createQuery("SELECT t from TimeZone t where t.userid = :default")
.setParameter("default","default")
.getResultList().size() == 1;
}

@Transactional
String showTimezones(Request request) {
this.request = request;
String showTimezone = "---- Your timezone is ---- \n";
String noTimezoneFound = "---- No Timezone found default timezone is ---- \n";
String defaultTimezone = "---- Default timezone is ---- \n";

if (!defaultTimezoneExists()){
logger.info("created default timezone");
TimeZone timeZone = new TimeZone("Europe/Athens","default");
entityManager.persist(timeZone);

}

TimeZone defaultTimezoneQuery = (TimeZone) entityManager.createNamedQuery("show.timezones")
.setParameter("id","default")
.getSingleResult();
try {

TimeZone myTimezone = (TimeZone) entityManager
.createNamedQuery("show.timezones")
.setParameter("id", request.getMessage().getSender().getName())
.getSingleResult();

return showTimezone + "Timezone = " + myTimezone.toString() + "\n " + defaultTimezone +
"Timezone = " + defaultTimezoneQuery.toString();

} catch (NoResultException e) {
logger.info("in case no timezone found for the user");
return noTimezoneFound + "Timezone = " + defaultTimezoneQuery.toString();

}

}


}
119 changes: 67 additions & 52 deletions src/main/java/gr/cytech/chatreminderbot/rest/controlCases/Control.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class Control {
@Inject
CaseShowReminders caseShowReminders;

@Inject
CaseShowTimezones caseShowTimezones;

@Inject
CaseSetTimezone caseSetTimezone;

Expand All @@ -32,25 +35,31 @@ public class Control {
private ArrayList<String> splitMsg;

//--- Key Words
private String keyWord_help = "help";

private String keyWord_remind = "remind";
private static final String KEYWORD_HELP = "help";

private static final String KEYWORD_REMIND = "remind";

//Current format;
//set my timezone to athens

private String[] keyWords_setTimezone = {"set", "timezone", "to"};
private String keyWord_setTimezone_global = "global";
private String keyWord_setTimezone_my = "my";
private static final String KEYWORD_SET_TIMEZONE_GLOBAL = "global";
private static final String KEYWORD_SET_TIMEZONE_MY = "my";


private static final String KEYWORD_SHOW_REMINDERS = "list";

private String keyWord_ShowReminders = "list";
private static final String KEYWORD_DELETE_REMINDER = "delete";

private String keyWord_deleteReminder = "delete";
private static final String KEYWORD_SHOW_TIMEZONES = "timezones";
//-- End of keyWords

//-- Responses
private String responseCaseDefault = "I didnt understand you, type help for instructions \n";
private String responseCaseEmptyRequest = "I didnt understand you, you send empty message";
private static final String RESPONSE_CASE_DEFAULT = "I didnt understand you, type help for instructions \n";

private static final String RESPONSE_CASE_EMPTY_REQUEST = "I didnt understand you, you send empty message";


public Control() {
}
Expand Down Expand Up @@ -79,49 +88,51 @@ public void setRequest(Request request) {
public String controlResponse() {

if (splitMsg.isEmpty()) {
return responseCaseEmptyRequest;
} else
//----- Case Response with instructions - Help
if (splitMsg.size() == 1 && splitMsg.get(0).equals(keyWord_help)) {
logger.info("----Case Help----");
return caseHelp();
} else
//-------Case create Reminder -----------------
if (splitMsg.get(0).equals(keyWord_remind)) {
logger.info("---Case remind---");

return caseSetReminder();
} else
//------------Case Set timezones -------------------
if (splitMsg.size() == 5 && (splitMsg.get(0).equals(keyWords_setTimezone[0])

&& splitMsg.get(2).equals(keyWords_setTimezone[1])
&& splitMsg.get(3).equals(keyWords_setTimezone[2]))) {
logger.info("----Case set timezone----");

return caseSetTimezone();
} else
///------------Case Show my reminders -------------------
if (splitMsg.size() == 1 && splitMsg.get(0).equals(keyWord_ShowReminders)) {
logger.info("----Case list reminders----");
return caseShowReminders();
} else
///------------Case Delete reminder -------------------
if (splitMsg.size() == 2 && splitMsg.get(0).equals(keyWord_deleteReminder)) {
logger.info("----Case delete reminder----");
return caseDeleteReminder();
} else
///------------Case Default -------------------
{
logger.info("----Case default ---- ");
return responseCaseDefault;
}
return RESPONSE_CASE_EMPTY_REQUEST;
}
//----- Case Response with instructions - Help
if (splitMsg.size() == 1 && splitMsg.get(0).equals(KEYWORD_HELP)) {
logger.info("----Case Help----");
return caseHelp();
}
//-------Case create Reminder -----------------
if (splitMsg.get(0).equals(KEYWORD_REMIND)) {
logger.info("---Case remind---");
return caseSetReminder();
}
//------------Case Set timezones -------------------
if (splitMsg.size() == 5 && (splitMsg.get(0).equals(keyWords_setTimezone[0])

&& splitMsg.get(2).equals(keyWords_setTimezone[1])
&& splitMsg.get(3).equals(keyWords_setTimezone[2]))) {
logger.info("----Case set timezone----");

return caseSetTimezone();
}
///------------Case Show my reminders -------------------
if (splitMsg.size() == 1 && splitMsg.get(0).equals(KEYWORD_SHOW_REMINDERS)) {
logger.info("----Case list reminders----");
return caseShowReminders();
}
///------------Case Delete reminder -------------------
if (splitMsg.size() == 2 && splitMsg.get(0).equals(KEYWORD_DELETE_REMINDER)) {
logger.info("----Case delete reminder----");
return caseDeleteReminder();
}
if (splitMsg.size()== 1 && splitMsg.get(0).equals(KEYWORD_SHOW_TIMEZONES)){
logger.info("---- Case Timezones -----");
return caseShowTimezones();
}
///------------Case Default -------------------

logger.info("----Case default ---- ");
return RESPONSE_CASE_DEFAULT;

}

//-- Cases ----

public String caseHelp() {
private String caseHelp() {
return "\n ----- Instructions using the reminder bot ----- \n \n" +
"1) Set a reminder \n \n" +
" a) For you \n" +
Expand Down Expand Up @@ -150,28 +161,32 @@ public String caseHelp() {
" `@" + BOT_NAME + " delete 323 ` \n";
}

public String caseSetReminder() {
private String caseSetReminder() {
caseSetReminder.setRequest(request);
caseSetReminder.setBOT_NAME(BOT_NAME);
return caseSetReminder.setReminder();
}

public String caseSetTimezone() {
private String caseSetTimezone() {

caseSetTimezone.setRequest(request);
caseSetTimezone.setSplitMsg(splitMsg);
caseSetTimezone.setKeyWord_global(keyWord_setTimezone_global);
caseSetTimezone.setKeyWord_my(keyWord_setTimezone_my);
caseSetTimezone.setKeyWord_global(KEYWORD_SET_TIMEZONE_GLOBAL);
caseSetTimezone.setKeyWord_my(KEYWORD_SET_TIMEZONE_MY);

return caseSetTimezone.setTimezone();
}

public String caseShowReminders() {
private String caseShowReminders() {
caseShowReminders.setRequest(request);
return caseShowReminders.showReminders();
}

public String caseDeleteReminder() {
private String caseShowTimezones(){
return caseShowTimezones.showTimezones(request);
}

private String caseDeleteReminder() {
caseDeleteReminder.setRequest(request);
caseDeleteReminder.setSplitMsg(splitMsg);
return caseDeleteReminder.deleteReminder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
@NamedQuery(name = "get.Alltimezone",
query = "SELECT t from TimeZone t"),
@NamedQuery(name = "set.timezone",
query = "UPDATE TimeZone t set t.timezone = :timezone WHERE t.userid LIKE :userid ")
query = "UPDATE TimeZone t set t.timezone = :timezone WHERE t.userid LIKE :userid "),
@NamedQuery(name = "show.timezones",
query = "SELECT t from TimeZone t where t.userid = :id ")
})
public class TimeZone {
private final static Logger logger = LoggerFactory.getLogger(TimeZone.class.getName());
Expand All @@ -36,6 +38,7 @@ public TimeZone(String timezone, String userid) {
this.userid = userid;
}


public String getUserid() {
return userid;
}
Expand All @@ -47,8 +50,8 @@ public String getTimezone() {
public String findTimeZones(String inputTimeZone) {
List<String> worldTimeZones = new ArrayList<>(ZoneId.getAvailableZoneIds());
for (String timeZone : worldTimeZones) {
//z is like Europe/Athens so,
//we either get it at full form or as the city name.
// z is like Europe/Athens so,
// we either get it at full form or as the city name.
String[] slitted = timeZone.split("/");
if (timeZone.equalsIgnoreCase(inputTimeZone) ||
slitted[slitted.length - 1].equalsIgnoreCase(inputTimeZone)) {
Expand All @@ -58,4 +61,9 @@ public String findTimeZones(String inputTimeZone) {
logger.info("Wrong timezone");
return null;
}

@Override
public String toString() {
return "'" + timezone + '\'';
}
}
1 change: 1 addition & 0 deletions src/main/resources/db/migration/V3__updateChars_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE reminder ALTER COLUMN what TYPE varchar(255);
Loading

0 comments on commit 98d102f

Please sign in to comment.