Permalink
Browse files

Added jspringbot-date library

  • Loading branch information...
1 parent 210c5ff commit 8fa7954563232dfebc6c6849822fe380a0353cdf @badong2210 badong2210 committed Dec 20, 2012
@@ -31,6 +31,6 @@
<bean name="csvHelper" class="org.jspringbot.keyword.csv.CSVHelper"/>
<!-- Scan components -->
- <ctx:component-scan base-package="org.jspringbot.Keyword.csv" />
+ <ctx:component-scan base-package="org.jspringbot.keyword.csv" />
<ctx:annotation-config/>
</beans>
@@ -0,0 +1,94 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ Copyright (c) 2012. JSpringBot. All Rights Reserved.
+ ~
+ ~ See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The JSpringBot licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ 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.
+ -->
+
+<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 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.jspringbot</groupId>
+ <artifactId>jspringbot-libraries</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.jspringbot</groupId>
+ <artifactId>jspringbot-date</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <packaging>jar</packaging>
+ <name>jspringbot-date</name>
+
+ <build>
+ <testResources>
+ <testResource>
+ <directory>src/test/resources</directory>
+ <filtering>true</filtering>
+ </testResource>
+ </testResources>
+ </build>
+
+ <properties>
+ <libdoc.name>JSpringBot DB Library</libdoc.name>
+ <libdoc.output>JSpringBotDBLibrary.html</libdoc.output>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>commons-collections</groupId>
+ <artifactId>commons-collections</artifactId>
+ <version>3.2.1</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-dbcp</groupId>
+ <artifactId>commons-dbcp</artifactId>
+ <version>1.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jspringbot</groupId>
+ <artifactId>jspringbot-syntax-highlight</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.8.0-rc2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>${spring.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>2.1</version>
+ </dependency>
+ </dependencies>
+</project>
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2012. JSpringBot. All Rights Reserved.
+ *
+ * See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The JSpringBot licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.jspringbot.keyword.date;
+
+import org.jspringbot.Keyword;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public abstract class AbstractDateKeyword implements Keyword {
+
+ @Autowired
+ protected DateHelper helper;
+
+ public abstract Object execute(Object[] objects) throws Exception;
+}
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2012. JSpringBot. All Rights Reserved.
+ *
+ * See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The JSpringBot licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.jspringbot.keyword.date;
+
+import org.jspringbot.KeywordInfo;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+@Component
+@KeywordInfo(name = "Add Date Time Days", description = "Add the given days to the current date time.", parameters = {"numberOfDays"})
+public class AddDateTimeDays extends AbstractDateKeyword {
+
+ @Override
+ public Object execute(Object[] params) throws IOException {
+ helper.plusDays(Integer.parseInt(String.valueOf(params[0])));
+
+ return null;
+ }
+}
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2012. JSpringBot. All Rights Reserved.
+ *
+ * See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The JSpringBot licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.jspringbot.keyword.date;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+import org.jspringbot.syntax.HighlightRobotLogger;
+
+public class DateHelper {
+ public static final HighlightRobotLogger LOG = HighlightRobotLogger.getLogger(DateHelper.class);
+
+ private DateTime current = new DateTime();
+
+ private DateTimeZone currentTimeZone = DateTimeZone.getDefault();
+
+ private String formatterPattern = "yyyy-MM-dd HH:mm:ss zz";
+
+ public void setDateTimeZone(String timeZoneId) {
+
+ LOG.createAppender()
+ .appendBold("Set Date Time Zone:")
+ .appendProperty("Time Zone ID", timeZoneId)
+ .log();
+
+ currentTimeZone = DateTimeZone.forID(timeZoneId);
+ }
+
+ public void setDateTimeFormat(String pattern) {
+ LOG.createAppender()
+ .appendBold("Set Date Time Format:")
+ .appendProperty("Pattern", pattern)
+ .log();
+
+ DateTimeFormat.forPattern(pattern);
+ }
+
+ public String printDateTime() {
+ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatterPattern);
+ DateTime dt = current.withZone(currentTimeZone);
+
+ String formattedValue = dateTimeFormatter.print(dt);
+
+ LOG.createAppender()
+ .appendBold("Print Date Time:")
+ .appendProperty("Pattern", formatterPattern)
+ .appendProperty("Time Zone ID", currentTimeZone.getID())
+ .appendProperty("Print", formattedValue)
+ .log();
+
+ return formattedValue;
+ }
+
+ public void resetDateTime() {
+ current = new DateTime();
+
+ LOG.createAppender()
+ .appendBold("Reset Date Time:")
+ .appendProperty("Pattern", formatterPattern)
+ .appendProperty("Time Zone ID", currentTimeZone.getID())
+ .appendProperty("Print", printDateTime())
+ .log();
+ }
+
+ public void plusDays(int days) {
+ current = current.plusDays(days);
+
+ LOG.createAppender()
+ .appendBold("Add Date Time Days:")
+ .appendProperty("Days", days)
+ .appendProperty("Print", printDateTime())
+ .log();
+ }
+
+ public void minusDays(int days) {
+ current = current.minusDays(days);
+
+ LOG.createAppender()
+ .appendBold("Subtract Date Time Days:")
+ .appendProperty("Days", days)
+ .appendProperty("Print", printDateTime())
+ .log();
+ }
+}
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2012. JSpringBot. All Rights Reserved.
+ *
+ * See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The JSpringBot licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.jspringbot.keyword.date;
+
+import org.jspringbot.KeywordInfo;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+@Component
+@KeywordInfo(name = "Print Date Time", description = "Prints the date time to string given the set format.")
+public class PrintDateTime extends AbstractDateKeyword {
+
+ @Override
+ public Object execute(Object[] params) throws IOException {
+ return helper.printDateTime();
+ }
+}
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2012. JSpringBot. All Rights Reserved.
+ *
+ * See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The JSpringBot licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.jspringbot.keyword.date;
+
+import org.jspringbot.KeywordInfo;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+@Component
+@KeywordInfo(name = "Reset Date Time", description = "Reset date time to current date.")
+public class ResetDateTime extends AbstractDateKeyword {
+
+ @Override
+ public Object execute(Object[] params) throws IOException {
+ helper.resetDateTime();
+
+ return null;
+ }
+}
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2012. JSpringBot. All Rights Reserved.
+ *
+ * See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The JSpringBot licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.jspringbot.keyword.date;
+
+import org.jspringbot.KeywordInfo;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+@Component
+@KeywordInfo(name = "Set Date Time Format", description = "Set the date time format pattern.", parameters = {"formatPattern"})
+public class SetDateTimeFormat extends AbstractDateKeyword {
+
+ @Override
+ public Object execute(Object[] params) throws IOException {
+ helper.setDateTimeFormat(String.valueOf(params[0]));
+
+ return null;
+ }
+}
Oops, something went wrong.

0 comments on commit 8fa7954

Please sign in to comment.