/*
 * Copyright 2013 Franklin Bristow <franklin.bristow@phac-aspc.gc.ca>.
 *
 * 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 ca.corefacility.bioinformatics.irida.service.impl;

import ca.corefacility.bioinformatics.irida.exceptions.InvalidPropertyException;
import ca.corefacility.bioinformatics.irida.model.Project;
import ca.corefacility.bioinformatics.irida.model.Sample;
import ca.corefacility.bioinformatics.irida.model.SequenceFile;
import ca.corefacility.bioinformatics.irida.model.roles.impl.Identifier;
import ca.corefacility.bioinformatics.irida.repositories.CRUDRepository;
import ca.corefacility.bioinformatics.irida.repositories.SequenceFileRepository;
import com.google.common.collect.ImmutableMap;

import javax.validation.Validator;
import java.util.List;
import java.util.Map;

/**
 * Implementation for managing {@link SequenceFile}.
 *
 * @author Franklin Bristow <franklin.bristow@phac-aspc.gc.ca>
 */
public class SequenceFileServiceImpl extends CRUDServiceImpl<Identifier, SequenceFile> {

    private CRUDRepository<Identifier, SequenceFile> fileRepository;

    /**
     * Constructor.
     *
     * @param sequenceFileRepository the sequence file repository.
     * @param validator              validator.
     */
    public SequenceFileServiceImpl(
            SequenceFileRepository sequenceFileRepository,
            CRUDRepository<Identifier, SequenceFile> fileRepository,
            Validator validator) {
        super(sequenceFileRepository, validator, SequenceFile.class);
        this.fileRepository = fileRepository;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SequenceFile create(SequenceFile sequenceFile) {
        // Send the file to the database repository to be stored (in super)
        sequenceFile = super.create(sequenceFile);
        // Then store the file in an appropriate directory
        sequenceFile = fileRepository.create(sequenceFile);
        // And finally, update the database with the stored file location

        sequenceFile = super.update(sequenceFile.getIdentifier(),
                ImmutableMap.of("file", (Object) sequenceFile.getFile()));
        return sequenceFile;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SequenceFile update(Identifier id, Map<String, Object> updatedFields) throws InvalidPropertyException {
        SequenceFile updated = super.update(id, updatedFields);

        if (updatedFields.containsKey("file")) {
            updated = fileRepository.update(id, updatedFields);
            updated = super.update(id, ImmutableMap.of("file",
                    (Object) updated.getFile()));
        }

        return updated;
    }

    public void addFileToProject(Project project, SequenceFile file) {
        sequenceFileRepository().addFileToProject(project, file);
    }

    public void addFileToSample(Sample sample, SequenceFile file) {
        sequenceFileRepository().addFileToSample(sample, file);
    }

    public List<SequenceFile> getFilesForSample(Sample sample) {
        return sequenceFileRepository().getFilesForSample(sample);
    }

    public List<SequenceFile> getFilesForProject(Project project) {
        return sequenceFileRepository().getFilesForProject(project);
    }

    public SequenceFileRepository sequenceFileRepository() {
        return (SequenceFileRepository) repository;
    }

}

@@ -1171,32 +1171,105 @@
 FileRepository;%0A
+import ca.corefacility.bioinformatics.irida.service.SequenceFileService;%0A
 import com.googl
@@ -1335,16 +1335,45 @@
 idator;%0A
+import java.util.Collection;%0A
 import j
@@ -1628,65 +1628,286 @@
 le%3E 
-%7B%0A%0A    private CRUDRepository%3CIdentifier, SequenceFile%3E f
+implements SequenceFileService %7B%0A%0A    /**%0A     * A reference to the file system repository.%0A     */%0A    private CRUDRepository%3CIdentifier, SequenceFile%3E fileRepository;%0A    /**%0A     * A reference to the data store repository.%0A     */%0A    private SequenceFileRepository sequenceF
 ileR
@@ -2341,24 +2341,86 @@
 ile.class);%0A
+        this.sequenceFileRepository = sequenceFileRepository;%0A
         this
@@ -3661,34 +3661,32 @@
 ceFileRepository
-()
 .addFileToProjec
@@ -3808,18 +3808,16 @@
 pository
-()
 .addFile
@@ -3851,147 +3851,8 @@
  %7D%0A%0A
-    public List%3CSequenceFile%3E getFilesForSample(Sample sample) %7B%0A        return sequenceFileRepository().getFilesForSample(sample);%0A    %7D%0A%0A
     
@@ -3952,18 +3952,16 @@
 pository
-()
 .getFile
@@ -3997,61 +3997,125 @@
     
-public SequenceFileRepository sequenceFileRepository(
+/**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public Collection%3CSequenceFile%3E getSequenceFilesForSample(Sample s
 ) %7B%0A
@@ -4129,18 +4129,17 @@
  return 
-(S
+s
 equenceF
@@ -4155,27 +4155,36 @@
 tory
-) repository
+.getFilesForSample(s)
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -4180,11 +4180,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Rename method for adding sequence files to samples.