Project

General

Profile

« Previous | Next » 

Revision 78ae2e04

Added by Andreas Müller over 6 years ago

reset MediaUtils (was accidentally commited)

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/media/MediaUtils.java
36 36
        // find best matching representations of each media
37 37
        SortedMap<Integer, MediaRepresentation> prefRepresentations
38 38
                = filterAndOrderMediaRepresentations(media.getRepresentations(), null, mimeTypes,
39
                        size, widthOrDuration, height, false);
39
                        size, widthOrDuration, height);
40 40
        try {
41 41
            // take first one and remove all other representations
42 42
            MediaRepresentation prefOne = prefRepresentations.get(prefRepresentations.firstKey());
......
107 107
        return mediaRepresentationPart;
108 108
    }
109 109

  
110
    /**
111
     * @see #findPreferredMedia(List, Class, String[], String[], Integer, Integer, Integer, boolean)
112
     */
113
    public static Map<Media, MediaRepresentation> findPreferredMedia(List<Media> mediaList,
114
            Class<? extends MediaRepresentationPart> representationPartType, String[] mimeTypes, String[] sizeTokens,
115
            Integer widthOrDuration, Integer height, Integer size
116
            ) {
117
        return findPreferredMedia(mediaList, representationPartType, mimeTypes, sizeTokens,
118
                widthOrDuration, height, size, false);
119
    }
120

  
121 110

  
122 111
    /**
123 112
     * Filters the given List of Media by the supplied filter parameters <code>representationPartType</code>,
......
136 125
     * @param widthOrDuration
137 126
     * @param height
138 127
     * @param size
139
     * @param onlyBestMatchIfFilterMatches if <code>true</code> and all required filter parameters
140
     *      (like size, width and height) are not <code>null</code> only the best matching representation is returned.
141
     *       Otherwise all representations are returned in sorted order.
142 128
     * @return
143 129
     */
144 130
    public static Map<Media, MediaRepresentation> findPreferredMedia(List<Media> mediaList,
145 131
            Class<? extends MediaRepresentationPart> representationPartType, String[] mimeTypes, String[] sizeTokens,
146
            Integer widthOrDuration, Integer height, Integer size,
147
            boolean onlyBestMatchIfFilterMatches) {
132
            Integer widthOrDuration, Integer height, Integer size) {
148 133

  
149 134
        if(mimeTypes != null) {
150 135
            for(int i=0; i<mimeTypes.length; i++){
......
186 171

  
187 172
                SortedMap<Integer, MediaRepresentation> prefRepresentations
188 173
                    = filterAndOrderMediaRepresentations(candidateRepresentations, representationPartType,
189
                            mimeTypes, size, widthOrDuration, height, false);
174
                            mimeTypes, size, widthOrDuration, height);
190 175
                try {
191 176
                    if(prefRepresentations.size() > 0){
192 177
                        // Media.representations is a set
......
257 242
    }
258 243

  
259 244
    */
260

  
261 245
    /**
262
     * @param mediaRepresentations
263
     * @param representationPartType
264 246
     * @param mimeTypeRegexes
265 247
     * @param size
266 248
     * @param widthOrDuration
267 249
     * @param height
268
     * @param onlyBestMatchIfFilterMatches if <code>true</code> and all required filter parameters
269
     *      (like size, width and height) are not <code>null</code> only the best matching representation is returned.
270
     *       Otherwise all representations are returned in sorted order.
271 250
     * @return
251
     *
252
     *
272 253
     */
273 254
    private static SortedMap<Integer, MediaRepresentation> filterAndOrderMediaRepresentations(
274 255
            Set<MediaRepresentation> mediaRepresentations,
275 256
            Class<? extends MediaRepresentationPart> representationPartType, String[] mimeTypeRegexes,
276
            Integer size, Integer widthOrDuration, Integer height, boolean onlyBestMatchIfFilterMatches) {
257
            Integer size, Integer widthOrDuration, Integer height) {
277 258

  
278 259
        SortedMap<Integer, MediaRepresentation> prefRepr = new TreeMap<>();
279 260

  
......
283 264
        height = (height == null ? new Integer(0) : height);
284 265
        mimeTypeRegexes = (mimeTypeRegexes == null ? new String[]{".*"} : mimeTypeRegexes);
285 266

  
286
        boolean filterMatches = true;
287 267
        for (String mimeTypeRegex : mimeTypeRegexes) {
288 268
            // getRepresentationByMimeType
289 269
            Pattern mimeTypePattern = Pattern.compile(mimeTypeRegex);
......
300 280

  
301 281
                int dwa = 0;
302 282

  
283

  
303 284
                //first the size is used for comparison
304 285
                for (MediaRepresentationPart part : representation.getParts()) {
305 286

  
......
315 296
                    logger.debug(part + " matches");
316 297
                    matchingParts.add(part);
317 298

  
318
                    boolean partFilterMatches = false;
319

  
320 299
                    if (part.getSize()!= null){
321 300
                        int sizeOfPart = part.getSize();
322 301
                        int distance = sizeOfPart - size;
......
324 303
                            distance *= -1;
325 304
                        }
326 305
                        dwa += distance;
327
                        if (size > 0){
328
                            partFilterMatches = true;
329
                        }
330 306
                    }
331 307

  
332 308
                    //if height and width/duration is defined, add this information, too
......
351 327
                        }
352 328
                        dwa += durationWidthWeight;
353 329

  
354
                    }else{
355
                        partFilterMatches = true;
356 330
                    }
357
                    filterMatches &= partFilterMatches;
358 331
                } // loop parts
359 332
                logger.debug("matchingParts.size():" + matchingParts.size());
360 333
                if(matchingParts.size() > 0 ){
......
370 343
            } // loop representations
371 344
        } // loop mime types
372 345
        logger.debug(prefRepr.size() + " preferred representations found");
373

  
374
        if (onlyBestMatchIfFilterMatches && filterMatches){
375
            SortedMap<Integer, MediaRepresentation> result = new TreeMap<>();
376
            try {
377
                result.put(prefRepr.firstKey(), prefRepr.get(prefRepr.firstKey()));
378
                return result;
379
            } catch (Exception e) {
380
                return result;
381
            }
382
        }else{
383
            return prefRepr;
384
        }
346
        return prefRepr;
385 347
    }
386 348
}

Also available in: Unified diff