/** {@inheritDoc} */
  @Override
  public void updateFarm(Long farmNo, String comment, String domainName) {
    // 引数チェック
    if (farmNo == null) {
      throw new AutoApplicationException("ECOMMON-000003", "farmNo");
    }

    // TODO: 長さチェック(comment,domainName)

    // TODO: 形式チェック(domainName)

    // ファームの存在チェック
    Farm farm = farmDao.read(farmNo);
    if (farm == null) {
      // ファームが存在しない場合
      throw new AutoApplicationException("ESERVICE-000204", farmNo);
    }

    if (!farm.getDomainName().equals(domainName)) {
      // ドメイン名を変更しようとした場合

      // 全てのインスタンスが停止状態であることのチェック
      List<Instance> instances = instanceDao.readByFarmNo(farmNo);
      for (Instance instance : instances) {
        if (InstanceStatus.fromStatus(instance.getStatus()) != InstanceStatus.STOPPED) {
          // インスタンスが停止状態でない場合
          throw new AutoApplicationException("ESERVICE-000205", instance.getInstanceName());
        }
      }

      // ドメイン名の一意チェック
      // TODO: データベース上で一意キーを設定するべきか?
      List<Farm> checkFarms = farmDao.readAll();
      for (Farm checkFarm : checkFarms) {
        if (checkFarm.getDomainName().equals(domainName)) {
          // 同名のドメイン名が存在する場合
          throw new AutoApplicationException("ESERVICE-000206", domainName);
        }
      }
    }

    // インスタンスの更新
    farm.setComment(comment);
    farm.setDomainName(domainName);
    farmDao.update(farm);

    // イベントログ出力
    eventLogger.log(
        EventLogLevel.INFO,
        farmNo,
        farm.getFarmName(),
        null,
        null,
        null,
        null,
        "FarmUpdate",
        null,
        null,
        null);
  }
    public void setItem(InstanceDto instanceDto) {
      int line = 0;

      if (instanceDto != null) {
        Instance instance = instanceDto.getInstance();
        //                hostName = new Label(instance.getInstanceName(), Label.CONTENT_TEXT);
        //                layout.removeComponent( 1, line );
        //                layout.addComponent(hostName , 1, line++ );

        fqdn = new Label(instance.getFqdn(), Label.CONTENT_TEXT);
        layout.removeComponent(1, line);
        layout.addComponent(fqdn, 1, line++);

        ipAddress = new Label(instance.getPublicIp(), Label.CONTENT_TEXT);
        layout.removeComponent(1, line);
        layout.addComponent(ipAddress, 1, line++);

        // プラットフォームの表示
        PlatformDto platformDto = instanceDto.getPlatform();

        // TODO: アイコン名の取得ロジックのリファクタリング
        Icons icon = Icons.NONE;
        if ("aws".equals(platformDto.getPlatform().getPlatformType())) {
          if (platformDto.getPlatformAws().getEuca()) {
            icon = Icons.EUCALYPTUS;
          } else {
            icon = Icons.AWS;
          }
        } else if ("vmware".equals(platformDto.getPlatform().getPlatformType())) {
          icon = Icons.VMWARE;
        } else if ("nifty".equals(platformDto.getPlatform().getPlatformType())) {
          icon = Icons.NIFTY;
        } else if ("cloudstack".equals(platformDto.getPlatform().getPlatformType())) {
          icon = Icons.CLOUD_STACK;
        }

        String description = platformDto.getPlatform().getPlatformNameDisp();
        platform =
            new Label(
                "<img src=\""
                    + VaadinUtils.getIconPath(ServerDescBasic.this, icon)
                    + "\"><div>"
                    + description
                    + "</div>",
                Label.CONTENT_XHTML);
        layout.removeComponent(1, line);
        layout.addComponent(platform, 1, line++);

        // OS名の表示
        ImageDto imageDto = instanceDto.getImage();
        // TODO: アイコン名取得ロジックのリファクタリング
        String os = imageDto.getImage().getOsDisp();
        Icons osIcon = Icons.NONE;
        if (imageDto.getImage().getOs().startsWith("centos")) {
          osIcon = Icons.CENTOS;
        } else if (imageDto.getImage().getOs().startsWith("windows")) {
          osIcon = Icons.WINDOWS;
        }
        layoutOsType = new CssLayout();
        ostype =
            new Label(
                "<img src=\""
                    + VaadinUtils.getIconPath(ServerDescBasic.this, osIcon)
                    + "\"><div>"
                    + os
                    + "</div>",
                Label.CONTENT_XHTML);
        layoutOsType.setSizeFull();
        layoutOsType.setMargin(false);
        layoutOsType.addComponent(ostype);

        // OSがWindowsの場合パスワード取得ボタンを表示
        if (imageDto.getImage().getOs().startsWith("windows")) {
          // ただしEUCALYPTUSは除外
          if (platformDto.getPlatformAws() == null
              || platformDto.getPlatformAws().getEuca() == false) {
            InstanceStatus instanceStatus = InstanceStatus.fromStatus(instance.getStatus());
            if (instanceStatus == InstanceStatus.RUNNING) {
              getPassword.setEnabled(true);
              getPassword.setData(instance.getInstanceNo());
            } else {
              getPassword.setEnabled(false);
            }
            layoutOsType.addComponent(getPassword);
            layoutOsType.setHeight("60px");
          }
        }

        layout.removeComponent(1, line);
        layout.addComponent(layoutOsType, 1, line++);

        // ステータスの表示
        String stat =
            instance.getStatus().substring(0, 1).toUpperCase()
                + instance.getStatus().substring(1).toLowerCase();
        Icons icon2 = Icons.fromName(stat);

        status =
            new Label(
                "<img src=\""
                    + VaadinUtils.getIconPath(ServerDescBasic.this, icon2)
                    + "\"><div>"
                    + stat
                    + "</div>",
                Label.CONTENT_XHTML);
        layout.removeComponent(1, line);
        layout.addComponent(status, 1, line++);

        // コメント
        comment = new Label(instance.getComment(), Label.CONTENT_XHTML);
        layout.removeComponent(1, line);
        layout.addComponent(comment, 1, line++);

      } else {
        //                hostName = new Label("", Label.CONTENT_TEXT);
        //                layout.removeComponent( 1, line );
        //                layout.addComponent(hostName , 1, line++ );

        fqdn = new Label("", Label.CONTENT_TEXT);
        layout.removeComponent(1, line);
        layout.addComponent(fqdn, 1, line++);

        ipAddress = new Label("", Label.CONTENT_TEXT);
        layout.removeComponent(1, line);
        layout.addComponent(ipAddress, 1, line++);

        // プラットフォームの表示
        platform = new Label("", Label.CONTENT_XHTML);
        layout.removeComponent(1, line);
        layout.addComponent(platform, 1, line++);

        // OS名の表示
        layoutOsType = new CssLayout();
        ostype = new Label("", Label.CONTENT_XHTML);
        layoutOsType.setSizeFull();
        layoutOsType.setMargin(false);
        layoutOsType.addComponent(ostype);
        layout.removeComponent(1, line);
        layout.addComponent(layoutOsType, 1, line++);

        // ステータスの表示
        status = new Label("", Label.CONTENT_XHTML);
        layout.removeComponent(1, line);
        layout.addComponent(status, 1, line++);

        // コメント
        comment = new Label("", Label.CONTENT_XHTML);
        layout.removeComponent(1, line);
        layout.addComponent(comment, 1, line++);
      }
    }
  /** {@inheritDoc} */
  @Override
  public void deleteFarm(Long farmNo) {
    // 引数チェック
    if (farmNo == null) {
      throw new AutoApplicationException("ECOMMON-000003", "farmNo");
    }

    // ファームの存在チェック
    Farm farm = farmDao.read(farmNo);
    if (farm == null) {
      // ファームが存在しない場合
      return;
    }

    // コンポーネントが停止しているかどうかのチェック
    List<Component> components = componentDao.readByFarmNo(farmNo);
    for (Component component : components) {
      // ロードバランサのコンポーネントはチェックしない
      if (BooleanUtils.isTrue(component.getLoadBalancer())) {
        continue;
      }

      List<ComponentInstance> componentInstances =
          componentInstanceDao.readByComponentNo(component.getComponentNo());
      for (ComponentInstance componentInstance : componentInstances) {
        ComponentInstanceStatus status =
            ComponentInstanceStatus.fromStatus(componentInstance.getStatus());
        if (status != ComponentInstanceStatus.STOPPED) {
          // コンポーネントが停止状態でない場合
          throw new AutoApplicationException("ESERVICE-000202", component.getComponentName());
        }
      }
    }

    // インスタンスが停止しているかどうかのチェック
    List<Instance> instances = instanceDao.readByFarmNo(farmNo);
    for (Instance instance : instances) {
      // ロードバランサのインスタンスはチェックしない
      if (BooleanUtils.isTrue(instance.getLoadBalancer())) {
        continue;
      }

      if (InstanceStatus.fromStatus(instance.getStatus()) != InstanceStatus.STOPPED) {
        // インスタンスが停止状態でない場合
        throw new AutoApplicationException("ESERVICE-000203", instance.getInstanceName());
      }
    }

    // ロードバランサが停止しているかどうかのチェック
    List<LoadBalancer> loadBalancers = loadBalancerDao.readByFarmNo(farmNo);
    for (LoadBalancer loadBalancer : loadBalancers) {
      if (LoadBalancerStatus.fromStatus(loadBalancer.getStatus()) != LoadBalancerStatus.STOPPED) {
        // ロードバランサが停止状態でない場合
        throw new AutoApplicationException("ESERVICE-000207", loadBalancer.getLoadBalancerName());
      }
    }

    // ホストグループの削除処理
    Boolean useZabbix = BooleanUtils.toBooleanObject(Config.getProperty("zabbix.useZabbix"));
    if (BooleanUtils.isTrue(useZabbix)) {
      zabbixHostProcess.deleteFarmHostgroup(farmNo);
    }

    // ロードバランサの削除処理
    for (LoadBalancer loadBalancer : loadBalancers) {
      loadBalancerService.deleteLoadBalancer(loadBalancer.getLoadBalancerNo());
    }

    // コンポーネントの削除処理
    for (Component component : components) {
      componentService.deleteComponent(component.getComponentNo());
    }

    // インスタンスの削除処理
    for (Instance instance : instances) {
      instanceService.deleteInstance(instance.getInstanceNo());
    }

    // AWS関連の削除処理
    // AWSボリュームの削除処理
    // TODO: ボリューム自体の削除処理を別で行うようにする
    List<AwsVolume> awsVolumes = awsVolumeDao.readByFarmNo(farmNo);
    for (AwsVolume awsVolume : awsVolumes) {
      if (StringUtils.isEmpty(awsVolume.getVolumeId())) {
        continue;
      }

      IaasGatewayWrapper gateway =
          iaasGatewayFactory.createIaasGateway(farm.getUserNo(), awsVolume.getPlatformNo());
      try {
        // ボリュームの削除
        gateway.deleteVolume(awsVolume.getVolumeId());

        // EC2ではDeleteVolumeに時間がかかるため、Waitしない
        // awsProcessClient.waitDeleteVolume(volumeId);
      } catch (AutoException ignore) {
        // ボリュームが存在しない場合などに備えて例外を握りつぶす
      }
    }
    awsVolumeDao.deleteByFarmNo(farmNo);

    // VMware関連の削除処理
    // VLANの割り当てを解除
    List<VmwareNetwork> vmwareNetworks = vmwareNetworkDao.readByFarmNo(farmNo);
    for (VmwareNetwork vmwareNetwork : vmwareNetworks) {
      // PortGroupを削除
      VmwareProcessClient vmwareProcessClient =
          vmwareProcessClientFactory.createVmwareProcessClient(vmwareNetwork.getPlatformNo());
      try {
        vmwareNetworkProcess.removeNetwork(vmwareProcessClient, vmwareNetwork.getNetworkNo());
      } finally {
        vmwareProcessClient.getVmwareClient().logout();
      }

      // VLAN割り当てを解除
      vmwareNetwork.setFarmNo(null);
      vmwareNetworkDao.update(vmwareNetwork);
    }

    // ユーザ権限の削除
    userAuthDao.deleteByFarmNo(farmNo);

    // ファームの削除処理
    farmDao.deleteByFarmNo(farmNo);

    // イベントログ出力
    eventLogger.log(
        EventLogLevel.INFO,
        farmNo,
        farm.getFarmName(),
        null,
        null,
        null,
        null,
        "FarmDelete",
        null,
        null,
        null);
  }