com.angstoverseer.service.command.handler.impl.WeightWatchCommand.java Source code

Java tutorial

Introduction

Here is the source code for com.angstoverseer.service.command.handler.impl.WeightWatchCommand.java

Source

/* 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.angstoverseer.service.command.handler.impl;

import com.angstoverseer.model.weight.Weight;
import com.angstoverseer.persistence.weight.WeightDao;
import com.angstoverseer.service.command.handler.Command;
import com.angstoverseer.service.command.handler.dto.CommandParameters;
import com.angstoverseer.service.command.parser.CommandParser;
import com.angstoverseer.service.command.parser.dto.ParsedCommand;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WeightWatchCommand implements Command {

    @Autowired
    private WeightDao weightDao;

    @Autowired
    private CommandParser commandParser;

    @Override
    public String getName() {
        return "w";
    }

    @Override
    public String execute(CommandParameters commandParameters) {
        final ParsedCommand parsedCommand = commandParser.parse(commandParameters);

        String value = parsedCommand.getDescription().toString().trim();
        if (!NumberUtils.isNumber(value)) {
            return "Error. Not a number: " + parsedCommand.getDescription();
        }

        Weight weight = new Weight();
        weight.setValue(new Double(value));
        weight.setDateTime(parsedCommand.getDateTime());
        weightDao.save(weight);

        return "Weight watched.";
    }
}