Skip to content

Commit

Permalink
SAK-25369 - Syllabus datepicker should use internationalized versions (
Browse files Browse the repository at this point in the history
…sakaiproject#4196)

Syllabus datepicker should use internationalized versions
  • Loading branch information
Miguel Pellicer authored and ottenhoff committed Apr 14, 2017
1 parent dc9d193 commit a109cc9
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
**********************************************************************************/
package org.sakaiproject.api.app.syllabus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

public interface SyllabusData
{
public final String ITEM_POSTED="posted";
public final String ITEM_DRAFT= "draft";
public final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
/**
* @return Returns the emailNotification.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.sakaiproject.tool.cover.SessionManager;
import org.sakaiproject.tool.api.Session;
import org.sakaiproject.tool.cover.ToolManager;
import org.sakaiproject.tool.syllabus.util.DateUtil;
import org.sakaiproject.user.cover.UserDirectoryService;
import org.sakaiproject.util.FormattedText;
import org.sakaiproject.util.ResourceLoader;
Expand All @@ -95,6 +96,11 @@ public class SyllabusTool
private static final String SESSION_ATTACHMENT_DATA_ID = "syllabysAttachDataId";
//used for the UI to know which data ID to have opened by default (i.e. if you added/removed an attachment on the main page)
private String openDataId;
private static final String HIDDEN_START_ISO_DATE = "dataStartDateISO8601";
private static final String HIDDEN_END_ISO_DATE = "dataEndDateISO8601";
private static final String DATEPICKER_DATE_FORMAT = "yyyy-MM-dd";
private static final String DATEPICKER_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";


public class DecoratedSyllabusEntry
{
Expand All @@ -111,6 +117,8 @@ public class DecoratedSyllabusEntry
protected boolean justCreated = false;

protected ArrayList attachmentList = null;
private String startDateString;
private String endDateString;

public DecoratedSyllabusEntry(SyllabusData en)
{
Expand All @@ -122,6 +130,8 @@ public DecoratedSyllabusEntry(SyllabusData en)
this.orig_endDate = en.getEndDate() == null ? null : (Date) en.getEndDate().clone();
this.orig_isLinkCalendar= en.isLinkCalendar();
this.orig_status = en.getStatus();
this.startDateString = DateUtil.format(en.getStartDate(), DATEPICKER_DATETIME_FORMAT, rb.getLocale());
this.endDateString = DateUtil.format(en.getEndDate(), DATEPICKER_DATETIME_FORMAT, rb.getLocale());
}

public SyllabusData getEntry()
Expand Down Expand Up @@ -326,6 +336,32 @@ public boolean getStartAndEndDatesSameDay(){
public String getDraftStatus() {
return draftStatus;
}

public String getStartDateString() {
return this.startDateString;
}

public void setStartDateString(String startDateString) {
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String startISODate = params.get((getEntry().getPosition() - new Integer(1)) + HIDDEN_START_ISO_DATE);
if(DateUtil.isValidISODate(startISODate)){
getEntry().setStartDate(DateUtil.parseISODate(startISODate));
}
this.startDateString = DateUtil.format(getEntry().getStartDate(), DATEPICKER_DATETIME_FORMAT, rb.getLocale());
}

public String getEndDateString() {
return this.endDateString;
}

public void setEndDateString(String endDateString) {
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String endISODate = params.get((getEntry().getPosition() - new Integer(1)) + HIDDEN_END_ISO_DATE);
if(DateUtil.isValidISODate(endISODate)){
getEntry().setEndDate(DateUtil.parseISODate(endISODate));
}
this.endDateString = DateUtil.format(getEntry().getEndDate(), DATEPICKER_DATETIME_FORMAT, rb.getLocale());
}
}

protected SyllabusManager syllabusManager;
Expand Down Expand Up @@ -2760,7 +2796,7 @@ public String getSyllabusDataStartDate()
{
Date rvDate = syllabusData.getStartDate();
if(rvDate != null){
rv = SyllabusData.dateFormat.format(rvDate);
rv = DateUtil.format(rvDate, DATEPICKER_DATETIME_FORMAT, rb.getLocale());
}
alert = false;
}
Expand Down Expand Up @@ -2789,11 +2825,11 @@ public void setSyllabusDataStartDate(String date)
if(date == null || "".equals(date)){
syllabusData.setStartDate(null);
}else{
try {
syllabusData.setStartDate(SyllabusData.dateFormat.parse(date));
} catch (ParseException e) {
//date won't be changed
}
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String startISODate = params.get(HIDDEN_START_ISO_DATE);
if(DateUtil.isValidISODate(startISODate)){
syllabusData.setStartDate(DateUtil.parseISODate(startISODate));
}
}
}
}
Expand All @@ -2816,7 +2852,7 @@ public String getSyllabusDataEndDate()
{
Date rvDate = syllabusData.getEndDate();
if(rvDate != null){
rv = SyllabusData.dateFormat.format(rvDate);
rv = DateUtil.format(rvDate, DATEPICKER_DATETIME_FORMAT, rb.getLocale());
}
alert = false;
}
Expand Down Expand Up @@ -2845,11 +2881,11 @@ public void setSyllabusDataEndDate(String date)
if(date == null || "".equals(date)){
syllabusData.setEndDate(null);
}else{
try {
syllabusData.setEndDate(SyllabusData.dateFormat.parse(date));
} catch (ParseException e) {
//date won't be changed
}
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String endISODate = params.get(HIDDEN_END_ISO_DATE);
if(DateUtil.isValidISODate(endISODate)){
syllabusData.setEndDate(DateUtil.parseISODate(endISODate));
}
}
}
}
Expand Down Expand Up @@ -2944,7 +2980,6 @@ private void removeCalendarAttachment(SyllabusData data, SyllabusAttachment atta
}

public class BulkSyllabusEntry{
public final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
public final SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm a");
private String title = "";
private Date startDate = null;
Expand Down Expand Up @@ -3041,7 +3076,7 @@ public String getStartDateString()
{
String rv = "";
if(getStartDate() != null){
rv = dateFormat.format(getStartDate());
rv = DateUtil.format(getStartDate(), DATEPICKER_DATE_FORMAT, rb.getLocale());
}
return rv;
}
Expand All @@ -3051,10 +3086,10 @@ public void setStartDateString(String date)
if(date == null || "".equals(date)){
setStartDate(null);
}else{
try {
setStartDate(dateFormat.parse(date));
} catch (ParseException e) {
//date won't be changed
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String startISODate = params.get(HIDDEN_START_ISO_DATE);
if(DateUtil.isValidISODate(startISODate)){
setStartDate(DateUtil.parseISODate(startISODate));
}
}
}
Expand All @@ -3063,7 +3098,7 @@ public String getEndDateString()
{
String rv = "";
if(getEndDate() != null){
rv = dateFormat.format(getEndDate());
rv = DateUtil.format(getEndDate(), DATEPICKER_DATE_FORMAT, rb.getLocale());
}
return rv;
}
Expand All @@ -3073,10 +3108,10 @@ public void setEndDateString(String date)
if(date == null || "".equals(date)){
setEndDate(null);
}else{
try {
setEndDate(dateFormat.parse(date));
} catch (ParseException e) {
//date won't be changed
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String endISODate = params.get(HIDDEN_END_ISO_DATE);
if(DateUtil.isValidISODate(endISODate)){
setEndDate(DateUtil.parseISODate(endISODate));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**********************************************************************************
* $URL$
* $Id$
**********************************************************************************
*
* Copyright (c) 2008 The Sakai Foundation
*
* Licensed under the Educational Community 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.opensource.org/licenses/ECL-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.sakaiproject.tool.syllabus.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Locale;

/**
* Performs date validation respecting i18n.<br>
* <b>Note:</b> This class does not support "hi_IN", "ja_JP_JP" and "th_TH" locales.
*/
public final class DateUtil {

private static DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;

private DateUtil() {
}

/**
* Performs date validation checking the ISO_ZONED_DATE_TIME format such as '2011-12-03T10:15:30+01:00[Europe/Paris]'.
*
* @param date
* The candidate String date.
* @return TRUE - Conforms to a valid input date format string.<br>
* FALSE - Does not conform.
*/
public static boolean isValidISODate(final String date) {
try {
isoFormatter.parse(date);
return true;
} catch (Exception e) {
return false;
}
}

/**
* Parse the date string input using the ISO_ZONED_DATE_TIME format such as '2011-12-03T10:15:30+01:00[Europe/Paris]'.
*
* @param inputDate
* The string that needs to be parsed.
*
* @throws DateTimeParseException
* If not a valid date compared to ISO_ZONED_DATE_TIME format
*/
public static Date parseISODate(final String inputDate) throws DateTimeParseException {
Date convertedDate = null;

try {
LocalDateTime ldt = LocalDateTime.parse(inputDate, isoFormatter);
convertedDate = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
} catch (DateTimeParseException e) {
throw new DateTimeParseException (e.getMessage(), inputDate, 0);
}

return convertedDate;
}
/**
* Formats the date input to String using the format given.
*
* @param inputDate
* The date that needs to be formatted.
* @param format
* The given date-time format.
* @param locale
* The given locale.
* @throws ParseException
* If not a valid date compared to ISO_ZONED_DATE_TIME format
*/
public static String format(Date inputDate, String format, Locale locale){
SimpleDateFormat formatter = null;
try{
formatter = new SimpleDateFormat(format, locale);
return formatter.format(inputDate);
}catch(Exception ex){
formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US);
return formatter.format(inputDate);
}
}
}
73 changes: 23 additions & 50 deletions syllabus/syllabus-app/src/webapp/syllabus/edit.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,34 @@

<script>includeLatestJQuery('edit.jsp');</script>
<link rel="stylesheet" href="/library/webjars/jquery-ui/1.11.3/jquery-ui.min.css" type="text/css" />
<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="/library/js/lang-datepicker/lang-datepicker.js"></script>


<script type="text/javascript">
$(function() {
$('.dateInput').datetimepicker({
hour: 8,
timeFormat: "hh:mm tt",
currentText: "<h:outputText value="#{msgs.now}"/>",
closeText: "<h:outputText value="#{msgs.done}"/>",
amNames: ['<h:outputText value="#{msgs.am}"/>', '<h:outputText value="#{msgs.am2}"/>'],
pmNames: ['<h:outputText value="#{msgs.pm}"/>', '<h:outputText value="#{msgs.pm2}"/>'],
timeText: "<h:outputText value="#{msgs.time}"/>",
hourText: "<h:outputText value="#{msgs.hour}"/>",
minuteText: "<h:outputText value="#{msgs.minute}"/>",
monthNames: ["<h:outputText value="#{msgs.jan}"/>",
"<h:outputText value="#{msgs.feb}"/>",
"<h:outputText value="#{msgs.mar}"/>",
"<h:outputText value="#{msgs.apr}"/>",
"<h:outputText value="#{msgs.may}"/>",
"<h:outputText value="#{msgs.jun}"/>",
"<h:outputText value="#{msgs.jul}"/>",
"<h:outputText value="#{msgs.aug}"/>",
"<h:outputText value="#{msgs.sep}"/>",
"<h:outputText value="#{msgs.oct}"/>",
"<h:outputText value="#{msgs.nov}"/>",
"<h:outputText value="#{msgs.dec}"/>"],
dayNames: ["<h:outputText value="#{msgs.sunday}"/>",
"<h:outputText value="#{msgs.monday}"/>",
"<h:outputText value="#{msgs.tuesday}"/>",
"<h:outputText value="#{msgs.wednesday}"/>",
"<h:outputText value="#{msgs.thursday}"/>",
"<h:outputText value="#{msgs.friday}"/>",
"<h:outputText value="#{msgs.saturday}"/>"],
dayNamesMin: ["<h:outputText value="#{msgs.sun}"/>",
"<h:outputText value="#{msgs.mon}"/>",
"<h:outputText value="#{msgs.tue}"/>",
"<h:outputText value="#{msgs.wed}"/>",
"<h:outputText value="#{msgs.thu}"/>",
"<h:outputText value="#{msgs.fri}"/>",
"<h:outputText value="#{msgs.sat}"/>"],
beforeShow: function (textbox, instance) {
instance.dpDiv.css({
marginLeft: textbox.offsetWidth + 'px'
});
}
jQuery(document).ready(function() {
localDatePicker({
input: '#syllabusEdit\\:dataStartDate',
useTime: 1,
parseFormat: 'YYYY-MM-DD HH:mm:ss',
allowEmptyDate: true,
val: '<h:outputText value="#{SyllabusTool.syllabusDataStartDate}"/>',
ashidden: {
iso8601: 'dataStartDateISO8601'}
});
localDatePicker({
input: '#syllabusEdit\\:dataEndDate',
useTime: 1,
parseFormat: 'YYYY-MM-DD HH:mm:ss',
allowEmptyDate: true,
val: '<h:outputText value="#{SyllabusTool.syllabusDataEndDate}"/>',
ashidden: {
iso8601: 'dataEndDateISO8601'}
});
});
});
</script>
<h:outputText value="#{SyllabusTool.alertMessage}" styleClass="alertMessage" rendered="#{SyllabusTool.alertMessage != null}" />
<h:messages styleClass="alertMessage" rendered="#{!empty facesContext.maximumSeverity}" />
<h:form>
<h:form id="syllabusEdit">
<sakai:tool_bar_message value="#{msgs.add_sylla}" />
<sakai:doc_section>
<h:outputText value="#{msgs.newSyllabusForm1}"/>
Expand Down Expand Up @@ -161,15 +136,13 @@
<h:outputLabel for="dataStartDate">
<h:outputText value="#{msgs.startdatetitle}"/>
</h:outputLabel>
<h:inputText styleClass="dateInput datInputStart" value="#{SyllabusTool.syllabusDataStartDate}" id="dataStartDate"/>
<f:verbatim><img src="/library/image/silk/calendar_view_month.png" onclick="$('.datInputStart').focus();"/></f:verbatim>
<h:inputText styleClass="datInputStart" value="#{SyllabusTool.syllabusDataStartDate}" id="dataStartDate"/>
</h:panelGroup>
<h:panelGroup styleClass="shorttext">
<h:outputLabel for="dataEndDate">
<h:outputText value="#{msgs.enddatetitle}"/>
</h:outputLabel>
<h:inputText styleClass="dateInput datInputEnd" value="#{SyllabusTool.syllabusDataEndDate}" id="dataEndDate"/>
<f:verbatim><img src="/library/image/silk/calendar_view_month.png" onclick="$('.datInputEnd').focus();"/></f:verbatim>
<h:inputText styleClass="datInputEnd" value="#{SyllabusTool.syllabusDataEndDate}" id="dataEndDate"/>
</h:panelGroup>
<h:panelGroup styleClass="shorttext" rendered="#{SyllabusTool.calendarExistsForSite}">
<h:selectBooleanCheckbox id="linkCalendar" value="#{SyllabusTool.syllabusDataLinkCalendar}" />
Expand Down
Loading

0 comments on commit a109cc9

Please sign in to comment.