Skip to content

Commit

Permalink
la-team#116 Timestamp doesn't show as Date Time, it shows time in mil…
Browse files Browse the repository at this point in the history
…liseconds
  • Loading branch information
max-dev committed Oct 3, 2014
1 parent 7667800 commit f514dcc
Show file tree
Hide file tree
Showing 21 changed files with 335 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.lightadmin.core.persistence.metamodel;

import com.google.common.collect.Sets;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
Expand All @@ -25,10 +24,11 @@

import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;
import java.util.UUID;

import static org.lightadmin.core.util.NumberUtils.isNumberFloat;
Expand All @@ -41,43 +41,108 @@ public enum PersistentPropertyType {
NUMBER_FLOAT,
BOOL,
DATE,
TIME,
DATE_TIME,
FILE,
ASSOC,
ASSOC_MULTI,
EMBEDDED,
MAP,
UNKNOWN;

public static boolean isSupportedAttributeType(PersistentPropertyType attributeType) {
return attributeType != UNKNOWN;
}
public static PersistentPropertyType forPersistentProperty(PersistentProperty persistentProperty) {
final Class<?> attrType = persistentProperty.getType();

public static PersistentPropertyType forType(Class<?> attrType) {
if (Boolean.class.equals(attrType) || boolean.class.equals(attrType)) {
return BOOL;
if (persistentProperty.isAnnotationPresent(Embedded.class) || persistentProperty.isAnnotationPresent(EmbeddedId.class)) {
return PersistentPropertyType.EMBEDDED;
}

if (persistentProperty.isAssociation()) {
if (persistentProperty.isCollectionLike()) {
return PersistentPropertyType.ASSOC_MULTI;
}
return PersistentPropertyType.ASSOC;
}

if (persistentProperty.isMap()) {
return PersistentPropertyType.MAP;
}

if (isAssignableFrom(attrType, standardDateTypes()) || isAssignableFrom(attrType, jodaTypes())) {
if (forType(attrType) == STRING && persistentProperty.isAnnotationPresent(FileReference.class)) {
return PersistentPropertyType.FILE;
}

if (isOfDateType(persistentProperty)) {
return DATE;
}

if (isNumberInteger(attrType)) {
return NUMBER_INTEGER;
if (isOfTimeType(persistentProperty)) {
return TIME;
}

if (isNumberFloat(attrType)) {
return NUMBER_FLOAT;
if (isOfDateTimeType(persistentProperty)) {
return DATE_TIME;
}

if (String.class.equals(attrType) || UUID.class.equals(attrType)) {
return STRING;
return forType(attrType);
}

private static boolean isOfTimeType(PersistentProperty persistentProperty) {
Class attrType = persistentProperty.getType();

if (java.sql.Time.class.equals(attrType)) {
return true;
}

if (byte[].class.equals(attrType)) {
return FILE;
if (LocalTime.class.equals(attrType)) {
return true;
}

return UNKNOWN;
if (Date.class.equals(attrType) || Calendar.class.equals(attrType)) {
return hasTemporalType(persistentProperty, TemporalType.TIME);
}

return false;
}

private static boolean isOfDateType(PersistentProperty persistentProperty) {
Class<?> attrType = persistentProperty.getType();

if (java.sql.Date.class.equals(attrType)) {
return true;
}

if (LocalDate.class.equals(attrType)) {
return true;
}

if (Date.class.equals(attrType) || Calendar.class.equals(attrType)) {
return hasTemporalType(persistentProperty, TemporalType.DATE);
}

return false;
}

private static boolean isOfDateTimeType(PersistentProperty persistentProperty) {
Class<?> attrType = persistentProperty.getType();

if (Timestamp.class.equals(attrType)) {
return true;
}

if (DateTime.class.equals(attrType) || LocalDateTime.class.equals(attrType)) {
return true;
}

if (Date.class.equals(attrType) || Calendar.class.equals(attrType)) {
return hasTemporalType(persistentProperty, TemporalType.TIMESTAMP);
}

return false;
}

public static boolean isSupportedAttributeType(PersistentPropertyType attributeType) {
return attributeType != UNKNOWN;
}

public static boolean isOfBinaryFileType(PersistentProperty persistentProperty) {
Expand All @@ -95,56 +160,32 @@ public static boolean isOfFileType(PersistentProperty persistentProperty) {
return isOfBinaryFileType(persistentProperty) || isOfFileReferenceType(persistentProperty);
}

public static PersistentPropertyType forPersistentProperty(PersistentProperty persistentProperty) {
final Class<?> attrType = persistentProperty.getType();

if (persistentProperty.isAnnotationPresent(Embedded.class) || persistentProperty.isAnnotationPresent(EmbeddedId.class)) {
return PersistentPropertyType.EMBEDDED;
private static PersistentPropertyType forType(Class<?> attrType) {
if (Boolean.class.equals(attrType) || boolean.class.equals(attrType)) {
return BOOL;
}

if (persistentProperty.isAssociation()) {
if (persistentProperty.isCollectionLike()) {
return PersistentPropertyType.ASSOC_MULTI;
}
return PersistentPropertyType.ASSOC;
if (isNumberInteger(attrType)) {
return NUMBER_INTEGER;
}

if (persistentProperty.isMap()) {
return PersistentPropertyType.MAP;
if (isNumberFloat(attrType)) {
return NUMBER_FLOAT;
}

if (forType(attrType) == STRING && persistentProperty.isAnnotationPresent(FileReference.class)) {
return PersistentPropertyType.FILE;
if (String.class.equals(attrType) || UUID.class.equals(attrType)) {
return STRING;
}

return forType(attrType);
}

private static boolean isAssignableFrom(Class cls, Set<Class> types) {
for (Class<?> type : types) {
if (type.isAssignableFrom(cls)) {
return true;
}
if (byte[].class.equals(attrType)) {
return FILE;
}
return false;
}

private static Set<Class> standardDateTypes() {
return Sets.<Class>newHashSet(
Date.class,
Calendar.class,
java.sql.Date.class,
java.sql.Time.class,
Timestamp.class
);
return UNKNOWN;
}

private static Set<Class> jodaTypes() {
return Sets.<Class>newHashSet(
DateTime.class,
LocalDateTime.class,
LocalDate.class,
LocalTime.class
);
private static boolean hasTemporalType(PersistentProperty persistentProperty, TemporalType temporalType) {
Temporal temporal = (Temporal) persistentProperty.findAnnotation(Temporal.class);
return temporal != null && temporal.value() == temporalType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ private Predicate attributePredicate(PersistentProperty persistentProperty, fina
return associationAttributesPredicate(persistentProperty, attributeName, parameterValues);
}

if (isDateType(persistentProperty)) {
if (isOfDateType(persistentProperty)) {
return dateAttributePredicate(attributeName, parameterValue);
}

if (isOfTimeType(persistentProperty)) {
return dateAttributePredicate(attributeName, parameterValue);
}

if (isOfDateTimeType(persistentProperty)) {
return dateAttributePredicate(attributeName, parameterValue);
}

Expand Down Expand Up @@ -185,10 +193,18 @@ private Predicate numericAttributePredicate(final PersistentProperty attribute,
return builder.and();
}

private boolean isDateType(final PersistentProperty attribute) {
private boolean isOfDateType(final PersistentProperty attribute) {
return PersistentPropertyType.forPersistentProperty(attribute) == DATE;
}

private boolean isOfTimeType(final PersistentProperty attribute) {
return PersistentPropertyType.forPersistentProperty(attribute) == TIME;
}

private boolean isOfDateTimeType(final PersistentProperty attribute) {
return PersistentPropertyType.forPersistentProperty(attribute) == DATE_TIME;
}

private boolean isAssociation(final PersistentProperty attribute) {
return PersistentPropertyType.forPersistentProperty(attribute) == ASSOC || PersistentPropertyType.forPersistentProperty(attribute) == ASSOC_MULTI;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class EditControlDispatcherTag extends SimpleTagSupport {
private JspFragment booleanEditControl;
private JspFragment fileEditControl;
private JspFragment dateEditControl;
private JspFragment timeEditControl;
private JspFragment dateTimeEditControl;
private JspFragment n2oneEditControl;
private JspFragment n2manyEditControl;
private JspFragment mapEditControl;
Expand Down Expand Up @@ -75,6 +77,12 @@ private void doWithStandardControl() throws JspException, IOException {
case DATE:
worker = dateEditControl;
break;
case TIME:
worker = timeEditControl;
break;
case DATE_TIME:
worker = dateTimeEditControl;
break;
case NUMBER_INTEGER:
worker = numberEditControl;
break;
Expand Down Expand Up @@ -137,4 +145,12 @@ public void setMapEditControl(JspFragment control) {
public void setFileEditControl(final JspFragment fileEditControl) {
this.fileEditControl = fileEditControl;
}

public void setTimeEditControl(JspFragment timeEditControl) {
this.timeEditControl = timeEditControl;
}

public void setDateTimeEditControl(JspFragment dateTimeEditControl) {
this.dateTimeEditControl = dateTimeEditControl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
<name>date-edit-control</name>
<path>/META-INF/tags/edit/date-edit-control.tag</path>
</tag-file>
<tag-file>
<name>time-edit-control</name>
<path>/META-INF/tags/edit/time-edit-control.tag</path>
</tag-file>
<tag-file>
<name>datetime-edit-control</name>
<path>/META-INF/tags/edit/datetime-edit-control.tag</path>
</tag-file>
<tag-file>
<name>n2many-edit-control</name>
<path>/META-INF/tags/edit/n2many-edit-control.tag</path>
Expand Down
10 changes: 10 additions & 0 deletions lightadmin-core/src/main/resources/META-INF/lightadmin.tld
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>timeEditControl</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>dateTimeEditControl</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
<attribute>
<name>n2oneEditControl</name>
<required>true</required>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ function LoadDomainEntityAction(resourceName) {
}
var filePropertyValue = fileUploaderController.loadFile(domainEntity.getPrimaryKeyValue());
editor.val(filePropertyValue['value']);
break;
case 'DATE_TIME':
var timeEditor = form.find('[name="' + propertyName + '_time"]');

var dateTime = new DateTime(propertyValue.toString());
editor.val(dateTime.getDate());
timeEditor.val(dateTime.getTime());

break;
case 'STRING':
if (editor.hasClass('wysiwyg')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ function decorateUIControls(container) {

$(".input-date", $(container)).datepicker({
autoSize: true,
appendText: '(YYYY-MM-DD)',
// appendText: '(YYYY-MM-DD)',
dateFormat: 'yy-mm-dd'
});

$( '.timepicker' ).timeEntry( {
show24Hours: true, // 24 hours format
showSeconds: true, // Show seconds?
spinnerImage: ApplicationConfig.BASE_URL + 'images/ui/spinnerUpDown.png', // Arrows image
spinnerSize: [17, 26, 0], // Image size
spinnerIncDecOnly: true // Only up and down arrows
} );
}

function formViewVisualDecoration(container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,23 @@ function DomainEntity(data) {
}
return this.original_properties[propertyName];
}
}

function DateTime(dateTimeValue) {

var date = '';
var time = '';

if (dateTimeValue != null && dateTimeValue.indexOf("T") > -1) {
date = dateTimeValue.split('T')[0];
time = dateTimeValue.split('T')[1].substr(0, 8);
}

this.getDate = function() {
return date;
};

this.getTime = function() {
return time;
};
}
Loading

0 comments on commit f514dcc

Please sign in to comment.