001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.web.bind;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreConstants;
020import org.kuali.rice.core.api.datetime.DateTimeService;
021import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
022import org.kuali.rice.core.api.util.RiceKeyConstants;
023import org.kuali.rice.core.web.format.FormatException;
024
025import java.beans.PropertyEditorSupport;
026import java.io.Serializable;
027import java.sql.Time;
028import java.text.ParseException;
029
030/**
031 * PropertyEditor converts between time display strings and {@code java.sql.Time} objects
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public class UifTimeEditor extends PropertyEditorSupport implements Serializable {
036
037    private static final long serialVersionUID = 3087028215717940843L;
038
039    private transient DateTimeService dateTimeService;
040
041    /**
042     * Converts the time object to the display string format.
043     *
044     * @return the time object in the display string format or null if it is empty
045     */
046    @Override
047    public String getAsText() {
048        if (getValue() == null) {
049            return null;
050        }
051
052        if (getValue() instanceof String && StringUtils.isBlank((String) getValue())) {
053            return null;
054        }
055
056        return getDateTimeService().toTimeString((Time) getValue());
057    }
058
059    /**
060     * Converts the display string to a time object.
061     */
062    @Override
063    public void setAsText(String text) throws IllegalArgumentException {
064        setValue(convertToObject(text));
065    }
066
067    /**
068     * Converts the display text to a time object.
069     *
070     * @param text the display text
071     *
072     * @return the time object
073     */
074    protected Object convertToObject(String text) {
075        if (StringUtils.isBlank(text)) {
076            return null;
077        }
078
079        try {
080            return getDateTimeService().convertToSqlTime(text);
081        } catch (ParseException e) {
082            throw new FormatException("parsing", RiceKeyConstants.ERROR_TIME, text, e);
083        }
084    }
085
086    /**
087     * Gets the date time service.
088     *
089     * @return the date time service
090     */
091    protected DateTimeService getDateTimeService() {
092        if (dateTimeService == null) {
093            dateTimeService = GlobalResourceLoader.getService(CoreConstants.Services.DATETIME_SERVICE);
094        }
095        return dateTimeService;
096    }
097
098}