Search in sources :

Example 11 with UserSessionSource

use of com.haulmont.cuba.core.global.UserSessionSource in project cuba by cuba-platform.

the class WebDatePicker method setDateRangeByProperty.

protected void setDateRangeByProperty(MetaProperty metaProperty) {
    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
    if (metaProperty.getAnnotations().get(Past.class.getName()) != null) {
        TimeSource timeSource = AppBeans.get(TimeSource.NAME);
        Date currentTimestamp = timeSource.currentTimestamp();
        Calendar calendar = Calendar.getInstance(sessionSource.getLocale());
        calendar.setTime(currentTimestamp);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        setRangeEnd(calendar.getTime());
    } else if (metaProperty.getAnnotations().get(Future.class.getName()) != null) {
        TimeSource timeSource = AppBeans.get(TimeSource.NAME);
        Date currentTimestamp = timeSource.currentTimestamp();
        Calendar calendar = Calendar.getInstance(sessionSource.getLocale());
        calendar.setTime(currentTimestamp);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DATE, 1);
        setRangeStart(calendar.getTime());
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) Calendar(java.util.Calendar) Future(javax.validation.constraints.Future) TimeSource(com.haulmont.cuba.core.global.TimeSource) Date(java.util.Date)

Example 12 with UserSessionSource

use of com.haulmont.cuba.core.global.UserSessionSource in project cuba by cuba-platform.

the class FileDownloadHelper method makeUrl.

/**
 * Creates URL for downloading a file.
 *
 * @param fd            file descriptor
 * @param attachment    sets Content-Disposition: attachment
 * @return URL string
 */
public static String makeUrl(FileDescriptor fd, boolean attachment) {
    StringBuilder sb = new StringBuilder();
    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
    sb.append("dispatch/download?").append("s=").append(sessionSource.getUserSession().getId()).append("&").append("f=").append(fd.getId());
    if (attachment)
        sb.append("&a=true");
    return sb.toString();
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource)

Example 13 with UserSessionSource

use of com.haulmont.cuba.core.global.UserSessionSource in project cuba by cuba-platform.

the class WindowCreationHelper method applyUiPermissions.

/**
 * Apply UI permissions to a frame.
 *
 * @param container frame
 */
public static void applyUiPermissions(Frame container) {
    Window window = container instanceof Window ? (Window) container : ComponentsHelper.getWindow(container);
    if (window == null) {
        log.warn(String.format("Unable to find window for container %s with id '%s'", container.getClass(), container.getId()));
        return;
    }
    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
    UserSession userSession = sessionSource.getUserSession();
    String screenId = window.getId();
    Map<String, Integer> uiPermissions = userSession.getPermissionsByType(PermissionType.UI);
    for (Map.Entry<String, Integer> permissionEntry : uiPermissions.entrySet()) {
        String target = permissionEntry.getKey();
        String targetComponentId = getTargetComponentId(target, screenId);
        if (targetComponentId != null) {
            if (targetComponentId.contains("[")) {
                applyCompositeComponentPermission(window, screenId, permissionEntry.getValue(), targetComponentId);
            } else if (targetComponentId.contains(">")) {
                applyComponentActionPermission(window, screenId, permissionEntry.getValue(), targetComponentId);
            } else {
                applyComponentPermission(window, screenId, permissionEntry.getValue(), targetComponentId);
            }
        }
    }
}
Also used : Window(com.haulmont.cuba.gui.components.Window) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) UserSession(com.haulmont.cuba.security.global.UserSession) Map(java.util.Map)

Example 14 with UserSessionSource

use of com.haulmont.cuba.core.global.UserSessionSource in project cuba by cuba-platform.

the class ListEditorHelper method getValueCaption.

public static String getValueCaption(Object v, ListEditor.ItemType itemType, TimeZone timeZone) {
    if (v == null)
        return null;
    switch(itemType) {
        case ENTITY:
            if (v instanceof Instance)
                return ((Instance) v).getInstanceName();
            else
                return v.toString();
        case STRING:
            return (String) v;
        case DATE:
            return Datatypes.getNN(java.sql.Date.class).format(v);
        case DATETIME:
            UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
            UserSession userSession = userSessionSource.getUserSession();
            if (timeZone != null) {
                return ((TimeZoneAwareDatatype) Datatypes.getNN(Date.class)).format(v, userSession.getLocale(), timeZone);
            } else {
                return Datatypes.getNN(Date.class).format(v, userSession.getLocale());
            }
        case INTEGER:
            return Datatypes.getNN(Integer.class).format(v);
        case LONG:
            return Datatypes.getNN(Long.class).format(v);
        case BIGDECIMAL:
            return Datatypes.getNN(BigDecimal.class).format(v);
        case DOUBLE:
            return Datatypes.getNN(Double.class).format(v);
        case ENUM:
            return AppBeans.get(Messages.class).getMessage((Enum) v);
        case UUID:
            return Datatypes.getNN(java.util.UUID.class).format(v);
        default:
            throw new IllegalStateException("Unknown item type");
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) TimeZoneAwareDatatype(com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype) Messages(com.haulmont.cuba.core.global.Messages) Instance(com.haulmont.chile.core.model.Instance) Date(java.util.Date) BigDecimal(java.math.BigDecimal) UserSession(com.haulmont.cuba.security.global.UserSession)

Example 15 with UserSessionSource

use of com.haulmont.cuba.core.global.UserSessionSource in project cuba by cuba-platform.

the class DateValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    if (value == null)
        return;
    boolean result;
    if (value instanceof String) {
        try {
            Datatype datatype = Datatypes.getNN(java.sql.Date.class);
            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
            datatype.parse((String) value, sessionSource.getLocale());
            result = true;
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = value instanceof Date;
    }
    if (!result) {
        String msg = message != null ? messages.getTools().loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ValidationException(com.haulmont.cuba.gui.components.ValidationException) ParseException(java.text.ParseException) Date(java.util.Date) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Aggregations

UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)16 Datatype (com.haulmont.chile.core.datatypes.Datatype)5 ParseException (java.text.ParseException)5 ValidationException (com.haulmont.cuba.gui.components.ValidationException)4 Date (java.util.Date)3 Instance (com.haulmont.chile.core.model.Instance)2 Range (com.haulmont.chile.core.model.Range)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 BigDecimal (java.math.BigDecimal)2 MetaProperty (com.haulmont.chile.core.annotations.MetaProperty)1 TimeZoneAwareDatatype (com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 Executions (com.haulmont.cuba.core.app.execution.Executions)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Messages (com.haulmont.cuba.core.global.Messages)1 TimeSource (com.haulmont.cuba.core.global.TimeSource)1 TreeModelAdapter (com.haulmont.cuba.desktop.gui.data.TreeModelAdapter)1 AbstractAction (com.haulmont.cuba.gui.components.AbstractAction)1 Action (com.haulmont.cuba.gui.components.Action)1