Package com.sonyericsson.hudson.plugins.metadata.model.definitions

Source Code of com.sonyericsson.hudson.plugins.metadata.model.definitions.DateMetadataDefinition$DateMetaDataDefinitionDescriptor

/*
*  The MIT License
*
*  Copyright 2011 Sony Ericsson Mobile Communications. All rights reserved.
*  Copyright 2012 Sony Mobile Communications AB. All rights reserved.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  The above copyright notice and this permission notice shall be included in
*  all copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/
package com.sonyericsson.hudson.plugins.metadata.model.definitions;

import com.sonyericsson.hudson.plugins.metadata.Messages;
import com.sonyericsson.hudson.plugins.metadata.model.TimeDetails;
import com.sonyericsson.hudson.plugins.metadata.model.values.AbstractMetadataValue;
import com.sonyericsson.hudson.plugins.metadata.model.values.DateMetadataValue;
import hudson.Extension;
import hudson.model.Descriptor;
import com.sonyericsson.hudson.plugins.metadata.model.MetadataChecks;
import hudson.util.FormValidation;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.Calendar;

import static com.sonyericsson.hudson.plugins.metadata.Constants.DEFAULT_MONTH_ADJUSTMENT;
import static com.sonyericsson.hudson.plugins.metadata.Constants.DEFAULT_TIME_DETAILS;

/**
* Meta data with the value of a {@link java.util.Date}.
*
* @author Tomas Westling <thomas.westling@sonyericsson.com>
*/
public class DateMetadataDefinition extends AbstractMetadataDefinition {

    private Calendar defaultCal;
    private boolean checked = false;

    /**
     * Getter for the defaultYear.
     *
     * @return the default year.
     */
    public int getYear() {
        return defaultCal.get(Calendar.YEAR);
    }

    /**
     * Getter for the defaultMonth.
     *
     * @return the default month of the year.
     */
    public int getMonth() {
        return defaultCal.get(Calendar.MONTH) + DEFAULT_MONTH_ADJUSTMENT;
    }

    /**
     * Getter for the defaultDay.
     *
     * @return the default day of the month.
     */
    public int getDay() {
        return defaultCal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * Getter for the default hour.
     *
     * @return the default hour of the day..
     */
    public int getHour() {
        return defaultCal.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * Getter for the default minute.
     *
     * @return the default minute of the hour.
     */
    public int getMinute() {
        return defaultCal.get(Calendar.MINUTE);
    }

    /**
     * Getter for the default second.
     *
     * @return the default second.
     */
    public int getSecond() {
        return defaultCal.get(Calendar.SECOND);
    }

    /**
     * Returns the checked value, used to decide if the time details should be visible.
     *
     * @return the checked value.
     */
    public boolean isChecked() {
        return checked;
    }


    /**
     * Standard Constructor.
     *
     * @param name         the name
     * @param year  the default year.
     * @param month the default month of the year.
     * @param day   the default day of the month.
     * @param description  the description.
     * @param details      the optional time details, hour/minute/second.
     * @param exposedToEnvironment if values of this definition should be exposed to the build as an
     *                      environment variable.
     */
    @DataBoundConstructor
    public DateMetadataDefinition(String name, String description, int year, int month,
                                  int day, TimeDetails details, boolean exposedToEnvironment) {
        super(name, description, exposedToEnvironment);
        defaultCal = Calendar.getInstance();


        if (details != null) {
            defaultCal.set(year, month - DEFAULT_MONTH_ADJUSTMENT, day,
                    details.getHour(), details.getMinute(), details.getSecond());
            checked = details.isChecked();
        } else {
            defaultCal.set(year, month - DEFAULT_MONTH_ADJUSTMENT, day,
                    DEFAULT_TIME_DETAILS, DEFAULT_TIME_DETAILS, DEFAULT_TIME_DETAILS);
        }
        defaultCal.set(Calendar.MILLISECOND, 0);
    }

    @Override
    public synchronized Calendar getDefaultValue() {
        return (Calendar)defaultCal.clone();
    }

    @Override
    public AbstractMetadataValue createValue(Object o) throws Descriptor.FormException {
        boolean detailsCheck = false;
        DateMetadataValue dateMetadataValue;
        Calendar cal = Calendar.getInstance();
        MetadataChecks checks = new MetadataChecks();

        if (o instanceof JSONObject) {
            String day = "";
            String month = "";
            String year = "";
            JSONObject jsonObject = (JSONObject)o;
            if (jsonObject.has("day")) {
                day = jsonObject.getString("day");
            }
            if (jsonObject.has("month")) {
                month = jsonObject.getString("month");
            }
            if (jsonObject.has("year")) {
                year = jsonObject.getString("year");
            }
            FormValidation dateValidation = checks.doCheckDateValue(year, month, day);
            if (!dateValidation.equals(FormValidation.ok())) {
                throw new Descriptor.FormException("Wrong date format " + dateValidation.getMessage(), "");
            }
            cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
            cal.set(Calendar.MONTH, Integer.parseInt(month) - DEFAULT_MONTH_ADJUSTMENT);
            cal.set(Calendar.YEAR, Integer.parseInt(year));


            if (jsonObject.has("details")) {
                String hour = "";
                String minute = "";
                String second = "";
                detailsCheck = true;
                JSONObject details = jsonObject.getJSONObject("details");
                if (details.has("hour")) {
                    hour = details.getString("hour");
                }
                if (details.has("minute")) {
                    minute = details.getString("minute");
                }
                if (details.has("second")) {
                    second = details.getString("second");
                }
                FormValidation timeValidation = checks.doCheckTimeValue(hour, minute, second);
                if (!timeValidation.equals(FormValidation.ok())) {
                    throw new Descriptor.FormException("Wrong time format: " + timeValidation.getMessage(), "");
                }
                cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
                cal.set(Calendar.MINUTE, Integer.parseInt(minute));
                cal.set(Calendar.SECOND, Integer.parseInt(second));
            } else {
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
            }
            cal.set(Calendar.MILLISECOND, 0);
            dateMetadataValue = new DateMetadataValue(getName(), getDescription(),
                    cal, detailsCheck, isExposedToEnvironment());
        } else {
            //maybe we should throw an exception here instead of going with default.
            dateMetadataValue = new DateMetadataValue(getName(), getDescription(),
                    getDefaultValue(), detailsCheck, isExposedToEnvironment());
        }
        return dateMetadataValue;
    }

    /**
     * Descriptor for {@link DateMetadataDefinition}s.
     */
    @Extension
    public static class DateMetaDataDefinitionDescriptor extends AbstractMetaDataDefinitionDescriptor {

        @Override
        public String getDisplayName() {
            return Messages.DateMetadataDefinition_DisplayName();
        }
    }
}
TOP

Related Classes of com.sonyericsson.hudson.plugins.metadata.model.definitions.DateMetadataDefinition$DateMetaDataDefinitionDescriptor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.