001/*
002 *  Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.agentsflex.core.store;
017
018import com.agentsflex.core.util.Metadata;
019import com.agentsflex.core.document.Document;
020
021import java.util.ArrayList;
022import java.util.List;
023
024public class StoreResult extends Metadata {
025    private final boolean success;
026    private Exception exception;
027    private String message;
028    private List<Object> ids;
029
030    public StoreResult(boolean success) {
031        this.success = success;
032        this.message = "";
033    }
034
035    public StoreResult(boolean success, String message) {
036        this.success = success;
037        this.message = message == null ? "" : message;
038    }
039
040    public boolean isSuccess() {
041        return success;
042    }
043
044    public String getMessage() {
045        return message;
046    }
047
048    public void setMessage(String message) {
049        this.message = message;
050    }
051
052    public List<Object> ids() {
053        return ids;
054    }
055
056    public static StoreResult fail(String message, Exception exception) {
057        StoreResult storeResult = new StoreResult(false);
058        storeResult.setMessage(message);
059        storeResult.exception = exception;
060        return storeResult;
061    }
062
063    public static StoreResult fail(String message) {
064        return new StoreResult(false, message);
065    }
066
067
068    public static StoreResult fail() {
069        return new StoreResult(false);
070    }
071
072    public static StoreResult success() {
073        return new StoreResult(true);
074    }
075
076    public static StoreResult successWithIds(List<Document> documents) {
077        StoreResult result = success();
078        result.ids = new ArrayList<>(documents.size());
079        for (Document document : documents) {
080            result.ids.add(document.getId());
081        }
082        return result;
083    }
084
085    public Exception getException() {
086        return exception;
087    }
088
089    public void setException(Exception exception) {
090        this.exception = exception;
091    }
092
093    public List<Object> getIds() {
094        return ids;
095    }
096
097    public void setIds(List<Object> ids) {
098        this.ids = ids;
099    }
100
101    @Override
102    public String toString() {
103        return "StoreResult{" +
104            "success=" + success +
105            ", exception=" + exception +
106            ", message='" + message + '\'' +
107            ", ids=" + ids +
108            ", metadataMap=" + metadataMap +
109            '}';
110    }
111}