package com.capgemini.reports.teamcenter.queries;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.reports.exceptions.NotFoundException;
import com.teamcenter.soa.client.model.ModelObject;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * Request a Teamcenter object directly using its Unique ID (UID).
 */
@Slf4j
public class DirectQuery<T extends ModelObject> {

    private final TcSession session;

    public DirectQuery(final TcSession session) {
        this.session = session;
    }

    /**
     * Direct search using a unique ID (UID).
     */
    public T searchByUID(final String objectUID) throws NotFoundException {
        List<T> modelObjects = session.loadObjects(objectUID);
        if (modelObjects.isEmpty()) {
            throw new NotFoundException("DirectQuery returned no result for UID=\"" + objectUID + "\"");
        } else {
            return modelObjects.get(0);
        }
    }
}
