View Javadoc

1   package org.kuali.ole.location.keyvalue;
2   
3   import junit.framework.TestCase;
4   import org.junit.Test;
5   import org.junit.runner.RunWith;
6   import org.kuali.ole.location.bo.OleLocation;
7   import org.kuali.rice.core.api.util.ConcreteKeyValue;
8   import org.kuali.rice.core.api.util.KeyValue;
9   import org.kuali.rice.krad.service.KRADServiceLocator;
10  import org.springframework.test.context.ContextConfiguration;
11  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12  import org.springframework.test.context.transaction.TransactionConfiguration;
13  import org.springframework.transaction.annotation.Transactional;
14  
15  import java.util.*;
16  
17  /**
18   * Created with IntelliJ IDEA.
19   * User: pvsubrah
20   * Date: 8/29/12
21   * Time: 11:14 AM
22   * To change this template use File | Settings | File Templates.
23   */
24  @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
25  @RunWith(value = SpringJUnit4ClassRunner.class)
26  @TransactionConfiguration(defaultRollback = true)
27  public class LocationKeyValuesFinder_UT extends TestCase {
28  
29      private List<OleLocation> temp;
30  
31      @Test
32      @Transactional
33      public void testGetKeyValues() throws Exception {
34          List<KeyValue> options = new ArrayList<KeyValue>();
35          List<String> locations = new ArrayList<String>();
36  
37          Collection<OleLocation> oleLocations = KRADServiceLocator.getBusinessObjectService().findAll(OleLocation.class);
38          temp = (List<OleLocation>) oleLocations;
39          StringBuilder locationString = new StringBuilder();
40          StringBuilder locationCode = new StringBuilder();
41          OleLocation parentLocation = null;
42          Iterator<OleLocation> iterator = oleLocations.iterator();
43          while (iterator.hasNext()) {
44              OleLocation location = iterator.next();
45              if (null == parentLocation) {
46                  if (null == location.getParentLocationId()) {
47                      parentLocation = location;
48                      locationString.append(parentLocation.getLocationName());
49                      locationCode.append(parentLocation.getLocationCode());
50                  }
51              } else if (parentLocation.getLocationId().equals(location.getParentLocationId())) {
52                  locationString.append("-" + location.getLocationName());
53                  locationCode.append("-" + parentLocation.getLocationCode());
54                  parentLocation = location;
55  
56              } else {
57                  locations.add(locationString.toString());
58                  options.add(new ConcreteKeyValue(locationCode.toString(), locationString.toString()));
59                  locationString = new StringBuilder();
60                  locationCode = new StringBuilder();
61                  List<OleLocation> oleLocations1 = new ArrayList<OleLocation>();
62                  if (null != temp) {
63                      oleLocations1.addAll(temp);
64                  }
65                  oleLocations1.remove(parentLocation);
66                  temp = oleLocations1;
67                  parentLocation = null;
68                  iterator = oleLocations1.iterator();
69              }
70          }
71          locations.add(locationString.toString());
72          locations.add(temp.get(0).getLocationName());
73  
74          Collections.sort(locations);
75  
76          for (Iterator<String> oleLocationIterator = locations.iterator(); oleLocationIterator.hasNext(); ) {
77              String locationName = oleLocationIterator.next();
78              System.out.println(locationName);
79  
80          }
81      }
82  }