Project

General

Profile

« Previous | Next » 

Revision 036b973d

Added by Andreas Kohlbecker almost 4 years ago

ref #9146 media metadata service method with cdm property based include and exclude filter definitions

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/MediaServiceImpl.java
9 9

  
10 10
package eu.etaxonomy.cdm.api.service;
11 11

  
12
import java.io.IOException;
12 13
import java.util.ArrayList;
14
import java.util.HashMap;
13 15
import java.util.List;
16
import java.util.Map;
14 17
import java.util.Set;
15 18
import java.util.UUID;
19
import java.util.stream.Collectors;
16 20

  
21
import org.apache.http.HttpException;
17 22
import org.springframework.beans.factory.annotation.Autowired;
18 23
import org.springframework.stereotype.Service;
19 24
import org.springframework.transaction.annotation.Transactional;
......
23 28
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
24 29
import eu.etaxonomy.cdm.api.service.pager.Pager;
25 30
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
31
import eu.etaxonomy.cdm.common.media.ImageInfo;
26 32
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
27 33
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28 34
import eu.etaxonomy.cdm.model.common.CdmBase;
......
37 43
import eu.etaxonomy.cdm.model.location.NamedArea;
38 44
import eu.etaxonomy.cdm.model.media.Media;
39 45
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
46
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
40 47
import eu.etaxonomy.cdm.model.media.Rights;
48
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
49
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
50
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
41 51
import eu.etaxonomy.cdm.model.name.TaxonName;
42 52
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
43 53
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
......
49 59
@Transactional(readOnly=true)
50 60
public class MediaServiceImpl extends IdentifiableServiceBase<Media,IMediaDao> implements IMediaService {
51 61

  
62
    public static final Integer IMAGE_READ_TIMEOUT = 3000;
63

  
64

  
52 65
    @Override
53 66
    @Autowired
54 67
	protected void setDao(IMediaDao dao) {
......
61 74
    private ITaxonService taxonService;
62 75
	@Autowired
63 76
    private INameService nameService;
77
	@Autowired
78
	private IPreferenceService prefsService;
64 79

  
65 80

  
66 81
	@Override
......
257 272

  
258 273
        return result;
259 274
    }
275

  
276
    /**
277
     * Reads the metadata as stored in the file or web resource and filters the data by the include and exclude lists of key names
278
     * as stored in the data base properties {@link PreferencePredicate#MediaMetadataKeynameExcludes} and {@link PreferencePredicate#MediaMetadataKeynameExcludes}
279
     * <p>
280
     * Metadata of multiple parts is merged into one common metadata map whereas the later part being read may overwrite data from previous parts.
281
     * The consequences of this can be neglected since we don't expect that multiple parts are actually being used.
282
     *
283
     * @param represenatation
284
     * @return
285
     * @throws IOException
286
     * @throws HttpException
287
     */
288
    public Map<String, String> readResourceMetadataFiltered(MediaRepresentation represenatation) throws IOException, HttpException {
289

  
290
        List<String> includes = mediaMetadataKeyIncludes();
291
        List<String> excludes = mediaMetadataKeyExludes();
292
        Map<String, String> metadata = new HashMap<>();
293

  
294
        for(MediaRepresentationPart part : represenatation.getParts()) {
295
            ImageInfo iInfo = ImageInfo.NewInstance(part.getUri(), IMAGE_READ_TIMEOUT);
296
            metadata.putAll(iInfo.getMetaData());
297
        }
298

  
299
        if(!includes.isEmpty()) {
300
            metadata = metadata.entrySet()
301
                    .stream()
302
                    .filter( e -> containsCaseInsensitive(e.getKey(), includes))
303
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
304
        }
305
        if(!excludes.isEmpty()) {
306
            metadata = metadata.entrySet()
307
                    .stream()
308
                    .filter( e -> !containsCaseInsensitive(e.getKey(), excludes))
309
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
310
        }
311

  
312
        if(metadata == null) {
313
            metadata = new HashMap<>(0);
314
        }
315
        return metadata;
316
    }
317

  
318
    private boolean containsCaseInsensitive(String s, List<String> l){
319
        return l.stream().anyMatch(x -> x.equalsIgnoreCase(s));
320
    }
321

  
322
    protected List<String> mediaMetadataKeyExludes(){
323
        CdmPreference pref = prefsService.find(CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.MediaMetadataKeynameExcludes));
324
        return pref.splitStringListValue();
325
    }
326

  
327
    protected List<String> mediaMetadataKeyIncludes(){
328
        CdmPreference pref = prefsService.find(CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.MediaMetadataKeynameIncludes));
329
        return pref.splitStringListValue();
330
    }
331

  
332

  
260 333
}

Also available in: Unified diff