|
| 1 | +package controllers; |
| 2 | + |
| 3 | +import com.thoughtworks.xstream.XStream; |
| 4 | +import com.thoughtworks.xstream.io.xml.DomDriver; |
| 5 | +import models.*; |
| 6 | +import utils.Utilities; |
| 7 | + |
| 8 | +import java.io.FileReader; |
| 9 | +import java.io.FileWriter; |
| 10 | +import java.io.ObjectInputStream; |
| 11 | +import java.io.ObjectOutputStream; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class DeveloperAPI { |
| 16 | + |
| 17 | + //TODO Nothing! The DeveloperAPI class is completed! All you have to do here is implemment the ISerializer interface |
| 18 | + |
| 19 | + private List<Developer> developers = new ArrayList<>(); |
| 20 | + |
| 21 | + //--------------------- |
| 22 | + // Create methods |
| 23 | + //--------------------- |
| 24 | + public boolean addDeveloper(Developer developer) { |
| 25 | + if (isValidDeveloper(developer.getDeveloperName())){ |
| 26 | + return false; |
| 27 | + } |
| 28 | + return developers.add(developer); |
| 29 | + } |
| 30 | + |
| 31 | + //--------------------- |
| 32 | + // Read methods |
| 33 | + //--------------------- |
| 34 | + public Developer getDeveloperByIndex(int index){ |
| 35 | + if (Utilities.isValidIndex(developers, index)){ |
| 36 | + return developers.get(index); |
| 37 | + } |
| 38 | + else{ |
| 39 | + return null; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public Developer getDeveloperByName (String developerName){ |
| 44 | + int index = retrieveDeveloperIndex(developerName); |
| 45 | + if (index != -1){ |
| 46 | + return developers.get(index); |
| 47 | + } |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public String listDevelopers(){ |
| 53 | + String listDevelopers = ""; |
| 54 | + for (Developer developer : developers){ |
| 55 | + listDevelopers += developers.indexOf(developer) + ": " + developer + "\n"; |
| 56 | + } |
| 57 | + if (listDevelopers.equals("")){ |
| 58 | + return "No developers"; |
| 59 | + } |
| 60 | + else { |
| 61 | + return listDevelopers; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + //--------------------- |
| 66 | + // Update methods |
| 67 | + //--------------------- |
| 68 | + public boolean updateDeveloperWebsite(String developerName, String developerWebsite){ |
| 69 | + if (isValidDeveloper(developerName)){ |
| 70 | + Developer developerToUpdate = getDeveloperByName(developerName); |
| 71 | + developerToUpdate.setDeveloperWebsite(developerWebsite); |
| 72 | + return true; |
| 73 | + } |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + //--------------------- |
| 78 | + // Delete methods |
| 79 | + //--------------------- |
| 80 | + public Developer removeDeveloper(String developerName){ |
| 81 | + int index = retrieveDeveloperIndex(developerName); |
| 82 | + if (index != -1) { |
| 83 | + return developers.remove(index); |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + //--------------------- |
| 89 | + // Validation Methods |
| 90 | + //--------------------- |
| 91 | + public boolean isValidDeveloper(String developerName){ |
| 92 | + for (Developer developer : developers){ |
| 93 | + if (developer.getDeveloperName().equalsIgnoreCase(developerName)){ |
| 94 | + return true; |
| 95 | + } |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + public int retrieveDeveloperIndex(String developerName){ |
| 101 | + for (Developer developer : developers){ |
| 102 | + if (developer.getDeveloperName().equalsIgnoreCase(developerName)){ |
| 103 | + return developers.indexOf(developer); |
| 104 | + } |
| 105 | + } |
| 106 | + return -1; |
| 107 | + } |
| 108 | + |
| 109 | + //--------------------- |
| 110 | + // Getters/Setters |
| 111 | + //--------------------- |
| 112 | + public List<Developer> getDevelopers() { |
| 113 | + return developers; |
| 114 | + } |
| 115 | + |
| 116 | + //--------------------- |
| 117 | + // Persistence Methods |
| 118 | + //--------------------- |
| 119 | + /** |
| 120 | + * The load method uses the XStream component to read all the objects from the xml |
| 121 | + * file stored on the hard disk. The read objects are loaded into the associated ArrayList |
| 122 | + * |
| 123 | + * @throws Exception An exception is thrown if an error occurred during the load e.g. a missing file. |
| 124 | + */ |
| 125 | + @SuppressWarnings("unchecked") |
| 126 | + public void load() throws Exception { |
| 127 | + //list of classes that you wish to include in the serialisation, separated by a comma |
| 128 | + Class<?>[] classes = new Class[]{Developer.class}; |
| 129 | + |
| 130 | + //setting up the xstream object with default security and the above classes |
| 131 | + XStream xstream = new XStream(new DomDriver()); |
| 132 | + XStream.setupDefaultSecurity(xstream); |
| 133 | + xstream.allowTypes(classes); |
| 134 | + |
| 135 | + //doing the actual serialisation to an XML file |
| 136 | + ObjectInputStream in = xstream.createObjectInputStream(new FileReader(fileName())); |
| 137 | + developers = (List<Developer>) in.readObject(); |
| 138 | + in.close(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * The save method uses the XStream component to write all the objects in the ArrayList |
| 143 | + * to the xml file stored on the hard disk. |
| 144 | + * |
| 145 | + * @throws Exception An exception is thrown if an error occurred during the save e.g. drive is full. |
| 146 | + */ |
| 147 | + public void save() throws Exception { |
| 148 | + XStream xstream = new XStream(new DomDriver()); |
| 149 | + ObjectOutputStream out = xstream.createObjectOutputStream(new FileWriter(fileName())); |
| 150 | + out.writeObject(developers); |
| 151 | + out.close(); |
| 152 | + } |
| 153 | + |
| 154 | + public String fileName(){ |
| 155 | + return "developers.xml"; |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | +} |
0 commit comments