1 package org.kuali.ole.util;
2
3 import com.thoughtworks.xstream.XStream;
4 import com.thoughtworks.xstream.converters.Converter;
5 import com.thoughtworks.xstream.converters.MarshallingContext;
6 import com.thoughtworks.xstream.converters.UnmarshallingContext;
7 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
8 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
9 import org.apache.commons.io.IOUtils;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import java.io.InputStream;
14 import java.net.URL;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18
19
20
21
22
23
24
25
26 public class Places008 {
27 private List<Place> listOfPlaces = new ArrayList<Place>();
28 private static Places008 placesObj = null;
29 protected final Logger LOG = LoggerFactory.getLogger(Places008.class);
30
31 public Places008() {
32 try {
33 URL url = this.getClass().getClassLoader().getResource("places008.xml");
34 XStream xStream = new XStream();
35 xStream.alias("places", Places008.class);
36 xStream.alias("place", Place.class);
37 xStream.addImplicitCollection(Places008.class, "listOfPlaces", Place.class);
38 xStream.registerConverter(new PlaceConverter());
39 Places008 places008 = (Places008) xStream.fromXML(IOUtils.toString((InputStream) url.getContent()));
40 this.setListOfPlaces(Collections.unmodifiableList(places008.getListOfPlaces()));
41 } catch (Exception e) {
42 LOG.error(e.getMessage(), e);
43 }
44 }
45
46 public static Places008 getInstance() {
47 if (placesObj == null) {
48 placesObj = new Places008();
49 }
50 return placesObj;
51
52 }
53
54
55 public Place getPlace() {
56 int indx = getListOfPlaces().indexOf(new Place());
57 if (indx != -1) {
58 return this.getListOfPlaces().get(indx);
59 } else {
60 return null;
61 }
62 }
63
64
65
66
67
68
69
70 public List<String> getPlaceDescription(String code) {
71 List<String> pList = new ArrayList<>();
72 for (Place place : listOfPlaces) {
73 if (code.equals("*")) {
74 pList.add(place.getValue());
75 } else if (place.getValue().toLowerCase().indexOf(code)>0) {
76 pList.add(place.getValue());
77 }
78 if(place.getCode().startsWith(code)){
79 pList.add(place.getValue());
80 }
81 }
82 return pList;
83 }
84
85 public List<Place> getListOfPlaces() {
86 return listOfPlaces;
87 }
88
89 private void setListOfPlaces(List<Place> listOfPlaces) {
90 this.listOfPlaces = listOfPlaces;
91 }
92
93 private void addLanguage(Place place) {
94 this.listOfPlaces.add(place);
95 }
96
97
98
99
100
101
102 public class Place {
103 public Place() {
104 }
105
106 public Place(String code) {
107 this.code = code;
108 }
109
110 private String code = null;
111 private String value = null;
112
113 public String getCode() {
114 return code;
115 }
116
117 public void setCode(String code) {
118 this.code = code;
119 }
120
121 public String getValue() {
122 return value;
123 }
124
125 public void setValue(String value) {
126 this.value = value;
127 }
128
129 @Override
130 public boolean equals(Object obj) {
131 if (obj instanceof Place) {
132 if (((Place) obj).getCode().equals(code)) {
133 return true;
134 } else {
135 return false;
136 }
137 } else {
138 return false;
139 }
140 }
141 }
142
143 private class PlaceConverter
144 implements Converter {
145 @Override
146 public void marshal(Object o, HierarchicalStreamWriter streamWriter, MarshallingContext marshallingContext) {
147 }
148
149 @Override
150 public Object unmarshal(HierarchicalStreamReader streamReader, UnmarshallingContext unmarshallingContext) {
151 Place place = new Place();
152 place.setCode(streamReader.getAttribute("code"));
153 place.setValue(streamReader.getValue());
154 return place;
155 }
156
157 @Override
158 public boolean canConvert(Class aClass) {
159 return aClass.equals(Place.class);
160 }
161 }
162
163 }