package org.vitrivr.cineast.core.metadata;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.vitrivr.cineast.core.data.entities.MultimediaMetadataDescriptor;
import org.vitrivr.cineast.core.util.json.JacksonJsonProvider;

public class JsonMetaDataExtractor implements MetadataExtractor {

  private static final JacksonJsonProvider jsonProvider = new JacksonJsonProvider();

  @Override
  public List<MultimediaMetadataDescriptor> extract(String objectId, Path path) {
    File file = path.toFile();
    File parent = file.getParentFile();
    File jsonFile = new File(parent, file.getName() + ".json");
    
    if(!jsonFile.exists()){
        jsonFile = new File(parent, com.google.common.io.Files.getNameWithoutExtension(file.getName()) + "json");
    }
    
    if(!jsonFile.exists()){
      return new ArrayList<>(0);
    }
    
    @SuppressWarnings("unchecked")
    Map<String, Object> json = jsonProvider.toObject(jsonFile, Map.class);
    
    if(json == null || json.isEmpty()){
      return new ArrayList<>(0);
    }
    
    ArrayList<MultimediaMetadataDescriptor> _return = new ArrayList<>(json.size());
    
    Set<String> keys = json.keySet();
    
    for(String key : keys){
      _return.add(
          MultimediaMetadataDescriptor.of(objectId, domain(), key, json.get(key))
          );
    }
    
    return _return;
  }

  @Override
  public String domain() {
    return "JSON";
  }

}

@@ -865,16 +865,17 @@
 e()) + %22
+.
 json%22);%0D

added missing '.' in filename check