/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You 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 org.apache.geode.management.internal.cli.commands;

import javax.management.ObjectName;

import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;

import org.apache.geode.distributed.DistributedMember;
import org.apache.geode.management.MemberMXBean;
import org.apache.geode.management.cli.CliMetaData;
import org.apache.geode.management.cli.ConverterHint;
import org.apache.geode.management.cli.Result;
import org.apache.geode.management.internal.ManagementConstants;
import org.apache.geode.management.internal.SystemManagementService;
import org.apache.geode.management.internal.cli.i18n.CliStrings;
import org.apache.geode.management.internal.cli.result.ErrorResultData;
import org.apache.geode.management.internal.cli.result.InfoResultData;
import org.apache.geode.management.internal.cli.result.ResultBuilder;
import org.apache.geode.management.internal.security.ResourceOperation;
import org.apache.geode.security.ResourcePermission;

public class ShowLogCommand extends InternalGfshCommand {
  @CliCommand(value = CliStrings.SHOW_LOG, help = CliStrings.SHOW_LOG_HELP)
  @CliMetaData(relatedTopic = {CliStrings.TOPIC_GEODE_DEBUG_UTIL})
  @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER,
      operation = ResourcePermission.Operation.READ)
  public Result showLog(
      @CliOption(key = CliStrings.MEMBER, optionContext = ConverterHint.ALL_MEMBER_IDNAME,
          help = CliStrings.SHOW_LOG_MEMBER_HELP, mandatory = true) String memberNameOrId,
      @CliOption(key = CliStrings.SHOW_LOG_LINE_NUM, unspecifiedDefaultValue = "0",
          help = CliStrings.SHOW_LOG_LINE_NUM_HELP) int numberOfLines) {
    DistributedMember targetMember = getMember(memberNameOrId);
    MemberMXBean targetMemberMXBean = getMemberMxBean(targetMember);

    if (numberOfLines > ManagementConstants.MAX_SHOW_LOG_LINES) {
      numberOfLines = ManagementConstants.MAX_SHOW_LOG_LINES;
    }
    if (numberOfLines == 0 || numberOfLines < 0) {
      numberOfLines = ManagementConstants.DEFAULT_SHOW_LOG_LINES;
    }
    InfoResultData resultData = ResultBuilder.createInfoResultData();
    if (targetMemberMXBean != null) {
      String log = targetMemberMXBean.showLog(numberOfLines);
      if (log != null) {
        resultData.addLine(log);
      } else {
        resultData.addLine(CliStrings.SHOW_LOG_NO_LOG);
      }
    } else {
      ErrorResultData errorResultData =
          ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT)
              .addLine(memberNameOrId + CliStrings.SHOW_LOG_MSG_MEMBER_NOT_FOUND);
      return (ResultBuilder.buildResult(errorResultData));
    }

    return ResultBuilder.buildResult(resultData);
  }

  public MemberMXBean getMemberMxBean(DistributedMember targetMember) {
    SystemManagementService service = (SystemManagementService) getManagementService();

    if (getCache().getDistributedSystem().getDistributedMember().equals(targetMember)) {
      return service.getMemberMXBean();
    } else {
      ObjectName objectName = service.getMemberMBeanName(targetMember);
      return service.getMBeanProxy(objectName, MemberMXBean.class);
    }
  }
}

@@ -1252,22 +1252,27 @@
 ent.cli.
-Result
+GfshCommand
 ;%0Aimport
@@ -1523,79 +1523,13 @@
 ult.
-ErrorResultData;%0Aimport org.apache.geode.management.internal.cli.result
+model
 .Inf
@@ -1535,20 +1535,21 @@
 foResult
-Data
+Model
 ;%0Aimport
@@ -1597,29 +1597,33 @@
 .result.
+model.
 Result
-Builder
+Model
 ;%0Aimport
@@ -1782,16 +1782,8 @@
 nds 
-Internal
 Gfsh
@@ -2072,16 +2072,21 @@
 c Result
+Model
  showLog
@@ -2690,24 +2690,25 @@
 INES;%0A    %7D%0A
+%0A
     if (numb
@@ -2822,74 +2822,100 @@
   %7D%0A
+%0A
     
-Info
 Result
-Data
+Model
  result
-Data = ResultBuilder.createInfoResultData
+ = new ResultModel();%0A    InfoResultModel resultInfo = result.addInfo
 ();%0A
@@ -3045,36 +3045,36 @@
 %7B%0A        result
-Data
+Info
 .addLine(log);%0A 
@@ -3101,20 +3101,20 @@
   result
-Data
+Info
 .addLine
@@ -3174,65 +3174,26 @@
     
-ErrorResultData errorResultData =%0A         
+return
  Result
-Builder
+Model
 .cre
@@ -3204,264 +3204,128 @@
 rror
-ResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT)%0A              .addLine(memberNameOrId + CliStrings.SHOW_LOG_MSG_MEMBER_NOT_FOUND);%0A      return (ResultBuilder.buildResult(errorResultData));%0A    %7D%0A%0A    return ResultBuilder.buildResult(resultData)
+(%0A          String.format(%22%25s: %25s%22, CliStrings.SHOW_LOG_MSG_MEMBER_NOT_FOUND, memberNameOrId));%0A    %7D%0A%0A    return result
 ;%0A  
@@ -3440,34 +3440,8 @@
 ce =
- (SystemManagementService)
  get

Refactor ShowLogCommand to extend GfshCommand and return ResultModel (#2927)