cz.muni.fi.pa165.creatures.rest.client.utils.DTOBuilder.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.pa165.creatures.rest.client.utils.DTOBuilder.java

Source

/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.rest.client.utils;

import cz.muni.fi.pa165.creatures.dto.AbstractDTO;
import cz.muni.fi.pa165.creatures.dto.RegionDTO;
import cz.muni.fi.pa165.creatures.dto.WeaponDTO;
import org.apache.commons.cli.CommandLine;

/**
 * This class is providing an easy way how to get DTO object from the values we
 * entered on the command line.
 *
 * @author xmiklos1
 */
public class DTOBuilder {

    public static AbstractDTO get(Class<?> clazz, CommandLine line) {
        if (clazz.equals(WeaponDTO.class)) {
            WeaponDTO dto = new WeaponDTO();
            if (line.hasOption("n")) {
                dto.setName(line.getOptionValue("n"));
            }
            if (line.hasOption("m")) {
                dto.setAmmunition(new Integer(line.getOptionValue("m")));
            }
            if (line.hasOption("g")) {
                dto.setRange(new Integer(line.getOptionValue("g")));
            }
            if (line.hasOption("i")) {
                dto.setId(line.getOptionValue("i"));
            }
            return dto;
        } else if (clazz.equals(RegionDTO.class)) {
            RegionDTO dto = new RegionDTO();
            if (line.hasOption("n")) {
                dto.setName(line.getOptionValue("n"));
            }
            if (line.hasOption("d")) {
                dto.setDescription(line.getOptionValue("d"));
            }
            if (line.hasOption("a")) {
                dto.setArea(Long.parseLong(line.getOptionValue("a")));
            }
            if (line.hasOption("i")) {
                dto.setId(line.getOptionValue("i"));
            }
            return dto;
        } else {
            return null;
        }
    }
}