Project

General

Profile

Download (2.28 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.json.processor.value;
10

    
11
import java.util.Map;
12
import java.util.Objects;
13

    
14
import eu.etaxonomy.cdm.api.service.l10n.TermRepresentation_L10n;
15
import eu.etaxonomy.cdm.api.service.name.TypeDesignationSet;
16
import eu.etaxonomy.cdm.model.common.LanguageString;
17
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
18
import eu.etaxonomy.cdm.ref.TypedEntityReference;
19
import net.sf.json.JSONObject;
20
import net.sf.json.JsonConfig;
21
import net.sf.json.processors.JsonValueProcessor;
22

    
23
/**
24
 * @author a.kohlbecker
25
 * @since 23.06.2010
26
 */
27
public class MapJSONValueProcessor implements JsonValueProcessor {
28

    
29

    
30
	@Override
31
	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
32

    
33
	    if(value instanceof TypeDesignationSet){
34
	        TypeDesignationSet map = (TypeDesignationSet)value;
35
	        JSONObject json = new JSONObject();
36
            for(TypeDesignationStatusBase<?> key : map.keySet()){
37
                TermRepresentation_L10n term_L10n = new TermRepresentation_L10n(key, false);
38
                String label = Objects.toString(term_L10n.getLabel(), "NULL");
39
                json.element(label, map.get(key), jsonConfig);
40
            }
41
            return json;
42
	    } else if(value instanceof Map){
43
			Map<?,?> map= (Map<?,?>)value;
44
			if( ! map.isEmpty()){
45
			    JSONObject json = new JSONObject();
46
			    if(map.keySet().iterator().next() instanceof TypedEntityReference){
47
			        for(Object key : map.keySet()){
48
			            json.element(key.toString(), map.get(key), jsonConfig);
49
			        }
50
			    } else {
51
    				for (Object val : map.values()){
52
    					if(val instanceof LanguageString){
53
    						json.element(((LanguageString)val).getLanguageLabel(), val, jsonConfig);
54
    					} else {
55
    						return JSONObject.fromObject(value, jsonConfig);
56
    					}
57
    				}
58
			    }
59
				return json;
60
			}
61
		}
62
		return JSONObject.fromObject(value, jsonConfig);
63
	}
64

    
65
	@Override
66
	public Object processObjectValue(String key, Object value,
67
			JsonConfig jsonConfig) {
68
		return processArrayValue(value, jsonConfig);
69
	}
70
}
(7-7/10)