Project

General

Profile

Download (2.43 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
 */
28
public class MapJSONValueProcessor implements JsonValueProcessor {
29

    
30

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

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

    
66
	/* (non-Javadoc)
67
	 * @see net.sf.json.processors.JsonValueProcessor#processObjectValue(java.lang.String, java.lang.Object, net.sf.json.JsonConfig)
68
	 */
69
	@Override
70
	public Object processObjectValue(String key, Object value,
71
			JsonConfig jsonConfig) {
72
		return processArrayValue(value, jsonConfig);
73
	}
74

    
75
}
(7-7/10)