All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.schoellerfamily.gedbrowser.api.crud.UpdateOperations Maven / Gradle / Ivy

There is a newer version: 1.3.0-RC2
Show newest version
package org.schoellerfamily.gedbrowser.api.crud;

import org.schoellerfamily.gedbrowser.api.datamodel.ApiObject;
import org.schoellerfamily.gedbrowser.api.transformers.ApiModelToGedObjectVisitor;
import org.schoellerfamily.gedbrowser.datamodel.GedObject;
import org.schoellerfamily.gedbrowser.datamodel.util.GedObjectBuilder;
import org.schoellerfamily.gedbrowser.persistence.domain.GedDocument;
import org.schoellerfamily.gedbrowser.persistence.domain.RootDocument;
import org.schoellerfamily.gedbrowser.persistence.mongo.gedconvert.GedObjectToGedDocumentMongoConverter;
import org.schoellerfamily.gedbrowser.persistence.repository.FindableDocument;
import org.springframework.data.repository.CrudRepository;

/**
 * This interface contains default methods that implement the update
 * operations for the classes that declare implementing the interface.
 *
 * @author Dick Schoeller
 *
 * @param  the data model type we are manipulating
 * @param  the DB type associated with the type X
 * @param  the Api type associated with the type X
 */
@SuppressWarnings("PMD.CommentSize")
public interface UpdateOperations,
        Z extends ApiObject>
    extends Converter {
    /**
     * @return the DB repository for this type
     */
    FindableDocument getRepository();

    /**
     * @return the converter
     */
    GedObjectToGedDocumentMongoConverter getConverter();

    /**
     * Implements updating an object that already exists.
     *
     * @param root the root of the db
     * @param in the requested object to update
     * @return the actual created object
     */
    default Z update(final RootDocument root, final Z in) {
        final ApiModelToGedObjectVisitor visitor =
                new ApiModelToGedObjectVisitor(
                        new GedObjectBuilder(root.getGedObject()),
                        root.getGedObject());
        in.accept(visitor);
        @SuppressWarnings("unchecked")
        final X gob = (X) visitor.getGedObject();
        return convert(update(gob));
    }

    /**
     * Save a GedObject to the database.
     *
     * @param gob the GedObject to update
     * @return the updated document
     */
    @SuppressWarnings("unchecked")
    default Y update(X gob) {
        Y document = (Y) getConverter().createGedDocument(gob);
        try {
            final FindableDocument repo = getRepository();
            Y oldDoc = repo.findByFileAndString(
                    gob.getFilename(), gob.getString());
            document.setIdString(oldDoc.getIdString());
            return ((CrudRepository) repo)
                    .save(document);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param db the dataset
     * @param id the ID of the thing to update
     * @param object the new data
     * @return the updated object
     */
    Z updateOne(String db, String id, Z object);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy