com.hedera.hashgraph.sdk.SystemUndeleteTransaction Maven / Gradle / Ivy
Show all versions of sdk-full Show documentation
/*-
*
* Hedera Java SDK
*
* Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
*
* 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 com.hedera.hashgraph.sdk;
import com.google.protobuf.InvalidProtocolBufferException;
import com.hedera.hashgraph.sdk.proto.FileServiceGrpc;
import com.hedera.hashgraph.sdk.proto.SchedulableTransactionBody;
import com.hedera.hashgraph.sdk.proto.SmartContractServiceGrpc;
import com.hedera.hashgraph.sdk.proto.SystemUndeleteTransactionBody;
import com.hedera.hashgraph.sdk.proto.TransactionBody;
import com.hedera.hashgraph.sdk.proto.TransactionResponse;
import io.grpc.MethodDescriptor;
import java.util.LinkedHashMap;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
/**
* Undelete a file or smart contract that was deleted by AdminDelete.
*
* Can only be done with a Hedera admin.
*/
public final class SystemUndeleteTransaction extends Transaction {
@Nullable
private FileId fileId;
@Nullable
private ContractId contractId;
/**
* Constructor.
*/
public SystemUndeleteTransaction() {
}
/**
* Constructor.
*
* @param txs Compound list of transaction id's list of (AccountId, Transaction)
* records
* @throws InvalidProtocolBufferException when there is an issue with the protobuf
*/
SystemUndeleteTransaction(LinkedHashMap> txs) throws InvalidProtocolBufferException {
super(txs);
initFromTransactionBody();
}
/**
* Constructor.
*
* @param txBody protobuf TransactionBody
*/
SystemUndeleteTransaction(com.hedera.hashgraph.sdk.proto.TransactionBody txBody) {
super(txBody);
initFromTransactionBody();
}
/**
* Extract the file id.
*
* @return the file id
*/
@Nullable
public final FileId getFileId() {
return fileId;
}
/**
* Sets the file ID to undelete.
*
* Mutually exclusive with {@link #setContractId(ContractId)}.
*
* @param fileId The FileId to be set
* @return {@code this}
*/
public final SystemUndeleteTransaction setFileId(FileId fileId) {
Objects.requireNonNull(fileId);
requireNotFrozen();
this.fileId = fileId;
return this;
}
/**
* The contract ID instance to undelete, in the format used in transactions
*
* @return the contractId
*/
@Nullable
public ContractId getContractId() {
return contractId;
}
/**
* Sets the contract ID to undelete.
*
* Mutually exclusive with {@link #setFileId(FileId)}.
*
* @param contractId The ContractId to be set
* @return {@code this}
*/
public final SystemUndeleteTransaction setContractId(ContractId contractId) {
Objects.requireNonNull(contractId);
requireNotFrozen();
this.contractId = contractId;
return this;
}
/**
* Initialize from the transaction body.
*/
void initFromTransactionBody() {
var body = sourceTransactionBody.getSystemUndelete();
if (body.hasFileID()) {
fileId = FileId.fromProtobuf(body.getFileID());
}
if (body.hasContractID()) {
contractId = ContractId.fromProtobuf(body.getContractID());
}
}
/**
* Build the transaction body.
*
* @return {@link com.hedera.hashgraph.sdk.proto.SystemUndeleteTransactionBody}
*/
SystemUndeleteTransactionBody.Builder build() {
var builder = SystemUndeleteTransactionBody.newBuilder();
if (fileId != null) {
builder.setFileID(fileId.toProtobuf());
}
if (contractId != null) {
builder.setContractID(contractId.toProtobuf());
}
return builder;
}
@Override
void validateChecksums(Client client) throws BadEntityIdException {
if (fileId != null) {
fileId.validateChecksum(client);
}
if (contractId != null) {
contractId.validateChecksum(client);
}
}
@Override
CompletableFuture onExecuteAsync(Client client) {
int modesEnabled = (fileId != null ? 1 : 0) + (contractId != null ? 1 : 0);
if (modesEnabled != 1) {
throw new IllegalStateException("SystemDeleteTransaction must have exactly 1 of the following fields set: contractId, fileId");
}
return super.onExecuteAsync(client);
}
@Override
MethodDescriptor getMethodDescriptor() {
if (fileId != null) {
return FileServiceGrpc.getSystemUndeleteMethod();
} else {
return SmartContractServiceGrpc.getSystemUndeleteMethod();
}
}
@Override
void onFreeze(TransactionBody.Builder bodyBuilder) {
bodyBuilder.setSystemUndelete(build());
}
@Override
void onScheduled(SchedulableTransactionBody.Builder scheduled) {
scheduled.setSystemUndelete(build());
}
}