├── LICENSE ├── README.md ├── jama.properties.dist ├── pom.xml ├── src └── main │ └── java │ └── com │ └── jamasoftware │ └── services │ └── restclient │ ├── JamaConfig.java │ ├── JamaParent.java │ ├── Main.java │ ├── exception │ ├── ItemCreationException.java │ ├── ItemEditException.java │ ├── JamaApiException.java │ ├── JamaFieldNotFound.java │ ├── JamaTypeMismatchException.java │ ├── JsonException.java │ ├── RestClientException.java │ └── UnexpectedJamaResponseException.java │ ├── httpconnection │ ├── ApacheHttpClient.java │ ├── FileResponse.java │ ├── HttpClient.java │ ├── Response.java │ └── TestHttpClient.java │ ├── jamaclient │ ├── JamaClient.java │ └── JamaPage.java │ ├── jamadomain │ ├── JamaLocation.java │ ├── JamaRichText.java │ ├── LockStatus.java │ ├── SerializableJamaDomainObject.java │ ├── TestCaseStep.java │ ├── core │ │ ├── JamaDomainObject.java │ │ ├── JamaInstance.java │ │ ├── LazyResource.java │ │ └── StagingDispenser.java │ ├── fields │ │ ├── CalculatedField.java │ │ ├── DateField.java │ │ ├── FlagField.java │ │ ├── IntegerField.java │ │ ├── JamaField.java │ │ ├── MultiSelectField.java │ │ ├── PickListField.java │ │ ├── ProjectField.java │ │ ├── ReleaseField.java │ │ ├── RichTextField.java │ │ ├── RollupField.java │ │ ├── TestCaseStatusField.java │ │ ├── TestCaseStepsField.java │ │ ├── TextBoxField.java │ │ ├── TextField.java │ │ ├── TimeField.java │ │ ├── URLField.java │ │ ├── UnsupportedField.java │ │ └── UserField.java │ ├── lazyresources │ │ ├── ChildrenList.java │ │ ├── ItemTypeList.java │ │ ├── JamaItem.java │ │ ├── JamaItemType.java │ │ ├── JamaProject.java │ │ ├── JamaRelationship.java │ │ ├── JamaRelationshipType.java │ │ ├── JamaUser.java │ │ ├── LazyBase.java │ │ ├── LazyCollection.java │ │ ├── OptionList.java │ │ ├── PickList.java │ │ ├── PickListOption.java │ │ ├── RelationshipTypeList.java │ │ └── Release.java │ ├── stagingresources │ │ ├── StagingFactory.java │ │ ├── StagingItem.java │ │ ├── StagingJamaItemType.java │ │ ├── StagingPickList.java │ │ ├── StagingPickListOption.java │ │ ├── StagingProject.java │ │ ├── StagingRelationship.java │ │ ├── StagingRelationshipType.java │ │ ├── StagingRelease.java │ │ ├── StagingResource.java │ │ └── StagingUser.java │ └── values │ │ ├── CalculatedFieldValue.java │ │ ├── DateFieldValue.java │ │ ├── FlagFieldValue.java │ │ ├── IntegerFieldValue.java │ │ ├── JamaFieldValue.java │ │ ├── MultiSelectFieldValue.java │ │ ├── PickListFieldValue.java │ │ ├── ProjectFieldValue.java │ │ ├── ReleaseFieldValue.java │ │ ├── RichTextFieldValue.java │ │ ├── RollupFieldValue.java │ │ ├── TestCaseStatusFieldValue.java │ │ ├── TestCaseStepsFieldValue.java │ │ ├── TextBoxFieldValue.java │ │ ├── TextFieldValue.java │ │ ├── TimeFieldValue.java │ │ ├── URLFieldValue.java │ │ └── UserFieldValue.java │ ├── json │ ├── ItemTypeImage.java │ ├── JsonHandler.java │ ├── JsonSerializer.java │ ├── JsonStagingItem.java │ ├── JsonStagingItemType.java │ ├── JsonStagingPickList.java │ ├── JsonStagingPickListOption.java │ ├── JsonStagingProject.java │ ├── JsonStagingRelationship.java │ ├── JsonStagingRelationshipType.java │ ├── JsonStagingRelease.java │ ├── JsonStagingUser.java │ ├── SimpleJsonDeserializer.java │ ├── SimpleJsonHandler.java │ ├── SimpleJsonSerializer.java │ └── SimpleJsonUtil.java │ └── util │ ├── CompareUtil.java │ └── DateUtil.java ├── test └── restclient │ └── jamadomain │ └── lazyresources │ ├── JamaItemTest.java │ ├── JamaItemTypeTest.java │ └── JamaProjectTest.java └── testData ├── testApiPayloads.json └── testApiResponses.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Jama Software 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jama Software 2 | Jama Software is the definitive system of record and action for product development. The company’s modern requirements and test management solution helps enterprises accelerate development time, mitigate risk, slash complexity and verify regulatory compliance. More than 600 product-centric organizations, including NASA, Boeing and Caterpillar use Jama to modernize their process for bringing complex products to market. The venture-backed company is headquartered in Portland, Oregon. For more information, visit [jamasoftware.com](http://jamasoftware.com). 3 | 4 | Please visit [dev.jamasoftware.com](http://dev.jamasoftware.com) for additional resources and join the discussion in our community [community.jamasoftware.com](http://community.jamasoftware.com). 5 | 6 | ## RestClient 7 | RestClient is a Java REST API client application for Jama Software customers. The client will allow customers to easily access the REST API to retrieve, and modify data within their Jama Instance. 8 | The client will be continuously updated in hopes of creating a more robust client that addresses all endpoints available through the API. 9 | 10 | Please note that this client is distributed as-is as an example and will likely require modification to work for your specific use-case. 11 | 12 | 13 | ## Requirements 14 | - Module SDK `Java 1.8` or higher. 15 | - Language level `7 - Diamonds, ARM, multi-catch etc.` 16 | 17 | 18 | ## Setup 19 | 1. As always, set up a test environment and test project. 20 | 21 | 2. Fill out the CONFIG section of the jama.properties file. The necessary fields are: 22 | - ```username``` 23 | - ```password``` 24 | - ```base_url``` 25 | - ```resourceTimeOut``` - refresh time for items 26 | - ```apiKey``` - (optional) apiKey will be injected into requests' headers 27 | 28 | 3. Change the name of the `jama.properties.dist` to `jama.properties` 29 | 30 | 31 | 32 | ### REST Calls Supported in the Client 33 | 34 | ##### Projects: 35 | - GET all projects 36 | - GET project by ID 37 | 38 | ##### Item Types 39 | - GET all Item Types in a Jama instance 40 | - GET an Item Type by id 41 | - GET an Item Type by name 42 | - GET an Item Type's image 43 | 44 | ##### Items 45 | - GET all items by project 46 | - PUT/POST items by project 47 | - GET/PUT/DELETE item locks 48 | - Change an item's location 49 | - GET all downstream/upstream related items 50 | - DELETE an item using its ID 51 | 52 | ##### Relationship Types 53 | - GET all relationship types in a project 54 | 55 | ##### Relationships 56 | - GET all relationships in a Jama project 57 | - GET a relationship's upstream/downstream items 58 | - GET all upstream/downstream relationships of an item 59 | - GET upstream/downstream item of a relationship 60 | - DELETE a relationship using its ID 61 | - PUT a relationship with updated fromItem, toItem, and/or relationshipType 62 | - POST a newly created relationship 63 | 64 | 65 | 66 | ## Usage Examples 67 | #### GET all Item Types in a Jama Instance 68 | ``` 69 | try { 70 | 71 | JamaInstance jamaInstance = new JamaInstance(new JamaConfig(true)); 72 | List jamaItemTypes = jamaInstance.getItemTypes(); 73 | System.out.println("Listing all Jama Instance Item Type names and IDs:"); 74 | for (JamaItemType jamaItemType : jamaItemTypes) { 75 | System.out.println(jamaItemType.getDisplayPlural() + " with API ID " + jamaItemType.getId()); 76 | } 77 | } catch(RestClientException e) { 78 | e.printStackTrace(); 79 | } 80 | ``` 81 | #### GET all Fields of a Jama Item Type 82 | ``` 83 | try { 84 | 85 | JamaInstance jamaInstance = new JamaInstance(new JamaConfig(true)); 86 | JamaItem jamaItem = jamaInstance.getItem(1972331); 87 | JamaItemType itemType = jamaItem.getItemType(); 88 | List jamaFields = itemType.getFields(); 89 | for(JamaField jamaField : jamaFields) { 90 | System.out.println("Field label: " + jamaField.getLabel()); 91 | } 92 | } catch (RestClientException e) { 93 | e.printStackTrace(); 94 | } 95 | ``` 96 | #### GET all Projects in a Jama Instance 97 | ``` 98 | try { 99 | JamaInstance jamaInstance = new JamaInstance(new JamaConfig(true)); 100 | ArrayList projects = (ArrayList) jamaInstance.getProjects(); 101 | System.out.println("Listing all Project names and IDs:"); 102 | for (JamaProject project : projects) { 103 | System.out.println(project.getName() + " with API ID " + project.getId()); 104 | } 105 | } catch(RestClientException e) { 106 | e.printStackTrace(); 107 | } 108 | ``` 109 | #### GET all Relationships in a Jama Project 110 | ``` 111 | try { 112 | 113 | JamaInstance jamaInstance = new JamaInstance(new JamaConfig(true)); 114 | JamaProject project = jamaInstance.getProject(20183); 115 | List jamaRelationships = project.getRelationships(); 116 | System.out.println("Listing all Relationships in Project"); 117 | for (JamaRelationship jamaRelationship : jamaRelationships) { 118 | System.out.println( 119 | "Relationship ID: " + jamaRelationship.getId() 120 | + " Upstream Item ID: " + jamaRelationship.getFromItem().getId() 121 | + "Downstream ItemID: " + jamaRelationship.getToItem().getId()); 122 | } 123 | } catch(RestClientException e) { 124 | e.printStackTrace(); 125 | } 126 | ``` 127 | #### GET all Items in a Jama Project 128 | ``` 129 | try { 130 | 131 | JamaInstance jamaInstance = new JamaInstance(new JamaConfig(true)); 132 | JamaProject project = jamaInstance.getProject(20183); 133 | List jamaItems = project.getItems(); 134 | System.out.println("Listing all Project Item names and IDs:"); 135 | for (JamaItem jamaItem : jamaItems) { 136 | System.out.println(jamaItem.getName() + " with API ID " + jamaItem.getId()); 137 | } 138 | } catch(RestClientException e) { 139 | e.printStackTrace(); 140 | } 141 | ``` 142 | #### Update a Jama Item's Description 143 | ``` 144 | try { 145 | 146 | JamaInstance jamaInstance = new JamaInstance(new JamaConfig(true)); 147 | JamaItem jamaItem = jamaInstance.getItem(2120043); 148 | JamaItem newParent = jamaInstance.getItem(2120044); 149 | System.out.println("Original parent ID: " + jamaItem.getParent().getId()); 150 | jamaItem.edit().setParent(newParent).commit(); 151 | System.out.println("Updated parent ID: " + jamaItem.getParent().getId()); 152 | } catch (RestClientException e) { 153 | e.printStackTrace(); 154 | } 155 | ``` 156 | #### Change a Jama Item's Location in a Project's Hierarchy Tree 157 | ``` 158 | try { 159 | JamaInstance jamaInstance = new JamaInstance(new JamaConfig(true)); 160 | ArrayList projects = (ArrayList) jamaInstance.getProjects(); 161 | System.out.println("Listing all project names and IDs:"); 162 | for (JamaProject project : projects) { 163 | System.out.println(project.getName() + " with API ID " + project.getId()); 164 | } 165 | } catch(RestClientException e) { 166 | e.printStackTrace(); 167 | } 168 | ``` 169 | #### GET all Downstream Related Items from a Jama Item 170 | ``` 171 | try { 172 | 173 | JamaInstance jamaInstance = new JamaInstance(new JamaConfig(true)); 174 | JamaItem jamaItem = jamaInstance.getItem(2120042); 175 | List downstreamRelatedItems = jamaItem.getDownstreamItems(); 176 | System.out.println("Listing all Downstream Related Items from Item: "); 177 | for (JamaItem downstreamRelatedItem : downstreamRelatedItems) { 178 | System.out.println("Item : " + downstreamRelatedItem.getName() + " with ID: " + downstreamRelatedItem.getId()); 179 | } 180 | } catch(RestClientException e) { 181 | e.printStackTrace(); 182 | } 183 | ``` 184 | #### Lock/Unlock a Jama Item 185 | ``` 186 | try { 187 | 188 | JamaInstance jamaInstance = new JamaInstance(new JamaConfig(true)); 189 | JamaItem jamaItem = jamaInstance.getItem(2120042); 190 | jamaItem.lock(); 191 | System.out.println(jamaItem.isLocked()); 192 | jamaItem.unlock(); 193 | System.out.println(jamaItem.isLocked()); 194 | } catch(RestClientException e) { 195 | e.printStackTrace(); 196 | } 197 | ``` 198 | 199 | #### POST a new Jama Relationship 200 | ```$xslt 201 | try { 202 | 203 | JamaRelationship create = new JamaRelationship(); 204 | create.associate(jamaInstance); 205 | JamaRelationship newlycreated = create.edit().setFromItem(fromItem).setToItem(toItem).setRelationshipType(relationshipType).commit(); 206 | System.out.println(newlycreated.toString()); 207 | System.out.println("done"); 208 | } catch(RestClientException e) { 209 | e.printStackTrace(); 210 | } 211 | ``` 212 | 213 | #### PUT (update) an existing Jama Relationship 214 | ```$xslt 215 | try { 216 | 217 | JamaItem fromItem = jamaInstance.getItem(2209261); 218 | JamaItem toItem = jamaInstance.getItem(2254438); 219 | 220 | JamaRelationshipType relationshipType = jamaInstance.getRelationshipTypes().get(0); 221 | JamaRelationship relationship = fromItem.getDownstreamRelationships().get(0); 222 | JamaRelationship updatedRelationship = relationship.edit().setFromItem(fromItem).setToItem(toItem).setRelationshipType(relationshipType).commit(); 223 | System.out.println(updatedRelationship); 224 | } catch(RestClientException e) { 225 | e.printStackTrace(); 226 | } 227 | ``` 228 | -------------------------------------------------------------------------------- /jama.properties.dist: -------------------------------------------------------------------------------- 1 | baseUrl=your jama url 2 | username=username 3 | password=password 4 | apiKey=SUPER_SECRET_KEY 5 | resourceTimeOut=30 -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | 1.8 9 | 1.8 10 | 11 | 12 | com.jamasoftware.services 13 | rest-client 14 | 1.2-SNAPSHOT 15 | 16 | 17 | org.apache.httpcomponents 18 | httpclient 19 | 4.5.2 20 | 21 | 22 | org.apache.httpcomponents 23 | httpmime 24 | 4.5.2 25 | 26 | 27 | org.apache.commons 28 | commons-io 29 | 1.3.2 30 | 31 | 32 | com.googlecode.json-simple 33 | json-simple 34 | 1.1.1 35 | 36 | 37 | org.skyscreamer 38 | jsonassert 39 | 1.4.0 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/JamaConfig.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient; 2 | 3 | import com.jamasoftware.services.restclient.httpconnection.ApacheHttpClient; 4 | import com.jamasoftware.services.restclient.httpconnection.HttpClient; 5 | import com.jamasoftware.services.restclient.json.JsonHandler; 6 | import com.jamasoftware.services.restclient.json.SimpleJsonHandler; 7 | 8 | import java.io.FileInputStream; 9 | import java.io.InputStream; 10 | import java.util.Properties; 11 | 12 | public class JamaConfig { 13 | private String baseUrl; 14 | // TODO add ending forward slash off of domain name 15 | private String username; 16 | private String password; 17 | private JsonHandler json; 18 | private HttpClient httpClient; 19 | private Integer resourceTimeOut; //value is seconds 20 | private String openUrlBase; 21 | private String apiKey = null; 22 | 23 | public JamaConfig() { 24 | json = new SimpleJsonHandler(); 25 | try { 26 | httpClient = new ApacheHttpClient(); 27 | } catch(Exception e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | 32 | public JamaConfig(boolean loadFromPropertiesFile, String filename) { 33 | this(); 34 | if(!loadFromPropertiesFile) { 35 | return; 36 | } 37 | InputStream input = null; 38 | try { 39 | Properties properties = new Properties(); 40 | input = new FileInputStream(filename); 41 | properties.load(input); 42 | setBaseUrl(properties.getProperty("baseUrl")); 43 | username = properties.getProperty("username"); 44 | password = properties.getProperty("password"); 45 | String timeOutString = properties.getProperty("resourceTimeOut"); 46 | setOpenUrlBase(properties.getProperty("baseUrl")); 47 | apiKey = properties.getProperty("apiKey"); 48 | resourceTimeOut = Integer.valueOf(timeOutString); 49 | } catch(Exception e) { 50 | e.printStackTrace(); 51 | System.exit(1); 52 | } 53 | } 54 | 55 | 56 | public JamaConfig(boolean loadFromPropertiesFile) { 57 | this(); 58 | if(!loadFromPropertiesFile) { 59 | return; 60 | } 61 | InputStream input = null; 62 | try { 63 | Properties properties = new Properties(); 64 | input = new FileInputStream("jama.properties"); 65 | properties.load(input); 66 | setBaseUrl(properties.getProperty("baseUrl")); 67 | username = properties.getProperty("username"); 68 | password = properties.getProperty("password"); 69 | setOpenUrlBase(properties.getProperty("baseUrl")); 70 | apiKey = properties.getProperty("apiKey"); 71 | String timeOutString = properties.getProperty("resourceTimeOut"); 72 | resourceTimeOut = Integer.valueOf(timeOutString); 73 | } catch(Exception e) { 74 | e.printStackTrace(); 75 | System.exit(1); 76 | } 77 | } 78 | 79 | public String getBaseUrl() { 80 | return baseUrl; 81 | } 82 | 83 | public void setBaseUrl(String baseUrl) { 84 | if(baseUrl.contains("/rest/")) { 85 | if(baseUrl.endsWith("/")) { 86 | this.baseUrl = baseUrl; 87 | return; 88 | } 89 | this.baseUrl = baseUrl + "/"; 90 | return; 91 | } 92 | if(baseUrl.endsWith("/")) { 93 | this.baseUrl = baseUrl + "rest/v1/"; 94 | return; 95 | } 96 | this.baseUrl = baseUrl + "/rest/v1/"; 97 | } 98 | 99 | public void setOpenUrlBase(String baseOpenUrl) { 100 | if(baseOpenUrl.contains("/perspective.req#/")) { 101 | if (baseOpenUrl.contains("items/")) { 102 | if (baseOpenUrl.endsWith("/")) { 103 | this.openUrlBase = baseOpenUrl; 104 | return; 105 | } 106 | this.openUrlBase = baseOpenUrl + "/"; 107 | return; 108 | } 109 | else { 110 | if(baseOpenUrl.endsWith("/")){ 111 | this.openUrlBase = baseOpenUrl + "items/"; 112 | } 113 | else { 114 | this.openUrlBase = baseOpenUrl + "/items/"; 115 | } 116 | } 117 | } else { 118 | if (baseOpenUrl.endsWith("/")) { 119 | this.openUrlBase = baseOpenUrl + "perspective.req#/items/"; 120 | return; 121 | } 122 | this.openUrlBase = baseOpenUrl + "/perspective.req#/items/"; 123 | } 124 | } 125 | 126 | public String getUsername() { 127 | return username; 128 | } 129 | 130 | public void setUsername(String username) { 131 | this.username = username; 132 | } 133 | 134 | public String getPassword() { 135 | return password; 136 | } 137 | 138 | public void setPassword(String password) { 139 | this.password = password; 140 | } 141 | 142 | public JsonHandler getJson() { 143 | return json; 144 | } 145 | 146 | public void setJson(JsonHandler json) { 147 | this.json = json; 148 | } 149 | 150 | public HttpClient getHttpClient() { 151 | return httpClient; 152 | } 153 | 154 | public void setHttpClient(HttpClient httpClient) { 155 | this.httpClient = httpClient; 156 | } 157 | 158 | public Integer getResourceTimeOut() { 159 | return resourceTimeOut; 160 | } 161 | 162 | public void setResourceTimeOut(Integer resourceTimeOut) { 163 | this.resourceTimeOut = resourceTimeOut; 164 | } 165 | 166 | public String getOpenUrlBase() { 167 | return this.openUrlBase; 168 | } 169 | 170 | public void setApiKey(String apiKey) { 171 | this.apiKey = apiKey; 172 | } 173 | 174 | public String getApiKey() { 175 | return this.apiKey; 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/JamaParent.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 5 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItem; 6 | 7 | import java.util.List; 8 | 9 | // Decorator 10 | public interface JamaParent { 11 | List getChildren() throws RestClientException; 12 | // void addChild(JamaItem jamaItem) throws RestClientException; 13 | boolean isProject(); 14 | void makeChildOf(JamaParent jamaParent) throws RestClientException; 15 | Integer getId(); 16 | JamaInstance getJamaInstance(); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/exception/ItemCreationException.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.exception; 2 | 3 | public class ItemCreationException extends RestClientException { 4 | public ItemCreationException() { 5 | } 6 | 7 | public ItemCreationException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/exception/ItemEditException.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.exception; 2 | 3 | public class ItemEditException extends RestClientException{ 4 | public ItemEditException() { 5 | } 6 | 7 | public ItemEditException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/exception/JamaApiException.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.exception; 2 | 3 | public class JamaApiException extends RestClientException { 4 | private int statusCode; 5 | private String responseMessage; 6 | 7 | public JamaApiException(int statusCode, String responseMessage) { 8 | this.statusCode = statusCode; 9 | this.responseMessage = responseMessage; 10 | } 11 | 12 | @Override 13 | public String getMessage() { 14 | return "Error response from Jama API: \n" + responseMessage; 15 | } 16 | 17 | @Override 18 | public String toString() { return getStatusCode() + ": " + getMessage(); } 19 | 20 | private int getStatusCode() { 21 | return statusCode; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/exception/JamaFieldNotFound.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.exception; 2 | 3 | public class JamaFieldNotFound extends RestClientException{ 4 | public JamaFieldNotFound() { 5 | } 6 | 7 | public JamaFieldNotFound(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/exception/JamaTypeMismatchException.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.exception; 2 | 3 | public class JamaTypeMismatchException extends RestClientException { 4 | public JamaTypeMismatchException () {} 5 | 6 | public JamaTypeMismatchException (String message) { 7 | super(message); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/exception/JsonException.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.exception; 2 | 3 | public class JsonException extends RestClientException { 4 | public JsonException(String message) { 5 | super(message); 6 | } 7 | public JsonException(Exception e) { 8 | super(e); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/exception/RestClientException.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.exception; 2 | 3 | public class RestClientException extends Exception { 4 | public RestClientException() {} 5 | 6 | public RestClientException(String message) { 7 | super(message); 8 | } 9 | 10 | public RestClientException(Exception e) { 11 | super(e.getMessage()); 12 | setStackTrace(e.getStackTrace()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/exception/UnexpectedJamaResponseException.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.exception; 2 | 3 | public class UnexpectedJamaResponseException extends RuntimeException { 4 | public UnexpectedJamaResponseException(String message) { 5 | super(message); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/httpconnection/ApacheHttpClient.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.httpconnection; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaApiException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import org.apache.http.Header; 6 | import org.apache.http.HttpEntity; 7 | import org.apache.http.HttpRequest; 8 | import org.apache.http.HttpResponse; 9 | import org.apache.http.auth.AuthenticationException; 10 | import org.apache.http.auth.UsernamePasswordCredentials; 11 | import org.apache.http.client.methods.*; 12 | import org.apache.http.config.SocketConfig; 13 | import org.apache.http.conn.ssl.NoopHostnameVerifier; 14 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; 15 | import org.apache.http.entity.ContentType; 16 | import org.apache.http.entity.StringEntity; 17 | import org.apache.http.entity.mime.MultipartEntityBuilder; 18 | import org.apache.http.impl.auth.BasicScheme; 19 | import org.apache.http.impl.client.HttpClients; 20 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 21 | import org.apache.http.ssl.SSLContexts; 22 | import org.apache.http.ssl.TrustStrategy; 23 | import org.apache.http.util.EntityUtils; 24 | 25 | import javax.net.ssl.SSLContext; 26 | import java.io.*; 27 | import java.security.cert.CertificateException; 28 | import java.security.cert.X509Certificate; 29 | import java.util.concurrent.TimeUnit; 30 | import java.util.logging.Level; 31 | import java.util.logging.Logger; 32 | 33 | public class ApacheHttpClient implements HttpClient { 34 | private final Logger log = Logger.getLogger(ApacheHttpClient.class.getName()); 35 | 36 | private org.apache.http.client.HttpClient client; 37 | 38 | public ApacheHttpClient() throws RestClientException { 39 | PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); 40 | connectionManager.setDefaultMaxPerRoute(10); 41 | try 42 | { 43 | SSLContext sslContext = SSLContexts.custom() 44 | .loadTrustMaterial(null, new TrustStrategy() { 45 | @Override 46 | public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { 47 | return true; 48 | } 49 | }) 50 | .build(); 51 | SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( 52 | sslContext, 53 | NoopHostnameVerifier.INSTANCE); 54 | SocketConfig socketConfig = SocketConfig.custom() 55 | .setSoTimeout(20000) 56 | .build(); 57 | connectionManager.setDefaultSocketConfig(socketConfig); 58 | client = HttpClients 59 | .custom() 60 | .disableAuthCaching() 61 | .setConnectionTimeToLive(20, TimeUnit.SECONDS) 62 | .disableCookieManagement() 63 | .disableRedirectHandling() 64 | .setSSLSocketFactory(sslsf) 65 | .setSSLContext(sslContext) 66 | .build(); 67 | } 68 | catch (Exception e) 69 | { 70 | throw new RestClientException(e); 71 | } 72 | } 73 | 74 | private Header getAuthenticationHeader(UsernamePasswordCredentials credentials, HttpRequest request) throws RestClientException { 75 | try { 76 | return new BasicScheme().authenticate(credentials, request, null); 77 | } catch(AuthenticationException e) { 78 | log.log(Level.SEVERE, e.toString(), e); 79 | throw new RestClientException("Unable to create authentication header"); 80 | } 81 | } 82 | 83 | private String getEntityContentOrNull(HttpEntity responseEntity) { 84 | try { 85 | // return (new BufferedReader(new InputStreamReader(responseEntity.getContent()))).readLine(); 86 | return (new BufferedReader(new InputStreamReader(responseEntity.getContent(), "UTF-8"))).readLine(); 87 | } catch(IOException | NullPointerException e) { 88 | return null; 89 | } 90 | } 91 | 92 | private Response execute(HttpRequestBase request, UsernamePasswordCredentials credentials) throws RestClientException { 93 | HttpEntity responseEntity = null; 94 | try { 95 | request.addHeader(getAuthenticationHeader(credentials, request)); 96 | HttpResponse rawResponse = client.execute(request); 97 | responseEntity = rawResponse.getEntity(); 98 | return new Response(rawResponse.getStatusLine().getStatusCode(), 99 | getEntityContentOrNull(rawResponse.getEntity())); 100 | } catch(IOException e) { 101 | throw new RestClientException(e); 102 | } finally { 103 | try { 104 | EntityUtils.consume(responseEntity); 105 | } catch(IOException e) { 106 | // Not throwing here because it's clean up 107 | log.log(Level.SEVERE, e.toString(), e); 108 | } 109 | } 110 | } 111 | 112 | public Response get(String url, String username, String password, String apiKey) throws RestClientException { 113 | // System.out.println("GET: " + url); 114 | HttpGet getRequest = new HttpGet(url); 115 | if(apiKey != null) 116 | getRequest.setHeader("api-key", apiKey); 117 | UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); 118 | Response response = execute(getRequest, credentials); 119 | if (response.getStatusCode() >= 400) { 120 | throw new JamaApiException(response.getStatusCode(), response.getResponse() + "\nURL: " + url); 121 | } 122 | return response; 123 | } 124 | 125 | public Response delete(String url, String username, String password, String apiKey) throws RestClientException { 126 | UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); 127 | HttpDelete deleteRequest = new HttpDelete(url); 128 | if(apiKey != null) 129 | deleteRequest.setHeader("api-key", apiKey); 130 | Response response = execute(deleteRequest, credentials); 131 | if (response.getStatusCode() >= 400) { 132 | throw new JamaApiException(response.getStatusCode(), response.getResponse() + "\nURL: " + url); 133 | } 134 | return response; 135 | } 136 | 137 | public Response post(String url, String username, String password, String apiKey, String payload) throws RestClientException { 138 | UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); 139 | HttpPost postRequest = new HttpPost(url); 140 | if(apiKey != null) 141 | postRequest.setHeader("api-key", apiKey); 142 | StringEntity body = new StringEntity(payload, "UTF-8"); 143 | body.setContentType("application/json"); 144 | postRequest.setEntity(body); 145 | Response response = execute(postRequest, credentials); 146 | if(response.getStatusCode() >= 400) { 147 | throw new JamaApiException(response.getStatusCode(), response.getResponse() + "\nURL: " + url + "\nPayload: " + payload); 148 | } 149 | return response; 150 | } 151 | 152 | public Response put(String url, String username, String password, String apiKey, String payload) throws RestClientException { 153 | System.out.println("PUT: " + url); 154 | UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); 155 | HttpPut putRequest = new HttpPut(url); 156 | if(apiKey != null) 157 | putRequest.setHeader("api-key", apiKey); 158 | StringEntity body = new StringEntity(payload, "UTF-8"); 159 | body.setContentType("application/json"); 160 | putRequest.setEntity(body); 161 | Response response = execute(putRequest, credentials); 162 | if(response.getStatusCode() >= 400) { 163 | throw new JamaApiException(response.getStatusCode(), response.getResponse() + "\nURL: " + url + "\nPayload: " + payload); 164 | } 165 | return response; 166 | } 167 | 168 | 169 | public Response putFile(String url, String username, String password, String apiKey, File file) throws RestClientException { 170 | UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); 171 | HttpPut putRequest = new HttpPut(url); 172 | if(apiKey != null) 173 | putRequest.setHeader("api-key", apiKey); 174 | MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); 175 | multipartEntityBuilder.addBinaryBody("file", file); 176 | multipartEntityBuilder.setContentType(ContentType.MULTIPART_FORM_DATA); 177 | HttpEntity fileEntity = multipartEntityBuilder.build(); 178 | putRequest.setEntity(fileEntity); 179 | Response response = execute(putRequest, credentials); 180 | if(response.getStatusCode() >= 400) { 181 | throw new JamaApiException(response.getStatusCode(), response.getResponse() + "\nURL: " + url + ", Filename: " + file.getName()); 182 | } 183 | return response; 184 | } 185 | 186 | public FileResponse getFile(String url, String username, String password, String apiKey) throws RestClientException { 187 | UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); 188 | HttpEntity responseEntity = null; 189 | HttpGet request = new HttpGet(url); 190 | if(apiKey != null) 191 | request.setHeader("api-key", apiKey); 192 | try { 193 | request.addHeader(getAuthenticationHeader(credentials, request)); 194 | HttpResponse rawResponse = client.execute(request); 195 | responseEntity = rawResponse.getEntity(); 196 | return new FileResponse(rawResponse.getStatusLine().getStatusCode(), 197 | rawResponse.getEntity().getContent()); 198 | } catch (IOException e) { 199 | throw new RestClientException(e); 200 | } finally { 201 | try { 202 | EntityUtils.consume(responseEntity); 203 | } catch (IOException e) { 204 | // Not throwing here because it's clean up 205 | log.log(Level.SEVERE, e.toString(), e); 206 | } 207 | } 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/httpconnection/FileResponse.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.httpconnection; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.httpconnection.Response; 5 | import org.apache.commons.io.IOUtils; 6 | 7 | import java.io.File; 8 | import java.io.FileOutputStream; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | 12 | public class FileResponse extends Response { 13 | private byte[] fileBytes; 14 | 15 | public FileResponse(int statusCode, InputStream inputStream) throws IOException { 16 | super(statusCode, "File response"); 17 | this.fileBytes = IOUtils.toByteArray(inputStream); 18 | } 19 | 20 | public byte[] getFileData() { 21 | return fileBytes; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/httpconnection/HttpClient.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.httpconnection; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | 5 | import java.io.File; 6 | 7 | public interface HttpClient { 8 | Response get(String url, String username, String password, String apiKey) throws RestClientException; 9 | Response delete(String url, String username, String password, String apiKey) throws RestClientException; 10 | Response post(String url, String username, String password, String apiKey, String payload) throws RestClientException; 11 | Response put(String url, String username, String password, String apiKey, String payload) throws RestClientException; 12 | Response putFile(String url, String username, String password, String apiKey, File file) throws RestClientException; 13 | FileResponse getFile(String url, String username, String password, String apiKey) throws RestClientException; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/httpconnection/Response.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.httpconnection; 2 | 3 | public class Response { 4 | private int statusCode; 5 | private String response; 6 | 7 | public Response(int statusCode, String response) { 8 | this.statusCode = statusCode; 9 | this.response = response; 10 | } 11 | 12 | public int getStatusCode() { 13 | return statusCode; 14 | } 15 | 16 | public String getResponse() { 17 | return response; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/httpconnection/TestHttpClient.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.httpconnection; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import org.json.JSONException; 5 | import org.json.simple.JSONObject; 6 | import org.json.simple.parser.JSONParser; 7 | import org.json.simple.parser.ParseException; 8 | import org.skyscreamer.jsonassert.JSONAssert; 9 | 10 | import java.io.File; 11 | import java.io.FileReader; 12 | import java.io.IOException; 13 | 14 | public class TestHttpClient implements HttpClient { 15 | private JSONObject responses; 16 | private JSONObject payloads; 17 | public TestHttpClient() { 18 | JSONParser parser = new JSONParser(); 19 | try { 20 | responses = (JSONObject)parser.parse(new FileReader("testData" + File.separator + "testApiResponses.json")); 21 | payloads = (JSONObject)parser.parse(new FileReader("testData" + File.separator + "testApiPayloads.json")); 22 | } catch (IOException | ParseException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | 27 | @Override 28 | public Response get(String url, String username, String password, String apiKey) throws RestClientException { 29 | String key = "GET " + url; 30 | JSONObject responseObject = (JSONObject)responses.get(key); 31 | String response = ((JSONObject)responseObject.get("response")).toJSONString(); 32 | Integer statusCode = (int)(long)responseObject.get("statusCode"); 33 | 34 | System.out.println(key); 35 | return new Response(statusCode, response); 36 | } 37 | 38 | @Override 39 | public Response delete(String url, String username, String password, String apiKey) throws RestClientException { 40 | return null; 41 | } 42 | 43 | @Override 44 | public Response post(String url, String username, String password, String apiKey, String payload) throws RestClientException { 45 | String key = "POST " + url; 46 | JSONObject payloadObject = (JSONObject)payloads.get(key); 47 | 48 | String expectedPayload = ((JSONObject)payloadObject.get("payload")).toJSONString(); 49 | String response = ((JSONObject)payloadObject.get("response")).toJSONString(); 50 | Integer statusCode = (int)(long)payloadObject.get("statusCode"); 51 | 52 | if(expectedPayload == null) { 53 | throw new RestClientException("Payloads object returned null for POST " + url); 54 | } 55 | try { 56 | JSONAssert.assertEquals(expectedPayload, payload, true); 57 | } catch (JSONException e) { 58 | throw new RestClientException(e); 59 | } 60 | 61 | return new Response(statusCode, response); 62 | } 63 | 64 | @Override 65 | public Response put(String url, String username, String password, String apiKey, String payload) throws RestClientException { 66 | return null; 67 | } 68 | 69 | @Override 70 | public Response putFile(String url, String username, String password, String apiKey, File file) throws RestClientException { 71 | return null; 72 | } 73 | 74 | @Override 75 | public FileResponse getFile(String url, String username, String password, String apiKey) throws RestClientException { 76 | return null; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamaclient/JamaClient.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamaclient; 2 | 3 | import com.jamasoftware.services.restclient.httpconnection.FileResponse; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 6 | import com.jamasoftware.services.restclient.httpconnection.Response; 7 | import com.jamasoftware.services.restclient.exception.RestClientException; 8 | import com.jamasoftware.services.restclient.httpconnection.HttpClient; 9 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 10 | import com.jamasoftware.services.restclient.json.JsonHandler; 11 | 12 | import java.io.FileWriter; 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | public class JamaClient { 17 | private HttpClient httpClient; 18 | private JsonHandler json; 19 | private String username; 20 | private String password; 21 | private String baseUrl; 22 | private String linkUrl; 23 | private String apiKey = null; 24 | 25 | public JamaClient(HttpClient httpClient, JsonHandler json, String baseUrl, String username, String password) { 26 | this.httpClient = httpClient; 27 | this.json = json; 28 | this.baseUrl = baseUrl; 29 | this.username = username; 30 | this.password = password; 31 | } 32 | 33 | public JamaClient(HttpClient httpClient, JsonHandler json, String baseUrl, String username, String password, String linkUrl, String apiKey) { 34 | this.httpClient = httpClient; 35 | this.json = json; 36 | this.baseUrl = baseUrl; 37 | this.username = username; 38 | this.password = password; 39 | this.linkUrl = linkUrl; 40 | this.apiKey = apiKey; 41 | } 42 | 43 | public JamaDomainObject getResource(String resource, JamaInstance jamaInstance) throws RestClientException { 44 | Response response = httpClient.get(baseUrl + resource, username, password, apiKey); 45 | return json.deserialize(response.getResponse(), jamaInstance); 46 | } 47 | 48 | // public JamaPage getPage(String url, JamaInstance jamaInstance) throws RestClientException { 49 | // return getPage(url, jamaInstance); 50 | // } 51 | 52 | public JamaPage getPage(String url, JamaInstance jamaInstance) throws RestClientException { 53 | return getPage(url, "", jamaInstance); 54 | } 55 | 56 | 57 | public JamaPage getPage(String url, String startAt, JamaInstance jamaInstance) throws RestClientException { 58 | Response response = httpClient.get(url + startAt, username, password, apiKey); 59 | JamaPage page = json.getPage(response.getResponse(), jamaInstance); 60 | page.setJamaClient(this); 61 | page.setUrl(url); 62 | return page; 63 | } 64 | 65 | public List getAll(String url, JamaInstance jamaInstance) throws RestClientException { 66 | List results = new ArrayList<>(); 67 | JamaPage page = getPage(url, jamaInstance); 68 | results.addAll(page.getResults()); 69 | while(page.hasNext()) { 70 | page = page.getNext(jamaInstance); 71 | results.addAll(page.getResults()); 72 | } 73 | return results; 74 | } 75 | 76 | 77 | public void ping() throws RestClientException { 78 | httpClient.get(baseUrl, username, password, apiKey); 79 | } 80 | 81 | public void putRaw(String url, String payload) throws RestClientException { 82 | httpClient.put(url, username, password, apiKey, payload); 83 | } 84 | 85 | public void deleteRaw(String url) throws RestClientException { 86 | httpClient.delete(url, username, password, apiKey); 87 | } 88 | 89 | public void delete(String resource) throws RestClientException { 90 | deleteRaw(baseUrl + resource); 91 | } 92 | 93 | public void put(String resource, LazyResource payload) throws RestClientException { 94 | // System.out.println(json.serialize(payload)); 95 | putRaw(baseUrl + resource, json.serializeEdited(payload)); 96 | } 97 | public Response postRaw(String url, String payload) throws RestClientException { 98 | return httpClient.post(url, username, password, apiKey, payload); 99 | // System.out.println(response.getResponse()); 100 | } 101 | 102 | public Integer post(String resource, LazyResource payload) throws RestClientException { 103 | Response response = postRaw(baseUrl + resource, json.serializeCreated(payload)); 104 | return json.deserializeLocation(response.getResponse()); 105 | } 106 | 107 | public byte[] getItemTypeImage(String url) throws RestClientException{ 108 | String domain = url.substring(0, url.indexOf("/img/")); 109 | if(!baseUrl.contains(domain)){ 110 | throw new RestClientException("Not a valid Item Type image URL: \"" + url + "\""); 111 | } 112 | FileResponse response = httpClient.getFile(url, username, password, apiKey); 113 | return response.getFileData(); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamaclient/JamaPage.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamaclient; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 5 | import com.jamasoftware.services.restclient.exception.RestClientException; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public class JamaPage { 11 | private JamaClient jamaClient; 12 | private String url; 13 | private int startIndex; 14 | private int resultCount; 15 | private int totalResults; 16 | private int maxResults; 17 | 18 | private List results = new ArrayList<>(); 19 | 20 | public JamaPage(int startIndex, int resultCount, int totalResults, int maxResults) { 21 | this.startIndex = startIndex; 22 | this.resultCount = resultCount; 23 | this.totalResults = totalResults; 24 | this.maxResults = maxResults; 25 | } 26 | 27 | public JamaPage(int startIndex, int resultCount, int totalResults) { 28 | this(startIndex, resultCount, totalResults, 20); 29 | } 30 | 31 | public boolean hasNext() { 32 | return startIndex + resultCount <= totalResults; 33 | } 34 | 35 | 36 | 37 | public JamaPage getNext(JamaInstance jamaInstance) throws RestClientException { 38 | int nextPageStart = startIndex + maxResults; 39 | String delim = !url.contains("?") ? "?" : "&"; 40 | return jamaClient.getPage(url, delim + "startAt=" + nextPageStart, jamaInstance); 41 | } 42 | 43 | public void addResource(JamaDomainObject jamaDomainObject) { 44 | if(jamaDomainObject != null) { 45 | results.add(jamaDomainObject); 46 | } 47 | } 48 | 49 | public List getResults() { 50 | return results; 51 | } 52 | 53 | public void setJamaClient(JamaClient jamaClient) { 54 | this.jamaClient = jamaClient; 55 | } 56 | 57 | public void setUrl(String url) { 58 | this.url = url; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/JamaLocation.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain; 2 | 3 | import com.jamasoftware.services.restclient.JamaParent; 4 | 5 | public class JamaLocation { 6 | private Integer sortOrder; 7 | private Integer globalSortOrder; 8 | private String sequence; 9 | private JamaParent parent; 10 | 11 | public Integer getSortOrder() { 12 | return sortOrder; 13 | } 14 | 15 | public void setSortOrder(Integer sortOrder) { 16 | this.sortOrder = sortOrder; 17 | } 18 | 19 | public Integer getGlobalSortOrder() { 20 | return globalSortOrder; 21 | } 22 | 23 | public void setGlobalSortOrder(Integer globalSortOrder) { 24 | this.globalSortOrder = globalSortOrder; 25 | } 26 | 27 | public String getSequence() { 28 | return sequence; 29 | } 30 | 31 | public void setSequence(String sequence) { 32 | this.sequence = sequence; 33 | } 34 | 35 | public JamaParent getParent() { 36 | return parent; 37 | } 38 | 39 | public void setParent(JamaParent parent) { 40 | this.parent = parent; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/JamaRichText.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain; 2 | 3 | public class JamaRichText { 4 | private String value; 5 | 6 | public String getValue() { 7 | return value; 8 | } 9 | 10 | public void setValue(String value) { 11 | this.value = value; 12 | } 13 | 14 | @Override 15 | public String toString() { 16 | return value; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/LockStatus.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaUser; 4 | 5 | import java.util.Date; 6 | 7 | public class LockStatus { 8 | private boolean isLocked; 9 | private Date lastLocked; 10 | private JamaUser lockedBy; 11 | 12 | public boolean isLocked() { 13 | return isLocked; 14 | } 15 | 16 | public void setLocked(boolean locked) { 17 | isLocked = locked; 18 | } 19 | 20 | public Date getLastLocked() { 21 | return lastLocked; 22 | } 23 | 24 | public void setLastLocked(Date lastLocked) { 25 | this.lastLocked = lastLocked; 26 | } 27 | 28 | public JamaUser getLockedBy() { 29 | return lockedBy; 30 | } 31 | 32 | public void setLockedBy(JamaUser lockedBy) { 33 | this.lockedBy = lockedBy; 34 | } 35 | 36 | @Override 37 | public String toString(){ 38 | return isLocked ? "Locked" : "Not locked"; 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/SerializableJamaDomainObject.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | 5 | public interface SerializableJamaDomainObject extends JamaDomainObject { 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/TestCaseStep.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain; 2 | 3 | public class TestCaseStep { 4 | private String action; 5 | private String expectedResult; 6 | private String notes; 7 | 8 | public String getAction() { 9 | return action; 10 | } 11 | 12 | public void setAction(String action) { 13 | this.action = action; 14 | } 15 | 16 | public String getExpectedResult() { 17 | return expectedResult; 18 | } 19 | 20 | public void setExpectedResult(String expectedResult) { 21 | this.expectedResult = expectedResult; 22 | } 23 | 24 | public String getNotes() { 25 | return notes; 26 | } 27 | 28 | public void setNotes(String notes) { 29 | this.notes = notes; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return action; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/core/JamaDomainObject.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.core; 2 | 3 | public interface JamaDomainObject {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/core/JamaInstance.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.core; 2 | 3 | import com.jamasoftware.services.restclient.JamaConfig; 4 | import com.jamasoftware.services.restclient.JamaParent; 5 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.*; 6 | import com.jamasoftware.services.restclient.exception.RestClientException; 7 | import com.jamasoftware.services.restclient.jamaclient.JamaClient; 8 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingItem; 9 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingRelationship; 10 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingResource; 11 | import com.jamasoftware.services.restclient.util.CompareUtil; 12 | 13 | import java.lang.ref.WeakReference; 14 | import java.util.ArrayList; 15 | import java.util.HashMap; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | public class JamaInstance implements JamaDomainObject { 20 | private JamaClient jamaClient; 21 | private JamaConfig jamaConfig; 22 | private Integer resourceTimeOut; 23 | private JamaUser currentUser; 24 | private ItemTypeList itemTypeList; 25 | private RelationshipTypeList relationshipTypeList; 26 | 27 | private Map> resourcePool = new HashMap<>(); 28 | 29 | public JamaInstance(JamaConfig jamaConfig) { 30 | this.jamaConfig = jamaConfig; 31 | this.resourceTimeOut = jamaConfig.getResourceTimeOut(); 32 | this.jamaClient = new JamaClient( 33 | jamaConfig.getHttpClient(), 34 | jamaConfig.getJson(), 35 | jamaConfig.getBaseUrl(), 36 | jamaConfig.getUsername(), 37 | jamaConfig.getPassword(), 38 | jamaConfig.getOpenUrlBase(), 39 | jamaConfig.getApiKey()); 40 | } 41 | 42 | private JamaDomainObject getPoolOrNull(String key) { 43 | WeakReference wr = resourcePool.get(key); 44 | return wr == null ? null : wr.get(); 45 | } 46 | 47 | public JamaDomainObject checkPool(Class clazz, int id) { 48 | return getPoolOrNull(clazz.getName() + id); 49 | } 50 | 51 | public void addToPool(Class clazz, int id, JamaDomainObject jamaDomainObject) { 52 | resourcePool.put(clazz.getName() + id, new WeakReference<>(jamaDomainObject)); 53 | } 54 | 55 | private JamaDomainObject checkPool(JamaDomainObject fresh) { 56 | if(fresh instanceof LazyResource) { 57 | String key = fresh.getClass().getName() + ((LazyResource) fresh).getId(); 58 | LazyResource existingResource = (LazyResource) getPoolOrNull(key); 59 | if(existingResource != null) { 60 | existingResource.copyContentFrom(fresh); 61 | return existingResource; 62 | } 63 | resourcePool.put(key, new WeakReference<>(fresh)); 64 | } 65 | return fresh; 66 | } 67 | 68 | public JamaDomainObject getResource(String resource) throws RestClientException { 69 | JamaDomainObject retrieved = jamaClient.getResource(resource, this); 70 | return checkPool(retrieved); 71 | } 72 | 73 | public List getResourceCollection(String resource) throws RestClientException { 74 | return getAll(resource); 75 | } 76 | 77 | public List getAll(String resource) throws RestClientException { 78 | List objects = jamaClient.getAll(jamaConfig.getBaseUrl() + resource, this); 79 | for(int i = 0; i < objects.size(); ++i) { 80 | objects.set(i, checkPool(objects.get(i))); 81 | } 82 | return objects; 83 | } 84 | 85 | 86 | public JamaProject getProject(int id) throws RestClientException{ 87 | String key = JamaProject.class.getName() + id; 88 | JamaProject project = (JamaProject) getPoolOrNull(key); 89 | if(project != null) { 90 | project.fetch(); 91 | } else { 92 | project = new JamaProject(); 93 | project.associate(id, this); 94 | resourcePool.put(key, new WeakReference<>((JamaDomainObject)project)); 95 | } 96 | return project; 97 | } 98 | 99 | public List getProjects() throws RestClientException { 100 | List projects = new ArrayList<>(); 101 | List jamaDomainObjects = getAll("projects"); 102 | for(JamaDomainObject jamaDomainObject : jamaDomainObjects) { 103 | JamaProject project = (JamaProject) jamaDomainObject; 104 | projects.add(project); 105 | } 106 | return projects; 107 | } 108 | 109 | public List getItemTypes() throws RestClientException { 110 | if(itemTypeList == null) { 111 | itemTypeList = new ItemTypeList(this); 112 | } 113 | return itemTypeList.getItemTypes(); 114 | } 115 | 116 | public JamaItemType getItemType(int id) throws RestClientException{ 117 | JamaItemType itemType = new JamaItemType(); 118 | itemType.associate(id, this); 119 | return itemType; 120 | } 121 | 122 | public JamaItemType getItemType(String name) throws RestClientException { 123 | List itemTypes = getItemTypes(); 124 | JamaItemType found = null; 125 | for(JamaItemType itemType : itemTypes) { 126 | if(CompareUtil.closeEnough(name, itemType.getDisplay())) { 127 | if(found != null) { 128 | throw new RestClientException("Multiple ItemTypes with the display: " + name); 129 | } 130 | found = itemType; 131 | } 132 | } 133 | return found; 134 | } 135 | 136 | public JamaItem getItem(int id) throws RestClientException { 137 | String key = JamaItem.class.getName() + id; 138 | JamaItem item = (JamaItem) getPoolOrNull(key); 139 | if(item != null) { 140 | item.fetch(); 141 | } else { 142 | item = new JamaItem(); 143 | item.associate(id, this); 144 | resourcePool.put(key, new WeakReference<>((JamaDomainObject)item)); 145 | } 146 | return item; 147 | } 148 | 149 | public JamaRelationship getRelationship(int id) throws RestClientException { 150 | String key = JamaRelationship.class.getName() + id; 151 | JamaRelationship relationship = (JamaRelationship) getPoolOrNull(key); 152 | if(relationship != null) { 153 | relationship.fetch(); 154 | } else { 155 | relationship = new JamaRelationship(); 156 | relationship.associate(id, this); 157 | resourcePool.put(key, new WeakReference<>((JamaDomainObject)relationship)); 158 | } 159 | return relationship; 160 | } 161 | 162 | public void deleteItem(int id) throws RestClientException { 163 | deleteRawData("items/" + id); 164 | } 165 | 166 | public void deleteRelationship(int id) throws RestClientException { 167 | deleteRawData("relationships/" + id); 168 | } 169 | 170 | void deleteRawData(String resource) throws RestClientException { 171 | jamaClient.deleteRaw(jamaConfig.getBaseUrl() + resource); 172 | } 173 | 174 | public void ping() throws RestClientException { 175 | jamaClient.ping(); 176 | } 177 | 178 | public Integer getResourceTimeOut() { 179 | return resourceTimeOut; 180 | } 181 | 182 | public JamaUser getCurrentUser() throws RestClientException{ 183 | if(currentUser == null) { 184 | currentUser = (JamaUser) getResource("users/current"); 185 | } 186 | return currentUser; 187 | } 188 | 189 | public void putRawData(String resource, String payload) throws RestClientException { 190 | jamaClient.putRaw(jamaConfig.getBaseUrl() + resource, payload); 191 | } 192 | 193 | public void postRawData(String resource, String payload) throws RestClientException { 194 | jamaClient.postRaw(jamaConfig.getBaseUrl() + resource, payload); 195 | } 196 | 197 | public byte[] retrieveItemTypeImage(String url) throws RestClientException { 198 | return jamaClient.getItemTypeImage(url); 199 | } 200 | 201 | public String getOpenUrl(JamaItem item) { 202 | return this.jamaConfig.getOpenUrlBase() + item.getId() + "?project=" + item.getProject().getId(); 203 | } 204 | 205 | public void setBaseOpenUrl(String baseOpenUrl) { 206 | this.jamaConfig.setOpenUrlBase(baseOpenUrl); 207 | } 208 | 209 | public void setResourceTimeOut(Integer resourceTimeOut) { 210 | this.resourceTimeOut = resourceTimeOut; 211 | } 212 | 213 | public StagingItem createItem(String name, JamaParent parent, JamaItemType itemType) throws RestClientException{ 214 | return (new StagingDispenser()).createStagingItem(this, name, parent, itemType); 215 | } 216 | 217 | protected void put(LazyResource lazyResource) throws RestClientException { 218 | jamaClient.put(lazyResource.getEditUrl(), lazyResource); 219 | } 220 | 221 | protected void delete(LazyResource lazyResource) throws RestClientException { 222 | jamaClient.delete(lazyResource.getDeleteUrl()); 223 | } 224 | 225 | protected LazyResource post(LazyResource lazyResource, Class clazz) throws RestClientException { 226 | Integer resourceId = jamaClient.post(lazyResource.getCreateUrl(), lazyResource); 227 | if(resourceId == null) { 228 | return null; 229 | } 230 | LazyResource createdResource; 231 | try { 232 | createdResource = (LazyResource)clazz.newInstance(); 233 | } catch (InstantiationException | IllegalAccessException e) { 234 | throw new RestClientException(e); 235 | } 236 | createdResource.associate(resourceId, this); 237 | return createdResource; 238 | } 239 | 240 | public StagingItem editItem(JamaItem jamaItem) throws RestClientException { 241 | jamaItem.fetch(); 242 | return (new StagingDispenser()).createStagingItem(jamaItem); 243 | } 244 | 245 | public StagingRelationship editRelationship(JamaRelationship jamaRelationship) throws RestClientException { 246 | jamaRelationship.fetch(); 247 | return (new StagingDispenser()).createStagingRelationship(jamaRelationship); 248 | } 249 | 250 | 251 | public List getRelationshipTypes() throws RestClientException { 252 | if(relationshipTypeList == null) { 253 | relationshipTypeList = new RelationshipTypeList(this); 254 | } 255 | return relationshipTypeList.getRelationshipTypes(); 256 | } 257 | } 258 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/core/LazyResource.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.core; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.LazyBase; 5 | 6 | import java.io.FileReader; 7 | import java.io.FileWriter; 8 | 9 | public abstract class LazyResource extends LazyBase implements JamaDomainObject { 10 | public void forceFetch() throws RestClientException { 11 | markFetch(); 12 | copyContentFrom(jamaInstance.getResource(getResourceUrl())); 13 | } 14 | 15 | protected abstract String getResourceUrl(); 16 | 17 | protected abstract void copyContentFrom(JamaDomainObject jamaDomainObject); 18 | 19 | protected abstract void writeContentTo(JamaDomainObject jamaDomainObject); 20 | 21 | public boolean isAssociated() { 22 | return getId() != null && jamaInstance != null; 23 | } 24 | protected void put() throws RestClientException { 25 | jamaInstance.put(this); 26 | } 27 | 28 | protected void deleteResource() throws RestClientException { 29 | jamaInstance.delete(this); 30 | } 31 | 32 | protected LazyResource post(Class clazz, JamaInstance jamaInstance) throws RestClientException { 33 | return jamaInstance.post(this, clazz); 34 | } 35 | 36 | protected LazyResource post(Class clazz) throws RestClientException { 37 | return jamaInstance.post(this, clazz); 38 | } 39 | 40 | protected String getEditUrl() throws RestClientException { 41 | throw new RestClientException("Unable to edit " + this.getClass() + " for item " + this); 42 | } 43 | 44 | protected String getCreateUrl() throws RestClientException { 45 | throw new RestClientException("Unable to create a new " + this.getClass()); 46 | } 47 | 48 | protected String getDeleteUrl() throws RestClientException { 49 | throw new RestClientException("Unable to delete " + this.getClass()); 50 | }; 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/core/StagingDispenser.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.core; 2 | 3 | import com.jamasoftware.services.restclient.JamaParent; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItem; 6 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItemType; 7 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaProject; 8 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaRelationship; 9 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingFactory; 10 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingItem; 11 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingRelationship; 12 | 13 | public class StagingDispenser extends StagingFactory { 14 | 15 | StagingDispenser() {} 16 | 17 | @Override 18 | protected StagingItem createStagingItem() { 19 | return super.createStagingItem(); 20 | } 21 | 22 | @Override 23 | protected StagingItem createStagingItem(JamaItem jamaItem) throws RestClientException { 24 | return super.createStagingItem(jamaItem); 25 | } 26 | 27 | @Override 28 | protected StagingItem createStagingItem(JamaInstance jamaInstance, String name, JamaParent parent, JamaItemType itemType) throws RestClientException { 29 | return super.createStagingItem(jamaInstance, name, parent, itemType); 30 | } 31 | 32 | 33 | @Override 34 | protected StagingRelationship createStagingRelationship() { 35 | return super.createStagingRelationship(); 36 | } 37 | 38 | @Override 39 | protected StagingRelationship createStagingRelationship(JamaRelationship jamaRelationship) throws RestClientException { 40 | return super.createStagingRelationship(jamaRelationship); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/CalculatedField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.CalculatedFieldValue; 4 | 5 | public class CalculatedField extends JamaField { 6 | @Override 7 | public CalculatedFieldValue getValue() { 8 | CalculatedFieldValue calculatedFieldValue = new CalculatedFieldValue(); 9 | populateFieldValue(calculatedFieldValue); 10 | return calculatedFieldValue; 11 | } 12 | 13 | public CalculatedField(String type) { 14 | super(type); 15 | } 16 | 17 | public CalculatedField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/DateField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.DateFieldValue; 4 | 5 | public class DateField extends JamaField { 6 | @Override 7 | public DateFieldValue getValue() { 8 | DateFieldValue dateFieldValue = new DateFieldValue(); 9 | populateFieldValue(dateFieldValue); 10 | return dateFieldValue; 11 | } 12 | 13 | public DateField(String type) { 14 | super(type); 15 | } 16 | 17 | public DateField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/FlagField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.FlagFieldValue; 4 | 5 | public class FlagField extends JamaField { 6 | @Override 7 | public FlagFieldValue getValue() { 8 | FlagFieldValue flagFieldValue = new FlagFieldValue(); 9 | populateFieldValue(flagFieldValue); 10 | return flagFieldValue; 11 | } 12 | 13 | public FlagField(String type) { 14 | super(type); 15 | } 16 | 17 | public FlagField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/IntegerField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.IntegerFieldValue; 4 | 5 | public class IntegerField extends JamaField { 6 | @Override 7 | public IntegerFieldValue getValue() { 8 | IntegerFieldValue integerFieldValue = new IntegerFieldValue(); 9 | populateFieldValue(integerFieldValue); 10 | return integerFieldValue; 11 | } 12 | 13 | public IntegerField(String type) { 14 | super(type); 15 | } 16 | 17 | public IntegerField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/JamaField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 4 | import com.jamasoftware.services.restclient.jamadomain.SerializableJamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.values.JamaFieldValue; 6 | 7 | import java.lang.reflect.Field; 8 | 9 | public abstract class JamaField implements SerializableJamaDomainObject { 10 | private JamaInstance jamaInstance; 11 | 12 | private int id; 13 | private String name; 14 | private String label; 15 | private boolean readOnly; 16 | private boolean required; 17 | private boolean triggerSuspect; 18 | private boolean synchronize; 19 | public final String type; 20 | 21 | public abstract JamaFieldValue getValue(); 22 | 23 | // public abstract String getType(); 24 | 25 | 26 | public JamaInstance getJamaInstance() { 27 | return jamaInstance; 28 | } 29 | 30 | public void setJamaInstance(JamaInstance jamaInstance) { 31 | this.jamaInstance = jamaInstance; 32 | } 33 | 34 | public int getId() { 35 | return id; 36 | } 37 | 38 | public void setId(int id) { 39 | this.id = id; 40 | } 41 | 42 | public String getName() { 43 | return name; 44 | } 45 | 46 | public void setName(String name) { 47 | if(name.contains("$")) { 48 | name = name.substring(0, name.indexOf("$")); 49 | } 50 | this.name = name; 51 | } 52 | 53 | public String getLabel() { 54 | return label; 55 | } 56 | 57 | public void setLabel(String label) { 58 | this.label = label; 59 | } 60 | 61 | public boolean isReadOnly() { 62 | return readOnly; 63 | } 64 | 65 | public void setReadOnly(boolean readOnly) { 66 | this.readOnly = readOnly; 67 | } 68 | 69 | public boolean isRequired() { 70 | return required; 71 | } 72 | 73 | public void setRequired(boolean required) { 74 | this.required = required; 75 | } 76 | 77 | public boolean isTriggerSuspect() { 78 | return triggerSuspect; 79 | } 80 | 81 | public void setTriggerSuspect(boolean triggerSuspect) { 82 | this.triggerSuspect = triggerSuspect; 83 | } 84 | 85 | public boolean isSynchronize() { 86 | return synchronize; 87 | } 88 | 89 | public void setSynchronize(boolean synchronize) { 90 | this.synchronize = synchronize; 91 | } 92 | 93 | protected void populateFieldValue(JamaFieldValue fieldValue) { 94 | fieldValue.setJamaInstance(jamaInstance); 95 | fieldValue.setName(name); 96 | fieldValue.setLabel(label); 97 | fieldValue.setField(this); 98 | } 99 | 100 | @Override 101 | public String toString() { 102 | return getName(); 103 | } 104 | 105 | public JamaField(String type) { 106 | this.type = type; 107 | } 108 | 109 | public JamaField() {this.type = null;} 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/MultiSelectField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.PickList; 4 | import com.jamasoftware.services.restclient.jamadomain.values.MultiSelectFieldValue; 5 | 6 | public class MultiSelectField extends JamaField { 7 | private PickList pickList; 8 | 9 | public PickList getPickList() { 10 | return pickList; 11 | } 12 | 13 | public void setPickList(PickList pickList) { 14 | this.pickList = pickList; 15 | } 16 | 17 | @Override 18 | public MultiSelectFieldValue getValue() { 19 | MultiSelectFieldValue multiSelectFieldValue = new MultiSelectFieldValue(); 20 | populateFieldValue(multiSelectFieldValue); 21 | return multiSelectFieldValue; 22 | } 23 | 24 | public MultiSelectField(String type) { 25 | super(type); 26 | } 27 | 28 | public MultiSelectField() { 29 | super(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/PickListField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.PickList; 4 | import com.jamasoftware.services.restclient.jamadomain.values.PickListFieldValue; 5 | 6 | public class PickListField extends JamaField { 7 | private int picklistId; 8 | private PickList pickList; 9 | 10 | public int getPicklistId() { 11 | return picklistId; 12 | } 13 | 14 | public void setPickList(PickList pickList) { 15 | this.pickList = pickList; 16 | } 17 | 18 | @Override 19 | public PickListFieldValue getValue() { 20 | PickListFieldValue pickListFieldValue = new PickListFieldValue(); 21 | populateFieldValue(pickListFieldValue); 22 | return pickListFieldValue; 23 | } 24 | 25 | public PickListField(String type) { 26 | super(type); 27 | } 28 | 29 | public PickListField() { 30 | super(); 31 | } 32 | 33 | 34 | public PickList getPickList() { 35 | return pickList; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/ProjectField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | import com.jamasoftware.services.restclient.jamadomain.values.ProjectFieldValue; 3 | 4 | public class ProjectField extends JamaField { 5 | @Override 6 | public ProjectFieldValue getValue() { 7 | ProjectFieldValue projectFieldValue = new ProjectFieldValue(); 8 | populateFieldValue(projectFieldValue); 9 | return projectFieldValue; 10 | } 11 | 12 | public ProjectField(String type) { 13 | super(type); 14 | } 15 | 16 | public ProjectField() { 17 | super(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/ReleaseField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.ReleaseFieldValue; 4 | 5 | public class ReleaseField extends JamaField { 6 | @Override 7 | public ReleaseFieldValue getValue() { 8 | ReleaseFieldValue releaseFieldValue = new ReleaseFieldValue(); 9 | populateFieldValue(releaseFieldValue); 10 | return releaseFieldValue; 11 | } 12 | 13 | public ReleaseField(String type) { 14 | super(type); 15 | } 16 | 17 | public ReleaseField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/RichTextField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.RichTextFieldValue; 4 | 5 | public class RichTextField extends JamaField { 6 | @Override 7 | public RichTextFieldValue getValue() { 8 | RichTextFieldValue richTextFieldValue = new RichTextFieldValue(); 9 | populateFieldValue(richTextFieldValue); 10 | return richTextFieldValue; 11 | } 12 | 13 | public RichTextField(String type) { 14 | super(type); 15 | } 16 | 17 | public RichTextField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/RollupField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.RollupFieldValue; 4 | 5 | public class RollupField extends JamaField { 6 | @Override 7 | public RollupFieldValue getValue() { 8 | RollupFieldValue rollupFieldValue = new RollupFieldValue(); 9 | populateFieldValue(rollupFieldValue); 10 | return rollupFieldValue; 11 | } 12 | 13 | public RollupField(String type) { 14 | super(type); 15 | } 16 | 17 | public RollupField() { 18 | super(); 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/TestCaseStatusField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.TestCaseStatusFieldValue; 4 | 5 | public class TestCaseStatusField extends JamaField { 6 | @Override 7 | public TestCaseStatusFieldValue getValue() { 8 | TestCaseStatusFieldValue testCaseStatusFieldValue = new TestCaseStatusFieldValue(); 9 | populateFieldValue(testCaseStatusFieldValue); 10 | return testCaseStatusFieldValue; 11 | } 12 | 13 | public TestCaseStatusField(String type) { 14 | super(type); 15 | } 16 | 17 | public TestCaseStatusField() { 18 | super(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/TestCaseStepsField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.TestCaseStepsFieldValue; 4 | 5 | public class TestCaseStepsField extends JamaField { 6 | @Override 7 | public TestCaseStepsFieldValue getValue() { 8 | TestCaseStepsFieldValue testCaseStepsFieldValue = new TestCaseStepsFieldValue(); 9 | populateFieldValue(testCaseStepsFieldValue); 10 | return testCaseStepsFieldValue; 11 | } 12 | 13 | public TestCaseStepsField(String type) { 14 | super(type); 15 | } 16 | 17 | public TestCaseStepsField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/TextBoxField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.TextBoxFieldValue; 4 | 5 | public class TextBoxField extends JamaField { 6 | @Override 7 | public TextBoxFieldValue getValue() { 8 | TextBoxFieldValue textBoxFieldValue = new TextBoxFieldValue(); 9 | populateFieldValue(textBoxFieldValue); 10 | return textBoxFieldValue; 11 | } 12 | 13 | public TextBoxField(String type) { 14 | super(type); 15 | } 16 | 17 | public TextBoxField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/TextField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.TextFieldValue; 4 | 5 | public class TextField extends JamaField { 6 | @Override 7 | public TextFieldValue getValue() { 8 | TextFieldValue textFieldValue = new TextFieldValue(); 9 | populateFieldValue(textFieldValue); 10 | return textFieldValue; 11 | } 12 | 13 | public TextField(String type) { 14 | super(type); 15 | } 16 | 17 | public TextField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/TimeField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.TimeFieldValue; 4 | 5 | public class TimeField extends JamaField { 6 | @Override 7 | public TimeFieldValue getValue() { 8 | TimeFieldValue timeFieldValue = new TimeFieldValue(); 9 | populateFieldValue(timeFieldValue); 10 | return timeFieldValue; 11 | } 12 | 13 | public TimeField(String type) { 14 | super(type); 15 | } 16 | 17 | public TimeField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/URLField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.URLFieldValue; 4 | 5 | public class URLField extends JamaField { 6 | @Override 7 | public URLFieldValue getValue() { 8 | URLFieldValue urlFieldValue = new URLFieldValue(); 9 | populateFieldValue(urlFieldValue); 10 | return urlFieldValue; 11 | } 12 | 13 | public URLField(String type) { 14 | super(type); 15 | } 16 | 17 | public URLField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/UnsupportedField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.UserFieldValue; 4 | 5 | /** 6 | * Jama Connect is a dynamic Cloud service, new Field types are potentially introduced. To cope with unknown and thus 7 | * unsupported field types, we introduce this {@link JamaField}. 8 | * 9 | * @author Jan mauersberger 10 | */ 11 | public class UnsupportedField extends JamaField { 12 | 13 | @Override 14 | public UserFieldValue getValue() { 15 | throw new RuntimeException("Unsupported field type " + getName()); //$NON-NLS-1$ 16 | } 17 | 18 | public UnsupportedField(String type) { 19 | super(type); 20 | } 21 | 22 | public UnsupportedField() { 23 | super(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/fields/UserField.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.fields; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.values.UserFieldValue; 4 | 5 | public class UserField extends JamaField { 6 | @Override 7 | public UserFieldValue getValue() { 8 | UserFieldValue userFieldValue = new UserFieldValue(); 9 | populateFieldValue(userFieldValue); 10 | return userFieldValue; 11 | } 12 | 13 | public UserField(String type) { 14 | super(type); 15 | } 16 | 17 | public UserField() { 18 | super(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/ChildrenList.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.JamaParent; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | public class ChildrenList extends LazyCollection { 12 | private final JamaParent parent; 13 | private List children; 14 | 15 | public ChildrenList(JamaParent parent) throws RestClientException { 16 | this.parent = parent; 17 | associate(parent.getId(), parent.getJamaInstance()); 18 | } 19 | 20 | @Override 21 | public String getCollectionUrl() { 22 | if(parent.isProject()) { 23 | return "items?project=" + getId() + "&rootOnly=true"; 24 | } 25 | return "items/" + getId() + "/children"; 26 | } 27 | 28 | @Override 29 | public void copyContentFrom(List objects) { 30 | List children = new ArrayList<>(); 31 | for(JamaDomainObject object : objects) { 32 | children.add((JamaItem) object); 33 | } 34 | this.children = children; 35 | } 36 | 37 | public List getChildren() { 38 | fetch(); 39 | /* 40 | * Problems have been reported that children is null at this stage. The reasons are not fully clear, an 41 | * exception should have been thrown in that case. Anyway, fix is to return an empty list instead. 42 | */ 43 | return Collections.unmodifiableList(children != null ? children : Collections.emptyList()); 44 | } 45 | 46 | public JamaParent getParent() { 47 | fetch(); 48 | return parent; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/ItemTypeList.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | public class ItemTypeList extends LazyCollection { 12 | private List itemTypeList; 13 | 14 | public ItemTypeList(JamaInstance jamaInstance) throws RestClientException { 15 | // this has an id of -1 because it doesn't have an Id associated with it in Jama 16 | associate(-1, jamaInstance); 17 | } 18 | 19 | @Override 20 | public String getCollectionUrl() { 21 | return "itemtypes"; 22 | } 23 | 24 | @Override 25 | public void copyContentFrom(List objectList) { 26 | List itemTypes = new ArrayList<>(); 27 | for(JamaDomainObject o : objectList) { 28 | itemTypes.add((JamaItemType) o); 29 | } 30 | 31 | this.itemTypeList = itemTypes; 32 | } 33 | 34 | public List getItemTypes() { 35 | fetch(); 36 | /* 37 | * Problems have been reported that itemTypeList is null at this stage. The reasons are not fully clear, an 38 | * exception should have been thrown in that case. Anyway, fix is to return an empty list instead. 39 | */ 40 | return Collections.unmodifiableList(itemTypeList != null ? itemTypeList : Collections.emptyList()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/JamaItem.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.JamaParent; 4 | import com.jamasoftware.services.restclient.exception.ItemEditException; 5 | import com.jamasoftware.services.restclient.exception.JsonException; 6 | import com.jamasoftware.services.restclient.exception.RestClientException; 7 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 8 | import com.jamasoftware.services.restclient.jamadomain.JamaLocation; 9 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 10 | import com.jamasoftware.services.restclient.jamadomain.LockStatus; 11 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingItem; 12 | import com.jamasoftware.services.restclient.jamadomain.values.JamaFieldValue; 13 | import com.jamasoftware.services.restclient.jamadomain.values.TextFieldValue; 14 | 15 | import java.util.ArrayList; 16 | import java.util.Collections; 17 | import java.util.Date; 18 | import java.util.List; 19 | 20 | public class JamaItem extends LazyResource implements JamaParent{ 21 | protected TextFieldValue name; 22 | protected String globalId; 23 | protected String documentKey; 24 | protected JamaProject project; 25 | protected JamaItemType itemType; 26 | protected JamaItemType childItemType; 27 | protected Date createdDate; 28 | protected Date modifiedDate; 29 | protected Date lastActivityDate; 30 | protected JamaUser createdBy; 31 | protected JamaUser modifiedBy; 32 | protected JamaLocation location; 33 | protected List fieldValues = new ArrayList<>(); 34 | protected LockStatus lockStatus; 35 | private ChildrenList children; 36 | 37 | public JamaItem(){} 38 | 39 | public JamaItem(JamaItem item) { 40 | copyContentFrom(item); 41 | } 42 | 43 | public JamaParent getParent() { 44 | fetch(); 45 | return location.getParent(); 46 | } 47 | 48 | public String getSequence() { 49 | fetch(); 50 | return location.getSequence(); 51 | } 52 | 53 | @Override 54 | public List getChildren() throws RestClientException { 55 | if(children == null) { 56 | children = new ChildrenList(this); 57 | } 58 | return Collections.unmodifiableList(children.getChildren()); 59 | } 60 | 61 | // @Override 62 | // public void addChild(JamaItem jamaItem) throws RestClientException { 63 | // throw new ItemEditException("You are attempting to update an item's location. This can only be done using the " + 64 | // "\'setParent\' method on an edited item."); 65 | // } 66 | 67 | @Override 68 | public boolean isProject() { 69 | return false; 70 | } 71 | 72 | @Override 73 | public void makeChildOf(JamaParent jamaParent) throws RestClientException { 74 | this.location.setParent(jamaParent); 75 | } 76 | 77 | @Override 78 | protected String getResourceUrl() { 79 | return "items/" + getId(); 80 | } 81 | 82 | 83 | @Override 84 | protected void copyContentFrom(JamaDomainObject jamaDomainObject) { 85 | checkType(JamaItem.class, jamaDomainObject); 86 | 87 | JamaItem item = (JamaItem) jamaDomainObject; 88 | name = item.name; 89 | globalId = item.globalId; 90 | documentKey = item.documentKey; 91 | project = item.project; 92 | itemType = item.itemType; 93 | childItemType = item.childItemType; 94 | createdDate = item.createdDate; 95 | modifiedDate = item.modifiedDate; 96 | lastActivityDate = item.lastActivityDate; 97 | createdBy = item.createdBy; 98 | modifiedBy = item.modifiedBy; 99 | location = item.location; 100 | fieldValues = item.fieldValues; 101 | lockStatus = item.lockStatus; 102 | } 103 | 104 | @Override 105 | protected void writeContentTo(JamaDomainObject jamaItem){ 106 | checkType(JamaItem.class, jamaItem); 107 | ((JamaItem)jamaItem).copyContentFrom(this); 108 | } 109 | 110 | public TextFieldValue getName() { 111 | fetch(); 112 | return name; 113 | } 114 | 115 | public String getGlobalId() { 116 | fetch(); 117 | return globalId; 118 | } 119 | 120 | public JamaProject getProject() { 121 | fetch(); 122 | return project; 123 | } 124 | 125 | public JamaItemType getItemType() { 126 | fetch(); 127 | return itemType; 128 | } 129 | 130 | 131 | public JamaItemType getChildItemType() { 132 | fetch(); 133 | return childItemType; 134 | } 135 | 136 | public JamaUser getCreatedBy() { 137 | fetch(); 138 | return createdBy; 139 | } 140 | 141 | public JamaUser getModifiedBy() { 142 | fetch(); 143 | return modifiedBy; 144 | } 145 | 146 | protected JamaLocation getLocation() { 147 | fetch(); 148 | return location; 149 | } 150 | 151 | public List getFieldValues() { 152 | fetch(); 153 | return fieldValues; 154 | } 155 | 156 | public JamaFieldValue getFieldValueByName(String fieldName) { 157 | if(fieldName.equals("name")) { 158 | return getName(); 159 | } 160 | for(JamaFieldValue fieldValue : getFieldValues()) { 161 | if(fieldValue.getName().equals(fieldName)) 162 | return fieldValue; 163 | } 164 | return null; 165 | } 166 | 167 | public String getDocumentKey() { 168 | fetch(); 169 | return documentKey; 170 | } 171 | 172 | public Date getCreatedDate() { 173 | fetch(); 174 | return createdDate; 175 | } 176 | 177 | public Date getModifiedDate() { 178 | fetch(); 179 | return modifiedDate; 180 | } 181 | 182 | public Date getLastActivityDate() { 183 | fetch(); 184 | return lastActivityDate; 185 | } 186 | 187 | public boolean isLocked() { 188 | fetch(); 189 | return lockStatus.isLocked(); 190 | } 191 | 192 | public boolean isLockedByCurrentUser() throws RestClientException{ 193 | // fetch is called in isLocked so we removed it here 194 | return isLocked() && lockStatus.getLockedBy().getId().equals(getJamaInstance().getCurrentUser().getId()); 195 | } 196 | 197 | public Date lastLockedDate(){ 198 | fetch(); 199 | return lockStatus.getLastLocked(); 200 | } 201 | 202 | public JamaUser lockedBy() { 203 | return lockStatus.getLockedBy(); 204 | } 205 | 206 | public List getDownstreamRelationships() throws RestClientException { 207 | List objects = getJamaInstance().getAll("items/" + getId() + "/downstreamrelationships"); 208 | List relationships = new ArrayList<>(); 209 | for(JamaDomainObject o : objects) { 210 | relationships.add((JamaRelationship)o); 211 | } 212 | return relationships; 213 | } 214 | 215 | public List getUpstreamRelationships() throws RestClientException { 216 | List objects = getJamaInstance().getAll("items/" + getId() + "/upstreamrelationships"); 217 | List relationships = new ArrayList<>(); 218 | for(JamaDomainObject object : objects){ 219 | relationships.add((JamaRelationship) object); 220 | } 221 | return relationships; 222 | } 223 | 224 | public List getDownstreamItems() throws RestClientException { 225 | List objects = getJamaInstance().getAll("items/" + getId() + "/downstreamrelated"); 226 | List items = new ArrayList<>(); 227 | for(JamaDomainObject o : objects) { 228 | items.add((JamaItem)o); 229 | } 230 | return items; 231 | } 232 | 233 | public List getUpstreamItems() throws RestClientException { 234 | List objects = getJamaInstance().getAll("items/" + getId() + "/upstreamrelated"); 235 | List items = new ArrayList<>(); 236 | for(JamaDomainObject o : objects) { 237 | items.add((JamaItem)o); 238 | } 239 | return items; 240 | } 241 | 242 | public void forceLockItem() throws RestClientException { 243 | sendLockRequest(false); 244 | sendLockRequest(true); 245 | forceFetch(); 246 | } 247 | 248 | public boolean acquireLock() throws RestClientException { 249 | try { 250 | sendLockRequest(true); 251 | forceFetch(); 252 | return getJamaInstance().getCurrentUser().equals(lockStatus.getLockedBy()); 253 | } catch (RestClientException e) { 254 | return false; 255 | } 256 | } 257 | 258 | public void lock() throws RestClientException { 259 | sendLockRequest(true); 260 | forceFetch(); 261 | } 262 | 263 | private void sendLockRequest(boolean lockState) throws RestClientException { 264 | getJamaInstance().putRawData("items/" + getId() + "/lock", "{\"locked\":" + lockState + "}" ); 265 | } 266 | 267 | // returns status of releasing the lock, for non org admin users to verify whether or not their request was successful 268 | public boolean releaseLock() throws RestClientException { 269 | try { 270 | sendLockRequest(false); 271 | forceFetch(); 272 | if(lockStatus.getLockedBy() == null) { 273 | return true; 274 | } else { 275 | return false; 276 | } 277 | } catch (RestClientException e) { 278 | return false; 279 | } 280 | } 281 | 282 | // if user knows they are an org admin user and can override the lock status of an item locked by another user 283 | public void unlock() throws RestClientException { 284 | sendLockRequest(false); 285 | forceFetch(); 286 | } 287 | 288 | @Override 289 | public String toString() { 290 | return getName().toString(); 291 | } 292 | 293 | public byte[] getItemTypeImage() throws JsonException { 294 | return getItemType().getImage(); 295 | } 296 | 297 | 298 | // returns a stagting item after removing the item from the jama instance pool of Ids 299 | public StagingItem edit() throws RestClientException{ 300 | return jamaInstance.editItem(this); 301 | } 302 | 303 | } 304 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/JamaItemType.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.JsonException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 6 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 7 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 8 | import com.jamasoftware.services.restclient.jamadomain.fields.JamaField; 9 | import com.jamasoftware.services.restclient.json.ItemTypeImage; 10 | import com.jamasoftware.services.restclient.util.CompareUtil; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | public class JamaItemType extends LazyResource { 16 | protected String typeKey; 17 | protected String display; 18 | protected String displayPlural; 19 | protected List fields = new ArrayList<>(); 20 | // protected byte[] image; 21 | protected ItemTypeImage image; 22 | 23 | public void checkType(JamaDomainObject jamaDomainObject){ 24 | checkType(JamaItemType.class, jamaDomainObject); 25 | } 26 | @Override 27 | protected String getResourceUrl() { 28 | return "itemtypes/" + getId(); 29 | } 30 | 31 | @Override 32 | public void copyContentFrom(JamaDomainObject jamaDomainObject) { 33 | checkType(jamaDomainObject); 34 | 35 | JamaItemType itemType = (JamaItemType) jamaDomainObject; 36 | typeKey = itemType.typeKey; 37 | display = itemType.display; 38 | displayPlural = itemType.displayPlural; 39 | fields = itemType.fields; 40 | image = itemType.image; 41 | } 42 | 43 | @Override 44 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 45 | checkType(jamaDomainObject); 46 | ((JamaItemType)jamaDomainObject).copyContentFrom(this); 47 | } 48 | 49 | @Override 50 | public void associate(int id, JamaInstance jamaInstance) throws RestClientException{ 51 | super.associate(id, jamaInstance); 52 | for(JamaField field : fields) { 53 | field.setJamaInstance(jamaInstance); 54 | } 55 | } 56 | 57 | public String getTypeKey() { 58 | fetch(); 59 | return typeKey; 60 | } 61 | 62 | public String getDisplay() { 63 | fetch(); 64 | return display; 65 | } 66 | 67 | 68 | public String getDisplayPlural() { 69 | fetch(); 70 | return displayPlural; 71 | } 72 | 73 | 74 | public byte[] getImage() throws JsonException { 75 | fetch(); 76 | return image.getImage(); 77 | } 78 | 79 | public List getFields() { 80 | fetch(); 81 | return fields; 82 | } 83 | 84 | public JamaField getField(String name) throws RestClientException { 85 | fetch(); 86 | for(JamaField field : fields) { 87 | if(CompareUtil.closeEnough(field.getName(), name)) { 88 | return field; 89 | } 90 | } 91 | throw new RestClientException("Unable to locate field with name \"" + name + "\" in Item Type " + this); 92 | } 93 | 94 | @Override 95 | public String toString() { 96 | //TODO fix this or replace it 97 | return "debug item type"; 98 | // return getDisplay(); 99 | } 100 | 101 | @Override 102 | protected String getEditUrl() throws RestClientException { 103 | throw new RestClientException("An attempt was made to edit an Item Type which is not editable. "); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/JamaProject.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.JamaParent; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 6 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 7 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | import java.util.Date; 12 | import java.util.List; 13 | 14 | public class JamaProject extends LazyResource implements JamaParent { 15 | protected String projectKey; 16 | protected String name; 17 | protected String description; 18 | protected boolean isFolder; 19 | protected Date createdDate; 20 | protected Date modifiedDate; 21 | protected JamaUser createdBy; 22 | protected JamaUser modifiedBy; 23 | private ChildrenList children; 24 | 25 | @Override 26 | public List getChildren() throws RestClientException { 27 | if(children == null) { 28 | children = new ChildrenList(this); 29 | } 30 | return Collections.unmodifiableList(children.getChildren()); 31 | } 32 | 33 | // @Override 34 | // public void addChild(JamaItem jamaItem) { 35 | // throw new NotImplementedException(); 36 | // 37 | // } 38 | 39 | @Override 40 | public boolean isProject() { 41 | return true; 42 | } 43 | 44 | @Override 45 | public void makeChildOf(JamaParent jamaParent) throws RestClientException { 46 | throw new RestClientException("You cannot set the parent of a project."); 47 | } 48 | 49 | @Override 50 | protected String getResourceUrl() { 51 | return "projects/" + getId(); 52 | } 53 | 54 | @Override 55 | public void copyContentFrom(JamaDomainObject jamaDomainObject) { 56 | checkType(JamaProject.class, jamaDomainObject); 57 | 58 | JamaProject project = (JamaProject) jamaDomainObject; 59 | projectKey = project.projectKey; 60 | name = project.name; 61 | description = project.description; 62 | isFolder = project.isFolder; 63 | createdDate = project.createdDate; 64 | modifiedDate = project.modifiedDate; 65 | createdBy = project.createdBy; 66 | modifiedBy = project.modifiedBy; 67 | } 68 | 69 | @Override 70 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 71 | checkType(JamaProject.class, jamaDomainObject); 72 | ((JamaProject)jamaDomainObject).copyContentFrom(this); 73 | } 74 | 75 | public String getProjectKey() { 76 | fetch(); 77 | return projectKey; 78 | } 79 | 80 | public String getName() { 81 | fetch(); 82 | return name; 83 | } 84 | 85 | public String getDescription() { 86 | fetch(); 87 | return description; 88 | } 89 | 90 | public boolean isFolder() { 91 | fetch(); 92 | return isFolder; 93 | } 94 | 95 | public JamaUser getCreatedBy() { 96 | fetch(); 97 | return createdBy; 98 | } 99 | 100 | public Date getCreatedDate() { 101 | fetch(); 102 | return createdDate; 103 | } 104 | 105 | public Date getModifiedDate() { 106 | fetch(); 107 | return modifiedDate; 108 | } 109 | 110 | public JamaUser getModifiedBy() { 111 | fetch(); 112 | return modifiedBy; 113 | } 114 | 115 | public List getItems() throws RestClientException { 116 | List objects = getJamaInstance().getAll("items?project=" + getId()); 117 | List items = new ArrayList<>(); 118 | for(JamaDomainObject object : objects) { 119 | items.add((JamaItem)object); 120 | } 121 | return items; 122 | } 123 | 124 | @Override 125 | public String toString() { 126 | return getName(); 127 | } 128 | 129 | public List getRelationships() throws RestClientException { 130 | List objects = getJamaInstance().getAll("relationships?project=" + getId()); 131 | List relationships = new ArrayList<>(); 132 | for(JamaDomainObject o : objects) { 133 | relationships.add((JamaRelationship)o); 134 | } 135 | return relationships; 136 | } 137 | 138 | } 139 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/JamaRelationship.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 6 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingRelationship; 7 | 8 | public class JamaRelationship extends LazyResource { 9 | protected JamaItem toItem; 10 | protected JamaItem fromItem; 11 | protected JamaRelationshipType relationshipType; 12 | protected boolean suspect; 13 | 14 | @Override 15 | protected String getResourceUrl() { 16 | return "relationships/" + getId(); 17 | } 18 | 19 | @Override 20 | protected void copyContentFrom(JamaDomainObject jamaDomainObject) { 21 | checkType(JamaRelationship.class, jamaDomainObject); 22 | 23 | JamaRelationship jamaRelationship = (JamaRelationship)jamaDomainObject; 24 | toItem = jamaRelationship.toItem; 25 | fromItem = jamaRelationship.fromItem; 26 | relationshipType = jamaRelationship.relationshipType; 27 | suspect = jamaRelationship.suspect; 28 | } 29 | 30 | public JamaRelationship(JamaRelationship relationship) { 31 | copyContentFrom(relationship); 32 | } 33 | 34 | public JamaRelationship(){} 35 | 36 | @Override 37 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 38 | checkType(JamaRelationship.class, jamaDomainObject); 39 | ((JamaRelationship)jamaDomainObject).copyContentFrom(this); 40 | } 41 | 42 | public JamaItem getToItem() { 43 | fetch(); 44 | return toItem; 45 | } 46 | 47 | public JamaItem getFromItem() { 48 | fetch(); 49 | return fromItem; 50 | } 51 | 52 | public JamaRelationshipType getRelationshipType() { 53 | fetch(); 54 | return relationshipType; 55 | } 56 | 57 | public boolean isSuspect() { 58 | fetch(); 59 | return suspect; 60 | } 61 | 62 | public StagingRelationship edit() throws RestClientException { 63 | return jamaInstance.editRelationship(this); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/JamaRelationshipType.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 6 | 7 | public class JamaRelationshipType extends LazyResource { 8 | protected String name; 9 | protected boolean isDefault; 10 | 11 | @Override 12 | protected String getResourceUrl() { 13 | return "relationshiptypes/" + getId(); 14 | } 15 | 16 | @Override 17 | public void copyContentFrom(JamaDomainObject jamaDomainObject) { 18 | checkType(this.getClass(), jamaDomainObject); 19 | 20 | JamaRelationshipType relationshipType = (JamaRelationshipType)jamaDomainObject; 21 | this.name = relationshipType.name; 22 | this.isDefault = relationshipType.isDefault; 23 | } 24 | 25 | @Override 26 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 27 | checkType(JamaRelationshipType.class, jamaDomainObject); 28 | ((JamaRelationshipType)jamaDomainObject).copyContentFrom(this); 29 | } 30 | 31 | 32 | public String getName() { 33 | fetch(); 34 | return name; 35 | } 36 | 37 | public boolean isDefault() { 38 | fetch(); 39 | return isDefault; 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return getName(); 45 | } 46 | 47 | @Override 48 | protected String getEditUrl() throws RestClientException { 49 | throw new RestClientException("An attempt was made to edit a Relationship Type which is not editable. "); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/JamaUser.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 5 | 6 | public class JamaUser extends LazyResource { 7 | protected String username; 8 | protected String firstName; 9 | protected String lastName; 10 | protected String email; 11 | protected String phone; 12 | protected String title; 13 | protected String location; 14 | protected String licenseType; 15 | protected boolean active; 16 | 17 | @Override 18 | protected String getResourceUrl() { 19 | return "users/" + getId(); 20 | } 21 | 22 | @Override 23 | protected void copyContentFrom(JamaDomainObject jamaDomainObject) { 24 | checkType(JamaUser.class, jamaDomainObject); 25 | 26 | JamaUser user = (JamaUser) jamaDomainObject; 27 | username = user.username; 28 | firstName = user.firstName; 29 | lastName = user.lastName; 30 | email = user.email; 31 | phone = user.phone; 32 | title = user.title; 33 | location = user.location; 34 | licenseType = user.licenseType; 35 | active = user.active; 36 | } 37 | 38 | @Override 39 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 40 | checkType(JamaUser.class, jamaDomainObject); 41 | ((JamaUser)jamaDomainObject).copyContentFrom(this); 42 | } 43 | 44 | public String getUsername() { 45 | fetch(); 46 | return username; 47 | } 48 | 49 | public String getFirstName() { 50 | fetch(); 51 | return firstName; 52 | } 53 | 54 | public String getLastName() { 55 | fetch(); 56 | return lastName; 57 | } 58 | 59 | public String getEmail() { 60 | fetch(); 61 | return email; 62 | } 63 | 64 | public String getPhone() { 65 | fetch(); 66 | return phone; 67 | } 68 | 69 | public String getTitle() { 70 | fetch(); 71 | return title; 72 | } 73 | 74 | public String getLocation() { 75 | fetch(); 76 | return location; 77 | } 78 | 79 | public String getLicenseType() { 80 | fetch(); 81 | return licenseType; 82 | } 83 | 84 | public boolean isActive() { 85 | fetch(); 86 | return active; 87 | } 88 | 89 | @Override 90 | public String toString() { 91 | return getUsername(); 92 | } 93 | 94 | 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/LazyBase.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.exception.UnexpectedJamaResponseException; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 6 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 7 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingResource; 8 | 9 | public abstract class LazyBase { 10 | protected JamaInstance jamaInstance; 11 | protected boolean shouldFetch = true; 12 | protected Long lastFetch = null; 13 | private Integer id; 14 | 15 | public void fetch() { 16 | if(this instanceof StagingResource) return; 17 | checkFetched(); 18 | try { 19 | if (shouldFetch && id != null) { 20 | forceFetch(); 21 | } 22 | } catch (RestClientException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | 27 | public void checkType(Class clazz, JamaDomainObject jamaDomainObject) { 28 | if(this == jamaDomainObject) return; 29 | if(!clazz.isInstance(jamaDomainObject)) { 30 | throw new UnexpectedJamaResponseException("Expecting a " + clazz.getName() + " from the Jama server. Instead, got: " + jamaDomainObject.getClass().getName()); 31 | } 32 | } 33 | 34 | protected void markFetch() { 35 | shouldFetch = false; 36 | lastFetch = System.currentTimeMillis(); 37 | } 38 | 39 | public JamaInstance getJamaInstance() { 40 | return jamaInstance; 41 | } 42 | 43 | public Integer getId() { 44 | return id; 45 | } 46 | 47 | public void associate(int id, JamaInstance jamaInstance) throws RestClientException { 48 | this.id = id; 49 | this.jamaInstance = jamaInstance; 50 | } 51 | 52 | public void associate(JamaInstance jamaInstance) throws RestClientException { 53 | this.id = null; 54 | this.jamaInstance = jamaInstance; 55 | } 56 | 57 | protected void checkFetched() { 58 | if(jamaInstance == null || lastFetch == null) { 59 | return; 60 | } 61 | if(!shouldFetch && System.currentTimeMillis() - lastFetch > jamaInstance.getResourceTimeOut() * 1000) { 62 | shouldFetch = true; 63 | } 64 | } 65 | 66 | public abstract void forceFetch() throws RestClientException; 67 | 68 | protected void invalidate() { 69 | shouldFetch = true; 70 | lastFetch = null; 71 | } 72 | 73 | protected void invalidate(LazyBase lazyBase) { 74 | lazyBase.invalidate(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/LazyCollection.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | 6 | import java.util.List; 7 | 8 | public abstract class LazyCollection extends LazyBase { 9 | 10 | @Override 11 | public void forceFetch() throws RestClientException { 12 | markFetch(); 13 | copyContentFrom(jamaInstance.getResourceCollection(getCollectionUrl())); 14 | } 15 | 16 | public abstract String getCollectionUrl(); 17 | public abstract void copyContentFrom(List objectList); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/OptionList.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Collections; 8 | import java.util.List; 9 | 10 | public class OptionList extends LazyCollection { 11 | private final PickList pickList; 12 | private List options = new ArrayList<>(); 13 | 14 | public OptionList(PickList pickList) throws RestClientException { 15 | this.pickList = pickList; 16 | associate(pickList.getId(), pickList.getJamaInstance()); 17 | } 18 | 19 | @Override 20 | public String getCollectionUrl() { 21 | return "picklists/" + getId() + "/options"; 22 | } 23 | 24 | @Override 25 | public void copyContentFrom(List objectList) { 26 | List options = new ArrayList<>(); 27 | for(JamaDomainObject o : objectList) { 28 | options.add((PickListOption)o); 29 | } 30 | this.options = options; 31 | } 32 | 33 | public PickList getPickList() { 34 | fetch(); 35 | return pickList; 36 | } 37 | 38 | public List getOptions() { 39 | fetch(); 40 | return Collections.unmodifiableList(options); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/PickList.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 6 | 7 | import java.util.List; 8 | 9 | public class PickList extends LazyResource { 10 | protected OptionList options; 11 | protected String name; 12 | protected String description; 13 | 14 | @Override 15 | protected String getResourceUrl() { 16 | return "picklists/" + getId(); 17 | } 18 | 19 | @Override 20 | protected void copyContentFrom(JamaDomainObject jamaDomainObject) { 21 | checkType(PickList.class, jamaDomainObject); 22 | PickList pickList = (PickList)jamaDomainObject; 23 | name = pickList.name; 24 | description = pickList.description; 25 | options = pickList.options; 26 | } 27 | 28 | @Override 29 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 30 | checkType(PickList.class, jamaDomainObject); 31 | ((PickList)jamaDomainObject).copyContentFrom(this); 32 | } 33 | 34 | public List getOptions() throws RestClientException { 35 | if(options == null) { 36 | options = new OptionList(this); 37 | } 38 | return options.getOptions(); 39 | } 40 | 41 | public String getName() { 42 | fetch(); 43 | return name; 44 | } 45 | 46 | public String getDescription() { 47 | fetch(); 48 | return description; 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return getName(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/PickListOption.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 6 | 7 | public class PickListOption extends LazyResource { 8 | protected String name; 9 | protected String description; 10 | protected boolean active; 11 | protected String color; 12 | protected boolean defaultValue; 13 | 14 | @Override 15 | protected String getResourceUrl() { 16 | return "picklistoptions/" + getId(); 17 | } 18 | 19 | @Override 20 | public void copyContentFrom(JamaDomainObject jamaDomainObject) { 21 | checkType(getClass(), jamaDomainObject); 22 | 23 | PickListOption option = (PickListOption)jamaDomainObject; 24 | name = option.name; 25 | description = option.description; 26 | active = option.active; 27 | color = option.color; 28 | defaultValue = option.defaultValue; 29 | } 30 | 31 | @Override 32 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 33 | checkType(PickListOption.class, jamaDomainObject); 34 | ((PickListOption)jamaDomainObject).copyContentFrom(this); 35 | } 36 | 37 | public String getName() { 38 | fetch(); 39 | return name; 40 | } 41 | 42 | public boolean isActive() { 43 | fetch(); 44 | return active; 45 | } 46 | 47 | public String getDescription() { 48 | fetch(); 49 | return description; 50 | } 51 | 52 | 53 | public String getColor() { 54 | fetch(); 55 | return color; 56 | } 57 | 58 | public boolean isDefaultValue() { 59 | fetch(); 60 | return defaultValue; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return getName(); 66 | } 67 | 68 | @Override 69 | protected String getEditUrl() throws RestClientException { 70 | throw new RestClientException("An attempt was made to edit a Pick List Option which is not editable. "); 71 | } 72 | 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/RelationshipTypeList.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | public class RelationshipTypeList extends LazyCollection { 12 | private List relationshipTypeList; 13 | 14 | public RelationshipTypeList(JamaInstance jamaInstance) throws RestClientException { 15 | // this has an id of -1 because it doesn't have an Id associated with it in Jama 16 | associate(-1, jamaInstance); 17 | } 18 | 19 | @Override 20 | public String getCollectionUrl() { 21 | return "relationshiptypes"; 22 | } 23 | 24 | @Override 25 | public void copyContentFrom(List objectList) { 26 | List relationshipTypeList = new ArrayList<>(); 27 | for(JamaDomainObject relationship : objectList) { 28 | relationshipTypeList.add((JamaRelationshipType) relationship); 29 | } 30 | this.relationshipTypeList = relationshipTypeList; 31 | } 32 | 33 | public List getRelationshipTypes() { 34 | fetch(); 35 | return Collections.unmodifiableList(relationshipTypeList); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/lazyresources/Release.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 5 | import com.jamasoftware.services.restclient.util.DateUtil; 6 | 7 | import java.util.Date; 8 | 9 | public class Release extends LazyResource { 10 | protected String name; 11 | protected String description; 12 | protected JamaProject project; 13 | protected Date releaseDate; 14 | protected boolean active; 15 | protected boolean archived; 16 | protected Integer itemCount; 17 | 18 | @Override 19 | protected String getResourceUrl() { 20 | return "releases/" + getId(); 21 | } 22 | 23 | @Override 24 | public void copyContentFrom(JamaDomainObject jamaDomainObject) { 25 | checkType(getClass(), jamaDomainObject); 26 | 27 | Release release = (Release)jamaDomainObject; 28 | name = release.name; 29 | description = release.description; 30 | project = release.project; 31 | releaseDate = release.releaseDate; 32 | active = release.active; 33 | archived = release.archived; 34 | itemCount = release.itemCount; 35 | } 36 | 37 | @Override 38 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 39 | checkType(Release.class, jamaDomainObject); 40 | ((Release)jamaDomainObject).copyContentFrom(this); 41 | } 42 | 43 | public String getName() { 44 | fetch(); 45 | return name; 46 | } 47 | 48 | public String getDescription() { 49 | fetch(); 50 | return description; 51 | } 52 | 53 | 54 | public JamaProject getProject() { 55 | fetch(); 56 | return project; 57 | } 58 | 59 | public Date getReleaseDate() { 60 | fetch(); 61 | return releaseDate; 62 | } 63 | 64 | 65 | public boolean isActive() { 66 | fetch(); 67 | return active; 68 | } 69 | 70 | public boolean isArchived() { 71 | fetch(); 72 | return archived; 73 | } 74 | 75 | public Integer getItemCount() { 76 | fetch(); 77 | return itemCount; 78 | } 79 | 80 | @Override 81 | public String toString() { 82 | String s = getName(); 83 | if(getReleaseDate() != null) { 84 | s += " [" + DateUtil.monthDayYear(getReleaseDate()) + "]"; 85 | } 86 | return s; 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingFactory.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.JamaParent; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 6 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItem; 7 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItemType; 8 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaProject; 9 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaRelationship; 10 | 11 | public class StagingFactory { 12 | 13 | protected StagingItem createStagingItem() { return new StagingItem(); } 14 | 15 | protected StagingItem createStagingItem(JamaItem jamaItem) throws RestClientException { 16 | return new StagingItem(jamaItem); 17 | } 18 | 19 | protected StagingItem createStagingItem(JamaInstance jamaInstance, String name, JamaParent parent, JamaItemType itemType) throws RestClientException { 20 | return new StagingItem(jamaInstance, name, parent, itemType); 21 | } 22 | 23 | protected StagingRelationship createStagingRelationship() { return new StagingRelationship(); } 24 | 25 | protected StagingRelationship createStagingRelationship(JamaRelationship jamaRelationship) throws RestClientException { 26 | return new StagingRelationship(jamaRelationship); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingItem.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.JamaParent; 4 | import com.jamasoftware.services.restclient.exception.JamaFieldNotFound; 5 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 6 | import com.jamasoftware.services.restclient.exception.RestClientException; 7 | import com.jamasoftware.services.restclient.jamadomain.JamaLocation; 8 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 9 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 10 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.*; 11 | import com.jamasoftware.services.restclient.jamadomain.values.JamaFieldValue; 12 | import com.jamasoftware.services.restclient.jamadomain.values.TextFieldValue; 13 | 14 | import java.io.IOException; 15 | import java.io.OutputStream; 16 | 17 | public class StagingItem extends JamaItem implements StagingResource { 18 | final private JamaItem originatingItem; 19 | private boolean invalid = false; 20 | 21 | private void testValidity() throws RestClientException { 22 | if(invalid) { 23 | throw new RestClientException("Staging item may not be accessed after commit is called."); 24 | } 25 | } 26 | 27 | protected StagingItem(){ 28 | originatingItem = null; 29 | } 30 | 31 | protected StagingItem(JamaItem item) throws RestClientException{ 32 | super(item); 33 | if(item.getId() != null) 34 | super.associate(item.getId(), item.getJamaInstance()); 35 | else 36 | super.associate(item.getJamaInstance()); 37 | originatingItem = item; 38 | } 39 | 40 | protected StagingItem(JamaInstance jamaInstance, String name, JamaParent parent, JamaItemType itemType) throws RestClientException{ 41 | setItemType(itemType); 42 | setName(name); 43 | this.location = new JamaLocation(); 44 | this.location.setParent(parent); 45 | this.jamaInstance = jamaInstance; 46 | originatingItem = null; 47 | } 48 | 49 | @Override 50 | protected void copyContentFrom(JamaDomainObject jamaDomainObject) { 51 | super.copyContentFrom(jamaDomainObject); 52 | } 53 | 54 | public StagingItem setName(String name) throws RestClientException { 55 | testValidity(); 56 | if(this.name == null) { 57 | this.name = (TextFieldValue) itemType.getField("name").getValue(); 58 | } 59 | this.name.setValue(name); 60 | return this; 61 | } 62 | 63 | 64 | public StagingItem setGlobalId(String globalId) throws RestClientException { 65 | testValidity(); 66 | //TODO need to change the query parameters 67 | this.globalId = globalId; 68 | return this; 69 | } 70 | 71 | public StagingItem setItemType(JamaItemType itemType) throws RestClientException { 72 | testValidity(); 73 | this.itemType = itemType; 74 | return this; 75 | } 76 | 77 | public StagingItem setChildItemType(JamaItemType childItemType) throws RestClientException{ 78 | testValidity(); 79 | this.childItemType = childItemType; 80 | return this; 81 | } 82 | 83 | public StagingItem setModifiedBy(JamaUser modifiedBy) throws RestClientException { 84 | testValidity(); 85 | this.modifiedBy = modifiedBy; 86 | return this; 87 | } 88 | 89 | public StagingItem addFieldValue(JamaFieldValue fieldValue) throws RestClientException { 90 | testValidity(); 91 | fieldValues.add(fieldValue); 92 | return this; 93 | } 94 | 95 | public StagingItem setFieldValue(String fieldName, Object value) throws RestClientException { 96 | testValidity(); 97 | if(fieldName.equals("name")) { 98 | setName(value.toString()); 99 | return this; 100 | } 101 | JamaFieldValue replacementValue = itemType.getField(fieldName).getValue(); 102 | replacementValue.setValue(value); 103 | replaceFieldValue(replacementValue); 104 | return this; 105 | } 106 | 107 | public StagingItem setFieldValueQuietly(String fieldName, Object value, OutputStream out) throws IOException, RestClientException{ 108 | testValidity(); 109 | try{ 110 | setFieldValue(fieldName, value); 111 | } catch(JamaTypeMismatchException | JamaFieldNotFound e) { 112 | //Deliberately ignoring exception 113 | if(out != null) { 114 | out.write(e.getMessage().getBytes()); 115 | } 116 | } 117 | return this; 118 | } 119 | 120 | public StagingItem setFieldValueQuietly(String fieldName, Object value) throws RestClientException { 121 | testValidity(); 122 | try{ 123 | setFieldValueQuietly(fieldName, value, null); 124 | } catch(IOException e) { 125 | // No output stream passed in. No exception will be thrown 126 | } 127 | return this; 128 | } 129 | 130 | private void replaceFieldValue(JamaFieldValue value) throws JamaFieldNotFound { 131 | for(int i = 0; i < fieldValues.size(); ++i) { 132 | if(fieldValues.get(i).getName().equals(value.getName())) { 133 | fieldValues.set(i, value); 134 | return; 135 | } 136 | } 137 | fieldValues.add(value); 138 | } 139 | 140 | public JamaItem commit() throws RestClientException { 141 | testValidity(); 142 | invalid = true; // never to be unset... 143 | if (getId() == null) { 144 | return postCommit(); 145 | } else { 146 | return putCommit(); 147 | } 148 | } 149 | 150 | public void delete() throws RestClientException { 151 | testValidity(); 152 | invalid = true; // never to be unset... 153 | if (getId() != null) { 154 | deleteResource(); 155 | } 156 | } 157 | 158 | private JamaItem postCommit() throws RestClientException { 159 | return (JamaItem)post(JamaItem.class); 160 | } 161 | 162 | private JamaItem putCommit() throws RestClientException { 163 | put(); 164 | invalidate(originatingItem); 165 | return originatingItem; 166 | } 167 | 168 | @Override 169 | protected String getEditUrl() throws RestClientException { 170 | return "items/" + getId(); 171 | } 172 | 173 | @Override 174 | protected String getDeleteUrl() throws RestClientException { 175 | return "items/" + getId(); 176 | } 177 | 178 | public StagingItem setParent(JamaParent item) throws RestClientException{ 179 | testValidity(); 180 | this.makeChildOf(item); 181 | return this; 182 | } 183 | 184 | // public StagingItem addChildToItem(JamaItem jamaItem) throws RestClientException { 185 | // testValidity(); 186 | // this.addChild(jamaItem); 187 | // return this; 188 | // } 189 | 190 | @Override 191 | public void associate(int id, JamaInstance jamaInstance) throws RestClientException { 192 | testValidity(); 193 | throw new RestClientException("You cannot associate an item while it is in edit mode."); 194 | } 195 | 196 | @Override 197 | protected String getCreateUrl() throws RestClientException { 198 | return "items"; 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingJamaItemType.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItemType; 4 | 5 | public class StagingJamaItemType extends JamaItemType implements StagingResource {} 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingPickList.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.PickList; 4 | 5 | public class StagingPickList extends PickList implements StagingResource {} 6 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingPickListOption.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.PickListOption; 4 | 5 | public class StagingPickListOption extends PickListOption implements StagingResource {} 6 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingProject.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaProject; 5 | 6 | public class StagingProject extends JamaProject implements StagingResource { 7 | public StagingProject setProjectKey(String projectKey) { 8 | this.projectKey = projectKey; 9 | return this; 10 | } 11 | 12 | public StagingProject setName(String name) { 13 | this.name = name; 14 | return this; 15 | } 16 | 17 | public StagingProject setDescription(String description) { 18 | this.description = description; 19 | return this; 20 | } 21 | 22 | public StagingProject setFolder(boolean folder) { 23 | this.isFolder = folder; 24 | return this; 25 | } 26 | 27 | @Override 28 | protected String getEditUrl() throws RestClientException { 29 | return "projects/" + getId(); 30 | } 31 | 32 | @Override 33 | protected String getCreateUrl() throws RestClientException { 34 | return "items/"; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingRelationship.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 6 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItem; 7 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaRelationship; 8 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaRelationshipType; 9 | 10 | public class StagingRelationship extends JamaRelationship implements StagingResource { 11 | final private JamaRelationship originatingRelationship; 12 | private boolean invalid = false; 13 | 14 | 15 | @Override 16 | public JamaItem getToItem() { 17 | return toItem; 18 | } 19 | 20 | @Override 21 | public JamaItem getFromItem() { 22 | return fromItem; 23 | } 24 | 25 | @Override 26 | protected String getEditUrl() throws RestClientException { 27 | return "relationships/" + getId(); 28 | } 29 | 30 | @Override 31 | protected String getCreateUrl() throws RestClientException { 32 | return "relationships/"; 33 | } 34 | 35 | @Override 36 | protected String getDeleteUrl() throws RestClientException { 37 | return "relationships/" + getId(); 38 | } 39 | 40 | private void testValidity() throws RestClientException { 41 | if(invalid) { 42 | throw new RestClientException("Staging relationship may not be accessed after commit is called."); 43 | } 44 | } 45 | 46 | protected StagingRelationship(){ 47 | originatingRelationship = null; 48 | } 49 | 50 | protected StagingRelationship(JamaRelationship relationship) throws RestClientException { 51 | super(relationship); 52 | if(relationship.getId() != null) 53 | super.associate(relationship.getId(), relationship.getJamaInstance()); 54 | else 55 | super.associate(relationship.getJamaInstance()); 56 | originatingRelationship = relationship; 57 | } 58 | 59 | @Override 60 | protected void copyContentFrom(JamaDomainObject jamaDomainObject) { 61 | super.copyContentFrom(jamaDomainObject); 62 | } 63 | 64 | 65 | public StagingRelationship setRelationshipType(JamaRelationshipType relationshipType) throws RestClientException { 66 | testValidity(); 67 | this.relationshipType = relationshipType; 68 | return this; 69 | } 70 | 71 | public StagingRelationship setFromItem(JamaItem item) throws RestClientException { 72 | testValidity(); 73 | this.fromItem = item; 74 | return this; 75 | } 76 | 77 | public StagingRelationship setToItem(JamaItem item) throws RestClientException { 78 | testValidity(); 79 | this.toItem = item; 80 | return this; 81 | } 82 | 83 | public JamaRelationship commit() throws RestClientException { 84 | testValidity(); 85 | invalid = true; // never to be unset... 86 | if (getId() == null) { 87 | return postCommit(); 88 | } else { 89 | return putCommit(); 90 | } 91 | } 92 | 93 | private JamaRelationship postCommit() throws RestClientException { 94 | return (JamaRelationship)post(JamaRelationship.class); 95 | } 96 | 97 | private JamaRelationship putCommit() throws RestClientException { 98 | put(); 99 | invalidate(originatingRelationship); 100 | return originatingRelationship; 101 | } 102 | 103 | public void delete() throws RestClientException { 104 | testValidity(); 105 | invalid = true; // never to be unset... 106 | if (getId() != null) { 107 | deleteResource(); 108 | } 109 | } 110 | 111 | @Override 112 | public void associate(int id, JamaInstance jamaInstance) throws RestClientException { 113 | testValidity(); 114 | throw new RestClientException("You cannot associate a relationship while it is in edit mode."); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingRelationshipType.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaRelationshipType; 4 | 5 | public class StagingRelationshipType extends JamaRelationshipType implements StagingResource {} 6 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingRelease.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaProject; 5 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.Release; 6 | 7 | import java.util.Date; 8 | 9 | public class StagingRelease extends Release implements StagingResource { 10 | 11 | public StagingRelease setName(String name) { 12 | this.name = name; 13 | return this; 14 | } 15 | 16 | public StagingRelease setDescription(String description) { 17 | this.description = description; 18 | return this; 19 | } 20 | 21 | public StagingRelease setProject(JamaProject project) { 22 | this.project = project; 23 | return this; 24 | } 25 | 26 | public StagingRelease setReleaseDate(Date releaseDate) { 27 | this.releaseDate = releaseDate; 28 | return this; 29 | } 30 | 31 | public StagingRelease setActive(boolean active) { 32 | this.active = active; 33 | return this; 34 | } 35 | 36 | public StagingRelease setArchived(boolean archived) { 37 | this.archived = archived; 38 | return this; 39 | } 40 | 41 | @Override 42 | protected String getEditUrl() throws RestClientException { 43 | return "releases/" + getId(); 44 | } 45 | 46 | @Override 47 | protected String getCreateUrl() throws RestClientException { 48 | return "releases/"; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingResource.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | public interface StagingResource { 4 | 5 | } 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/stagingresources/StagingUser.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.stagingresources; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaUser; 5 | 6 | public class StagingUser extends JamaUser implements StagingResource { 7 | 8 | public StagingUser setUsername(String username) { 9 | this.username = username; 10 | return this; 11 | } 12 | 13 | public StagingUser setFirstName(String firstName) { 14 | this.firstName = firstName; 15 | return this; 16 | } 17 | 18 | public StagingUser setLastName(String lastName) { 19 | this.lastName = lastName; 20 | return this; 21 | } 22 | 23 | public StagingUser setEmail(String email) { 24 | this.email = email; 25 | return this; 26 | } 27 | 28 | public StagingUser setPhone(String phone) { 29 | this.phone = phone; 30 | return this; 31 | } 32 | 33 | public StagingUser setTitle(String title) { 34 | this.title = title; 35 | return this; 36 | } 37 | 38 | public StagingUser setLocation(String location) { 39 | this.location = location; 40 | return this; 41 | } 42 | 43 | public StagingUser setLicenseType(String licenseType) { 44 | this.licenseType = licenseType; 45 | return this; 46 | } 47 | 48 | @Override 49 | protected String getEditUrl() throws RestClientException { 50 | return "users/" + getId(); 51 | } 52 | 53 | @Override 54 | protected String getCreateUrl() throws RestClientException { 55 | return "users/"; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/CalculatedFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | 6 | public class CalculatedFieldValue extends JamaFieldValue { 7 | private String value; 8 | @Override 9 | public Object getValue() { 10 | return value; 11 | } 12 | 13 | @Override 14 | public void setValue(String value) throws RestClientException { 15 | this.value = value; 16 | } 17 | public void setValue(Object value) throws JamaTypeMismatchException { 18 | throw new JamaTypeMismatchException("Calculated fields are not editable for field " + getName()); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/DateFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.util.DateUtil; 6 | 7 | import java.text.ParseException; 8 | import java.util.Date; 9 | 10 | public class DateFieldValue extends JamaFieldValue { 11 | private Date value; 12 | 13 | @Override 14 | public Date getValue() { 15 | return value; 16 | } 17 | 18 | @Override 19 | public void setValue(String value) throws RestClientException { 20 | try { 21 | this.value = DateUtil.parseDate(value); 22 | } catch (ParseException e) { 23 | throw new RestClientException("Unable to parse date: " + value); 24 | } 25 | } 26 | 27 | public void setValue(Object value) throws JamaTypeMismatchException { 28 | checkType(Date.class, value); 29 | this.value = (Date) value; 30 | } 31 | public void setValue(Date value) { 32 | this.value = value; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/FlagFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | 6 | public class FlagFieldValue extends JamaFieldValue { 7 | private Boolean value; 8 | 9 | public Boolean getValue() { 10 | return value; 11 | } 12 | 13 | @Override 14 | public void setValue(String value) throws RestClientException { 15 | if(value == null) { 16 | this.value = null; 17 | return; 18 | } 19 | this.value = Boolean.valueOf(value); 20 | } 21 | 22 | public void setValue(Object value) throws JamaTypeMismatchException { 23 | checkType(Boolean.class, value); 24 | this.value = (Boolean) value; 25 | } 26 | public void setValue(Boolean value) { 27 | this.value = value; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/IntegerFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | 6 | public class IntegerFieldValue extends JamaFieldValue { 7 | private Integer value; 8 | 9 | public Integer getValue() { 10 | return value; 11 | } 12 | 13 | @Override 14 | public void setValue(String value) throws RestClientException { 15 | if(value == null) { 16 | this.value = null; 17 | return; 18 | } 19 | this.value = Integer.valueOf(value); 20 | } 21 | 22 | public void setValue(Object value) throws JamaTypeMismatchException { 23 | checkType(Integer.class, value); 24 | this.value = (Integer)value; 25 | } 26 | public void setValue(Integer value) { 27 | this.value = value; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/JamaFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 6 | import com.jamasoftware.services.restclient.exception.RestClientException; 7 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 8 | import com.jamasoftware.services.restclient.jamadomain.fields.JamaField; 9 | 10 | public abstract class JamaFieldValue { 11 | private JamaInstance jamaInstance; 12 | private String name; 13 | private String label; 14 | protected JamaField field; 15 | 16 | public abstract Object getValue(); 17 | 18 | public abstract void setValue(String value) throws RestClientException; 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public String getLabel() { 29 | return label; 30 | } 31 | 32 | public void setLabel(String label) { 33 | this.label = label; 34 | } 35 | 36 | public JamaInstance getJamaInstance() { 37 | return jamaInstance; 38 | } 39 | 40 | public void setJamaInstance(JamaInstance jamaInstance) { 41 | this.jamaInstance = jamaInstance; 42 | } 43 | 44 | public abstract void setValue(Object value) throws JamaTypeMismatchException, RestClientException; 45 | 46 | @Override 47 | public String toString() { 48 | if(getValue() == null) { 49 | return ""; 50 | } 51 | return getValue().toString(); 52 | } 53 | 54 | protected void checkType(Class clazz, Object value) throws JamaTypeMismatchException { 55 | if(!clazz.isInstance(value)){ 56 | throw new JamaTypeMismatchException("Expected type " + clazz.getName() + ", received " + value.getClass() + " instead. " + 57 | "In field: " + getName()); 58 | } 59 | } 60 | 61 | protected void checkTypes(Class[] classes, Object value) throws JamaTypeMismatchException { 62 | for(Class clazz : classes) { 63 | if(clazz.isInstance(value)) { 64 | return; 65 | } 66 | } 67 | String message = "Expected one type of "; 68 | for(int i = 0; i < classes.length; ++i) { 69 | message += classes[i].getName() + (i == classes.length - 1 ? "" : ", "); 70 | } 71 | throw new JamaTypeMismatchException(message + ". Received " + value.getClass() + " instead. In field: " + getName()); 72 | } 73 | 74 | public void setField(JamaField field) { 75 | this.field = field; 76 | } 77 | 78 | public boolean readOnly() { 79 | return field.isReadOnly(); 80 | } 81 | 82 | protected void setValueFromPoolOrNew(Class clazz, int resourceId) throws RestClientException { 83 | JamaDomainObject jamaDomainObject = getJamaInstance().checkPool(clazz, resourceId); 84 | try { 85 | if(jamaDomainObject == null) { 86 | jamaDomainObject = (JamaDomainObject)clazz.newInstance(); 87 | jamaInstance.addToPool(clazz, resourceId, jamaDomainObject); 88 | if(jamaDomainObject instanceof LazyResource) { 89 | ((LazyResource)jamaDomainObject).associate(resourceId, jamaInstance); 90 | } 91 | } 92 | setValue(jamaDomainObject); 93 | } catch (InstantiationException | IllegalAccessException e) { 94 | throw new RestClientException(e); 95 | } 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/MultiSelectFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.PickListOption; 5 | import com.jamasoftware.services.restclient.exception.RestClientException; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public class MultiSelectFieldValue extends JamaFieldValue { 11 | private List value = new ArrayList<>(); 12 | 13 | @Override 14 | public List getValue() { 15 | return value; 16 | } 17 | 18 | @Override 19 | public void setValue(String value) throws RestClientException { 20 | if(value == null || value.equals("[]")) { 21 | return; 22 | } 23 | value = value.substring(1, value.length() - 1); 24 | 25 | String[] values = value.split(","); 26 | for(String optionIdString : values) { 27 | optionIdString = optionIdString.trim(); 28 | int optionId = Integer.valueOf(optionIdString); 29 | setValueFromPoolOrNew(PickListOption.class, optionId); 30 | } 31 | } 32 | 33 | public void setValue(Object value) throws JamaTypeMismatchException { 34 | Class[] allowedTypes = { 35 | List.class, 36 | PickListOption.class 37 | }; 38 | checkTypes(allowedTypes, value); 39 | if(value instanceof PickListOption) { 40 | this.value.add((PickListOption)value); 41 | return; 42 | } 43 | List list = (List)value; 44 | List options = new ArrayList<>(); 45 | for(Object o : list) { 46 | checkType(PickListOption.class, o); 47 | options.add((PickListOption)o); 48 | } 49 | this.value = options; 50 | } 51 | 52 | public void setValue(List value) { 53 | this.value = value; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/PickListFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.fields.PickListField; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.PickListOption; 5 | import com.jamasoftware.services.restclient.exception.RestClientException; 6 | import com.jamasoftware.services.restclient.util.CompareUtil; 7 | 8 | import java.util.List; 9 | 10 | public class PickListFieldValue extends JamaFieldValue { 11 | private PickListOption value; 12 | 13 | @Override 14 | public PickListOption getValue() { 15 | return value; 16 | } 17 | 18 | @Override 19 | public void setValue(String value) throws RestClientException { 20 | if(value == null) { 21 | this.value = null; 22 | return; 23 | } 24 | int optionId = Integer.valueOf(value); 25 | setValueFromPoolOrNew(PickListOption.class, optionId); 26 | } 27 | 28 | public void setValue(Object value) throws RestClientException { 29 | Class[] allowedTypes = { 30 | PickListOption.class, 31 | String.class 32 | }; 33 | checkTypes(allowedTypes, value); 34 | if(value instanceof String) { 35 | value = getOption((String)value); 36 | } 37 | this.value = (PickListOption) value; 38 | } 39 | 40 | public void setValue(PickListOption value) { 41 | this.value = value; 42 | } 43 | 44 | public List getOptions() throws RestClientException { 45 | return ((PickListField)field).getPickList().getOptions(); 46 | } 47 | 48 | public PickListOption getOption(String optionName) throws RestClientException { 49 | PickListOption found = null; 50 | for(PickListOption option : getOptions()) { 51 | if(CompareUtil.closeEnough(option.getName(), optionName)) { 52 | if(found != null) { 53 | throw new RestClientException("More than one pickListOption closely matches the string: \"" + optionName + "\" in pickList \"" + ((PickListField)field).getPickList() + "\""); 54 | } 55 | found = option; 56 | } 57 | } 58 | if(found == null) { 59 | throw new RestClientException("No pickListOptions matched the string: \"" + optionName + "\" in pickList \"" + ((PickListField)field).getPickList() + "\""); 60 | } 61 | return found; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/ProjectFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaProject; 5 | import com.jamasoftware.services.restclient.exception.RestClientException; 6 | 7 | public class ProjectFieldValue extends JamaFieldValue { 8 | private JamaProject value; 9 | 10 | @Override 11 | public JamaProject getValue() { 12 | return value; 13 | } 14 | 15 | @Override 16 | public void setValue(String value) throws RestClientException { 17 | if(value == null) { 18 | this.value = null; 19 | return; 20 | } 21 | int projectId = Integer.valueOf(value); 22 | setValueFromPoolOrNew(JamaProject.class, projectId); 23 | } 24 | public void setValue(Object value) throws JamaTypeMismatchException { 25 | checkType(JamaProject.class, value); 26 | this.value = (JamaProject)value; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/ReleaseFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.Release; 5 | import com.jamasoftware.services.restclient.exception.RestClientException; 6 | 7 | public class ReleaseFieldValue extends JamaFieldValue { 8 | private Release value; 9 | 10 | @Override 11 | public Release getValue() { 12 | return value; 13 | } 14 | 15 | public void setValue(Release value) { 16 | this.value = value; 17 | } 18 | 19 | @Override 20 | public void setValue(String value) throws RestClientException { 21 | if(value == null) { 22 | this.value = null; 23 | return; 24 | } 25 | int releaseId = Integer.valueOf(value); 26 | setValueFromPoolOrNew(Release.class, releaseId); 27 | } 28 | 29 | public void setValue(Object value) throws JamaTypeMismatchException { 30 | checkType(Release.class, value); 31 | this.value = (Release) value; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/RichTextFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.jamadomain.JamaRichText; 5 | import com.jamasoftware.services.restclient.exception.RestClientException; 6 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaProject; 7 | 8 | public class RichTextFieldValue extends JamaFieldValue { 9 | private JamaRichText value = new JamaRichText(); 10 | 11 | @Override 12 | public JamaRichText getValue() { 13 | return value; 14 | } 15 | 16 | public void setValue(JamaRichText value) { 17 | this.value = value; 18 | } 19 | 20 | public void setValue(Object value) throws JamaTypeMismatchException { 21 | checkType(String.class, value); 22 | this.value.setValue((String)value); 23 | } 24 | 25 | @Override 26 | public void setValue(String value) throws RestClientException { 27 | this.value = new JamaRichText(); 28 | this.value.setValue(value); 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/RollupFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | 6 | public class RollupFieldValue extends JamaFieldValue{ 7 | private Integer value; 8 | 9 | @Override 10 | public Integer getValue() { 11 | return value; 12 | } 13 | 14 | @Override 15 | public void setValue(String value) throws RestClientException { 16 | if(value == null) { 17 | this.value = null; 18 | return; 19 | } 20 | this.value = Integer.valueOf(value.substring(0, value.indexOf('%'))); 21 | } 22 | 23 | public void setValue(Integer value) { 24 | this.value = value; 25 | } 26 | 27 | 28 | public void setValue(Object value) throws JamaTypeMismatchException { 29 | throw new JamaTypeMismatchException("Rollup fields are not editable for field " + getName()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/TestCaseStatusFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | 6 | public class TestCaseStatusFieldValue extends JamaFieldValue { 7 | private String value; 8 | 9 | public String getValue() { 10 | return value; 11 | } 12 | 13 | public void setValue(String value) throws RestClientException { 14 | this.value = value; 15 | } 16 | public void setValue(Object value) throws JamaTypeMismatchException { 17 | throw new JamaTypeMismatchException("Test Case status is not editable for field " + getName()); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/TestCaseStepsFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.jamadomain.TestCaseStep; 6 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.PickListOption; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | public class TestCaseStepsFieldValue extends JamaFieldValue { 12 | List steps = new ArrayList<>(); 13 | @Override 14 | public List getValue() { 15 | return steps; 16 | } 17 | 18 | @Override 19 | public void setValue(String value) throws RestClientException { 20 | throw new RestClientException("testCaseSteps must call setValue(List steps)"); 21 | } 22 | public void setValue(Object value) throws JamaTypeMismatchException { 23 | checkType(List.class, value); 24 | List list = (List)value; 25 | List testCaseSteps = new ArrayList<>(); 26 | for(Object o : list) { 27 | checkType(TestCaseStep.class, o); 28 | testCaseSteps.add((TestCaseStep) o); 29 | } 30 | this.steps = testCaseSteps; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/TextBoxFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | 6 | public class TextBoxFieldValue extends JamaFieldValue { 7 | private String value; 8 | 9 | @Override 10 | public String getValue() { 11 | return value; 12 | } 13 | 14 | public void setValue(String value) throws RestClientException { 15 | this.value = value; 16 | } 17 | public void setValue(Object value) throws JamaTypeMismatchException { 18 | checkType(String.class, value); 19 | this.value = (String)value; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/TextFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | 6 | public class TextFieldValue extends JamaFieldValue { 7 | private String value; 8 | 9 | @Override 10 | public String getValue() { 11 | return value; 12 | } 13 | 14 | public void setValue(String value) { 15 | this.value = value; 16 | } 17 | 18 | public void setValue(Object value) throws JamaTypeMismatchException { 19 | checkType(String.class, value); 20 | this.value = (String)value; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/TimeFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; 6 | 7 | public class TimeFieldValue extends JamaFieldValue { 8 | private String value; 9 | 10 | @Override 11 | 12 | public Object getValue() { 13 | return value; 14 | } 15 | 16 | @Override 17 | public void setValue(String value) throws RestClientException { 18 | // todo: something better 19 | this.value = value; 20 | } 21 | 22 | public void setValue(Object value) throws JamaTypeMismatchException { 23 | checkType(String.class, value); 24 | this.value = (String)value; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/URLFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | 6 | public class URLFieldValue extends JamaFieldValue { 7 | private String value; 8 | 9 | @Override 10 | public String getValue() { 11 | return value; 12 | } 13 | 14 | public void setValue(String value) throws RestClientException { 15 | this.value = value; 16 | } 17 | 18 | 19 | public void setValue(Object value) throws JamaTypeMismatchException { 20 | checkType(String.class, value); 21 | this.value = (String)value; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/jamadomain/values/UserFieldValue.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.jamadomain.values; 2 | 3 | import com.jamasoftware.services.restclient.exception.JamaTypeMismatchException; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaUser; 5 | import com.jamasoftware.services.restclient.exception.RestClientException; 6 | 7 | public class UserFieldValue extends JamaFieldValue { 8 | private JamaUser value; 9 | 10 | @Override 11 | public JamaUser getValue() { 12 | return value; 13 | } 14 | 15 | @Override 16 | public void setValue(String value) throws RestClientException { 17 | if(value == null) { 18 | this.value = null; 19 | return; 20 | } 21 | int userId = Integer.valueOf(value); 22 | setValueFromPoolOrNew(JamaUser.class, userId); 23 | } 24 | 25 | 26 | public void setValue(Object value) throws JamaTypeMismatchException { 27 | checkType(JamaUser.class, value); 28 | this.value = (JamaUser) value; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/ItemTypeImage.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.exception.JsonException; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 6 | 7 | public class ItemTypeImage { 8 | private String imageUrl; 9 | private byte [] image; 10 | private JamaInstance jamaInstance; 11 | 12 | ItemTypeImage(){} 13 | 14 | public byte[] getImage() throws JsonException { 15 | try { 16 | image = jamaInstance.retrieveItemTypeImage(imageUrl); 17 | } catch (RestClientException e) { 18 | throw new JsonException(e); 19 | } 20 | return image; 21 | } 22 | 23 | public void setJamaInstance(JamaInstance jamaInstance) { 24 | this.jamaInstance = jamaInstance; 25 | } 26 | 27 | public void setImageUrl(String imageUrl) { 28 | this.imageUrl = imageUrl; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonHandler.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.exception.RestClientException; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 5 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 6 | import com.jamasoftware.services.restclient.jamaclient.JamaPage; 7 | 8 | public interface JsonHandler { 9 | JamaPage getPage(String json, JamaInstance jamaInstance) throws RestClientException; 10 | 11 | JamaDomainObject deserialize(String json, JamaInstance jamaInstance) throws RestClientException; 12 | 13 | String serializeCreated(JamaDomainObject jamaDomainObject) throws RestClientException; 14 | 15 | String serializeEdited(JamaDomainObject jamaDomainObject) throws RestClientException; 16 | 17 | Integer deserializeLocation(String response) throws RestClientException; 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonSerializer.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | public interface JsonSerializer { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonStagingItem.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.JamaLocation; 5 | import com.jamasoftware.services.restclient.jamadomain.LockStatus; 6 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaProject; 7 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaUser; 8 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingItem; 9 | 10 | import java.util.Date; 11 | 12 | 13 | public class JsonStagingItem extends StagingItem{ 14 | 15 | JsonStagingItem(){} 16 | 17 | public void setLocation(JamaLocation jamaLocation){ 18 | this.location = jamaLocation; 19 | } 20 | 21 | @Override 22 | public void copyContentFrom(JamaDomainObject jamaDomainObject) { 23 | super.copyContentFrom(jamaDomainObject); 24 | } 25 | 26 | public void setLock(LockStatus lockStatus) { 27 | this.lockStatus = lockStatus; 28 | } 29 | 30 | @Override 31 | public void writeContentTo(JamaDomainObject jamaItem){ 32 | super.writeContentTo(jamaItem); 33 | } 34 | 35 | public StagingItem setDocumentKey(String documentKey) { 36 | this.documentKey = documentKey; 37 | return this; 38 | } 39 | 40 | public StagingItem setCreatedDate(Date createdDate) { 41 | this.createdDate = createdDate; 42 | return this; 43 | } 44 | 45 | public StagingItem setModifiedDate(Date modifiedDate) { 46 | this.modifiedDate = modifiedDate; 47 | return this; 48 | } 49 | 50 | public StagingItem setLastActivityDate(Date lastActivityDate) { 51 | this.lastActivityDate = lastActivityDate; 52 | return this; 53 | } 54 | public StagingItem setCreatedBy(JamaUser createdBy) { 55 | this.createdBy = createdBy; 56 | return this; 57 | } 58 | 59 | public StagingItem setProject(JamaProject project) { 60 | this.project = project; 61 | return this; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonStagingItemType.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 5 | import com.jamasoftware.services.restclient.jamadomain.fields.JamaField; 6 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingJamaItemType; 7 | 8 | public class JsonStagingItemType extends StagingJamaItemType { 9 | 10 | JsonStagingItemType(){} 11 | 12 | @Override 13 | public void writeContentTo(JamaDomainObject jamaDomainObject) { 14 | super.writeContentTo(jamaDomainObject); 15 | } 16 | 17 | public JsonStagingItemType setTypeKey(String typeKey) { 18 | this.typeKey = typeKey; 19 | return this; 20 | } 21 | 22 | public JsonStagingItemType setDisplay(String display) { 23 | this.display = display; 24 | return this; 25 | } 26 | 27 | public JsonStagingItemType setDisplayPlural(String displayPlural) { 28 | this.displayPlural = displayPlural; 29 | return this; 30 | } 31 | 32 | // public JsonStagingItemType setImage(byte[] imageData) { 33 | // this.image = imageData; 34 | // return this; 35 | // } 36 | 37 | public JsonStagingItemType setImageJamaInstance(JamaInstance jamaInstance) { 38 | this.image.setJamaInstance(jamaInstance); 39 | return this; 40 | } 41 | 42 | public JsonStagingItemType setImageUrl(String imageUrl) { 43 | this.image.setImageUrl(imageUrl); 44 | return this; 45 | } 46 | 47 | public JsonStagingItemType addField(JamaField jamaField){ 48 | this.fields.add(jamaField); 49 | return this; 50 | } 51 | 52 | public JsonStagingItemType setImage(ItemTypeImage itemTypeImage) { 53 | this.image = itemTypeImage; 54 | return this; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonStagingPickList.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingPickList; 5 | 6 | public class JsonStagingPickList extends StagingPickList { 7 | public JsonStagingPickList setName(String name) { 8 | this.name = name; 9 | return this; 10 | } 11 | 12 | public JsonStagingPickList setDescription(String description) { 13 | this.description = description; 14 | return this; 15 | } 16 | 17 | @Override 18 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 19 | super.writeContentTo(jamaDomainObject); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonStagingPickListOption.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingPickListOption; 5 | 6 | public class JsonStagingPickListOption extends StagingPickListOption { 7 | 8 | JsonStagingPickListOption(){} 9 | 10 | @Override 11 | protected void writeContentTo(JamaDomainObject jamaDomainObject) { 12 | super.writeContentTo(jamaDomainObject); 13 | } 14 | 15 | public JsonStagingPickListOption setName(String name) { 16 | this.name = name; 17 | return this; 18 | } 19 | 20 | public JsonStagingPickListOption setActive(boolean active) { 21 | this.active = active; 22 | return this; 23 | } 24 | 25 | public JsonStagingPickListOption setDescription(String description) { 26 | this.description = description; 27 | return this; 28 | } 29 | 30 | public JsonStagingPickListOption setColor(String color) { 31 | this.color = color; 32 | return this; 33 | } 34 | 35 | public JsonStagingPickListOption setDefaultValue(boolean defaultValue) { 36 | this.defaultValue = defaultValue; 37 | return this; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonStagingProject.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaUser; 5 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingProject; 6 | 7 | import java.util.Date; 8 | 9 | public class JsonStagingProject extends StagingProject { 10 | 11 | JsonStagingProject(){} 12 | 13 | @Override 14 | public void writeContentTo(JamaDomainObject jamaDomainObject) { 15 | super.writeContentTo(jamaDomainObject); 16 | } 17 | 18 | public JsonStagingProject setModifiedDate(Date modifiedDate) { 19 | this.modifiedDate = modifiedDate; 20 | return this; 21 | } 22 | 23 | public JsonStagingProject setCreatedDate(Date createdDate) { 24 | this.createdDate = createdDate; 25 | return this; 26 | } 27 | 28 | public JsonStagingProject setCreatedBy(JamaUser createdBy) { 29 | this.createdBy = createdBy; 30 | return this; 31 | } 32 | 33 | public JsonStagingProject setModifiedBy(JamaUser modifiedBy) { 34 | this.modifiedBy = modifiedBy; 35 | return this; 36 | } 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonStagingRelationship.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaRelationshipType; 5 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingRelationship; 6 | 7 | public class JsonStagingRelationship extends StagingRelationship { 8 | 9 | JsonStagingRelationship(){} 10 | 11 | public JsonStagingRelationship setSuspect(boolean suspect){ 12 | this.suspect = suspect; 13 | return this; 14 | } 15 | 16 | public JsonStagingRelationship setRelationshipType(JamaRelationshipType jamaRelationshipType){ 17 | this.relationshipType = jamaRelationshipType; 18 | return this; 19 | } 20 | 21 | @Override 22 | public void writeContentTo(JamaDomainObject jamaDomainObject){ 23 | super.writeContentTo(jamaDomainObject); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonStagingRelationshipType.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingRelationshipType; 5 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingResource; 6 | 7 | public class JsonStagingRelationshipType extends StagingRelationshipType implements StagingResource{ 8 | 9 | JsonStagingRelationshipType(){} 10 | 11 | @Override 12 | public void writeContentTo(JamaDomainObject jamaDomainObject) { 13 | super.writeContentTo(jamaDomainObject); 14 | } 15 | 16 | public JsonStagingRelationshipType setName(String name) { 17 | this.name = name; 18 | return this; 19 | } 20 | 21 | public JsonStagingRelationshipType setDefault(boolean aDefault) { 22 | this.isDefault = aDefault; 23 | return this; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonStagingRelease.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingRelease; 5 | 6 | public class JsonStagingRelease extends StagingRelease { 7 | 8 | JsonStagingRelease() {} 9 | 10 | @Override 11 | public void writeContentTo(JamaDomainObject jamaDomainObject) { 12 | super.writeContentTo(jamaDomainObject); 13 | } 14 | 15 | public JsonStagingRelease setItemCount(int itemCount){ 16 | this.itemCount = itemCount; 17 | return this; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/JsonStagingUser.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingUser; 5 | 6 | public class JsonStagingUser extends StagingUser { 7 | JsonStagingUser(){} 8 | 9 | public void writeContentTo(JamaDomainObject jamaDomainObject) { 10 | super.writeContentTo(jamaDomainObject); 11 | } 12 | 13 | public void setActive(boolean active) { 14 | this.active = active; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/SimpleJsonHandler.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 4 | import com.jamasoftware.services.restclient.jamaclient.JamaPage; 5 | import com.jamasoftware.services.restclient.exception.JsonException; 6 | import com.jamasoftware.services.restclient.exception.RestClientException; 7 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 8 | 9 | public class SimpleJsonHandler implements JsonHandler { 10 | private SimpleJsonDeserializer deserializer = new SimpleJsonDeserializer(); 11 | private SimpleJsonSerializer serializer = new SimpleJsonSerializer(); 12 | 13 | public JamaDomainObject deserialize(String json, JamaInstance jamaInstance) throws JsonException, RestClientException { 14 | return deserializer.deserialize(json, jamaInstance); 15 | } 16 | 17 | @Override 18 | public JamaPage getPage(String json, JamaInstance jamaInstance) throws JsonException, RestClientException { 19 | return deserializer.getPage(json, jamaInstance); 20 | } 21 | 22 | @Override 23 | public String serializeCreated(JamaDomainObject jamaDomainObject) throws RestClientException { 24 | return serializer.serializeCreated(jamaDomainObject); 25 | } 26 | 27 | @Override 28 | public String serializeEdited(JamaDomainObject jamaDomainObject) throws RestClientException { 29 | return serializer.serializeEdited(jamaDomainObject); 30 | } 31 | 32 | @Override 33 | public Integer deserializeLocation(String response) throws RestClientException{ 34 | return deserializer.deserializeLocation(response); 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/SimpleJsonSerializer.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.JamaParent; 4 | import com.jamasoftware.services.restclient.exception.JsonException; 5 | import com.jamasoftware.services.restclient.exception.RestClientException; 6 | import com.jamasoftware.services.restclient.jamadomain.core.JamaDomainObject; 7 | import com.jamasoftware.services.restclient.jamadomain.core.LazyResource; 8 | import com.jamasoftware.services.restclient.jamadomain.TestCaseStep; 9 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.*; 10 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingItem; 11 | import com.jamasoftware.services.restclient.jamadomain.stagingresources.StagingRelationship; 12 | import com.jamasoftware.services.restclient.jamadomain.values.*; 13 | import org.json.simple.JSONArray; 14 | import org.json.simple.JSONObject; 15 | 16 | public class SimpleJsonSerializer implements JsonSerializer { 17 | private SimpleJsonUtil util = new SimpleJsonUtil(); 18 | 19 | 20 | public String serializeCreated(JamaDomainObject jamaDomainObject) throws JsonException, RestClientException { 21 | return serialize(jamaDomainObject, true); 22 | } 23 | 24 | public String serializeEdited(JamaDomainObject jamaDomainObject) throws JsonException, RestClientException { 25 | return serialize(jamaDomainObject, false); 26 | } 27 | 28 | private String serialize(JamaDomainObject object, boolean create) throws JsonException, RestClientException { 29 | JSONObject jsonObject = null; 30 | 31 | if(object instanceof StagingItem) { 32 | jsonObject = serializeItem((StagingItem)object, create); 33 | } 34 | if(object instanceof StagingRelationship) { 35 | jsonObject = serializeRelationship((StagingRelationship) object); 36 | } 37 | if(jsonObject == null) { 38 | throw new JsonException("Unable to serialize " + object.getClass() + 39 | " to JSON for " + object); 40 | } 41 | return jsonObject.toJSONString(); 42 | } 43 | 44 | 45 | private JSONObject serializeItem(StagingItem jamaItem, boolean create) throws JsonException { 46 | JSONObject payload = serializeItem(jamaItem); 47 | 48 | if(create) { 49 | if(jamaItem.getParent().isProject()) { 50 | util.putIfNotNull(payload, "project", getIdOrNull((LazyResource)jamaItem.getParent())); 51 | } 52 | util.putIfNotNull(payload, "itemType", getIdOrNull(jamaItem.getItemType())); 53 | } 54 | return payload; 55 | } 56 | 57 | 58 | @SuppressWarnings("unchecked") 59 | private JSONObject serializeItem(StagingItem jamaItem) { 60 | JSONObject itemJson = new JSONObject(); 61 | JSONObject fields = new JSONObject(); 62 | JSONObject location = new JSONObject(); 63 | JSONObject parent = new JSONObject(); 64 | 65 | itemJson.put("fields", fields); 66 | itemJson.put("location", location); 67 | location.put("parent", parent); 68 | 69 | fields.put("name", serializeValue(jamaItem.getName())); 70 | for(JamaFieldValue value : jamaItem.getFieldValues()) { 71 | util.putIfNotNull(fields, value.getName(), serializeValue(value)); 72 | } 73 | 74 | util.putIfNotNull(itemJson, "childItemType", getIdOrNull(jamaItem.getChildItemType())); 75 | util.putIfNotNull(itemJson, "project", getIdOrNull(jamaItem.getProject())); 76 | 77 | util.putIfNotNull(itemJson, "globalId", jamaItem.getGlobalId()); 78 | 79 | JamaParent jamaParent = jamaItem.getParent(); 80 | if(jamaParent instanceof JamaProject) { 81 | parent.put("project", ((JamaProject) jamaParent).getId()); 82 | } else if(jamaParent instanceof JamaItem) { 83 | parent.put("item", ((JamaItem) jamaParent).getId()); 84 | } 85 | 86 | return itemJson; 87 | } 88 | 89 | @SuppressWarnings("unchecked") 90 | private JSONObject serializeRelationship(StagingRelationship stagingRelationship) { 91 | JSONObject relationshipJson = new JSONObject(); 92 | relationshipJson.put("fromItem", stagingRelationship.getFromItem().getId()); 93 | relationshipJson.put("toItem", stagingRelationship.getToItem().getId()); 94 | relationshipJson.put("relationshipType", stagingRelationship.getRelationshipType().getId()); 95 | return relationshipJson; 96 | } 97 | 98 | 99 | private Object serializeValue(JamaFieldValue value) { 100 | if(value.readOnly()) return null; 101 | if(value instanceof CalculatedFieldValue) return null; 102 | if(value instanceof RollupFieldValue) return null; 103 | if(value instanceof TestCaseStatusFieldValue) return null; 104 | if(value instanceof RichTextFieldValue) return ((RichTextFieldValue)value).getValue().getValue(); 105 | if(value instanceof MultiSelectFieldValue) { 106 | MultiSelectFieldValue multiSelect = (MultiSelectFieldValue)value; 107 | JSONArray options = new JSONArray(); 108 | for(PickListOption option : multiSelect.getValue()) { 109 | options.add(option.getId()); 110 | } 111 | return options; 112 | } 113 | if(value instanceof PickListFieldValue) { 114 | return getIdOrNull(((PickListFieldValue)value).getValue()); 115 | } 116 | if(value instanceof ProjectFieldValue) { 117 | return getIdOrNull(((ProjectFieldValue)value).getValue()); 118 | } 119 | if(value instanceof ReleaseFieldValue) { 120 | return getIdOrNull(((ReleaseFieldValue)value).getValue()); 121 | } 122 | if(value instanceof TestCaseStepsFieldValue) { 123 | TestCaseStepsFieldValue steps = (TestCaseStepsFieldValue)value; 124 | JSONArray stepArray = new JSONArray(); 125 | for(TestCaseStep step : steps.getValue()) { 126 | stepArray.add(serializeStep(step)); 127 | } 128 | return stepArray; 129 | } 130 | if(value instanceof UserFieldValue) { 131 | return getIdOrNull(((UserFieldValue)value).getValue()); 132 | } 133 | 134 | return value.getValue(); 135 | } 136 | 137 | private JSONObject serializeStep(TestCaseStep step) { 138 | JSONObject stepJson = new JSONObject(); 139 | util.putIfNotNull(stepJson, "action", step.getAction()); 140 | util.putIfNotNull(stepJson, "expectedResults", step.getExpectedResult()); 141 | util.putIfNotNull(stepJson, "notes", step.getNotes()); 142 | return stepJson; 143 | } 144 | 145 | public JSONObject serializeProject(JamaProject jamaProject) { 146 | JSONObject project = new JSONObject(); 147 | 148 | util.putIfNotNull(project, "isFolder", jamaProject.isFolder()); 149 | util.putIfNotNull(project, "projectKey", jamaProject.getProjectKey()); 150 | util.putIfNotNull(project, "parent", 0); 151 | 152 | JSONObject fields = new JSONObject(); 153 | util.putIfNotNull(fields, "name", jamaProject.getName()); 154 | util.putIfNotNull(fields, "description", jamaProject.getDescription()); 155 | util.putIfNotNull(fields, "legacyId", jamaProject.getId()); 156 | util.putIfNotNull(project, "fields", fields); 157 | return project; 158 | } 159 | 160 | private Integer getIdOrNull(LazyResource object) { 161 | return object == null ? null : object.getId(); 162 | } 163 | 164 | } 165 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/json/SimpleJsonUtil.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.json; 2 | 3 | import com.jamasoftware.services.restclient.exception.JsonException; 4 | import com.jamasoftware.services.restclient.util.DateUtil; 5 | import org.json.simple.JSONArray; 6 | import org.json.simple.JSONObject; 7 | import org.json.simple.parser.JSONParser; 8 | import org.json.simple.parser.ParseException; 9 | 10 | import java.util.Date; 11 | 12 | class SimpleJsonUtil { 13 | 14 | // Type checking and casting 15 | private int longToInt(long value) throws JsonException { 16 | if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { 17 | throw new JsonException("Attempt to cast long to int would result in lost information."); 18 | } 19 | return (int) value; 20 | } 21 | 22 | private void validateObjectAndKey(JSONObject jsonObject, String key) throws JsonException { 23 | if (jsonObject == null) { 24 | throw new JsonException("JSON Object is null"); 25 | } 26 | if (key == null || key.length() == 0) { 27 | throw new JsonException("Empty key"); 28 | } 29 | } 30 | 31 | private boolean testType(Object object, Class clazz) throws JsonException { 32 | return object == null || clazz.isInstance(object); 33 | } 34 | 35 | // Require 36 | 37 | private Object require(JSONObject jsonObject, String key) throws JsonException { 38 | validateObjectAndKey(jsonObject, key); 39 | Object object = jsonObject.get(key); 40 | if (object == null) { 41 | throw new JsonException("No value for key \"" + key + "\" in object: " + jsonObject.toJSONString()); 42 | } 43 | return object; 44 | } 45 | 46 | private Object require(JSONObject jsonObject, String key, Class clazz) throws JsonException { 47 | Object object = require(jsonObject, key); 48 | if (testType(object, clazz)) { 49 | return object; 50 | } 51 | throw new JsonException("Expected \"" + key + "\" to be type " + clazz.getName() + " in object: " + jsonObject.toJSONString()); 52 | } 53 | 54 | String requireString(JSONObject jsonObject, String key) throws JsonException { 55 | return (String) require(jsonObject, key, String.class); 56 | } 57 | 58 | Integer requireInt(JSONObject jsonObject, String key) throws JsonException { 59 | return longToInt((Long) require(jsonObject, key, Long.class)); 60 | } 61 | 62 | JSONObject requireObject(JSONObject jsonObject, String key) throws JsonException { 63 | return (JSONObject) require(jsonObject, key, JSONObject.class); 64 | } 65 | 66 | JSONArray requireArray(JSONObject jsonObject, String key) throws JsonException { 67 | return (JSONArray) require(jsonObject, key, JSONArray.class); 68 | } 69 | 70 | Boolean requireBoolean(JSONObject jsonObject, String key) throws JsonException { 71 | return (Boolean) require(jsonObject, key, Boolean.class); 72 | } 73 | 74 | // Request 75 | 76 | private Object request(JSONObject jsonObject, String key) throws JsonException { 77 | validateObjectAndKey(jsonObject, key); 78 | return jsonObject.get(key); 79 | } 80 | 81 | private Object request(JSONObject jsonObject, String key, Class clazz) throws JsonException { 82 | Object object = request(jsonObject, key); 83 | if (testType(object, clazz)) { 84 | return object; 85 | } 86 | throw new JsonException("Expected \"" + key + "\" to be type " + clazz.getName() + " in object: " + jsonObject.toJSONString()); 87 | } 88 | 89 | Date requestDate(JSONObject jsonObject, String key) throws JsonException { 90 | String dateString = requestString(jsonObject, key); 91 | try { 92 | return DateUtil.parseDate(dateString); 93 | } catch (java.text.ParseException e) { 94 | throw new JsonException(e); 95 | } 96 | } 97 | 98 | String requestString(JSONObject jsonObject, String key) throws JsonException { 99 | return (String) request(jsonObject, key, String.class); 100 | } 101 | 102 | Integer requestInt(JSONObject jsonObject, String key) throws JsonException { 103 | Object object = request(jsonObject, key, Long.class); 104 | if(object == null) { 105 | return null; 106 | } 107 | return longToInt((Long) object); 108 | } 109 | 110 | JSONObject requestObject(JSONObject jsonObject, String key) throws JsonException { 111 | return (JSONObject) request(jsonObject, key, JSONObject.class); 112 | } 113 | 114 | // Misc 115 | 116 | String getFieldValue(JSONObject jsonObject, String key, int itemTypeId) throws JsonException { 117 | Object fieldObject = request(jsonObject, key); 118 | Object customFieldObject = request(jsonObject, key + "$" + itemTypeId); 119 | if (fieldObject == null && customFieldObject == null) { 120 | return null; 121 | } 122 | if (fieldObject != null && customFieldObject != null && !fieldObject.equals(customFieldObject)) { 123 | throw new JsonException("Two different values exist for field " + key); 124 | } 125 | if (fieldObject != null) { 126 | return fieldObject.toString(); 127 | } 128 | return customFieldObject.toString(); 129 | } 130 | 131 | JSONObject parseObject(String json, JSONParser jsonParser) throws JsonException { 132 | try { 133 | JSONObject jsonObject = (JSONObject) jsonParser.parse(json); 134 | Object data = jsonObject.get("data"); 135 | if (data instanceof JSONObject) { 136 | return (JSONObject) data; 137 | } 138 | return jsonObject; 139 | 140 | } catch (ParseException e) { 141 | throw new JsonException(e); 142 | } 143 | } 144 | 145 | @SuppressWarnings("unchecked") 146 | void putIfNotNull(JSONObject object, String key, Object value) { 147 | if(value != null) { 148 | object.put(key, value); 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/util/CompareUtil.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.util; 2 | 3 | public class CompareUtil { 4 | // This will be modified further to accommodate other checks for misspelled words 5 | public static boolean closeEnough(String one, String two) { 6 | return one.equals(two) 7 | || one.toLowerCase().equals(two.toLowerCase()) 8 | || one.toLowerCase().replaceAll(" ", "_").equals(two.toLowerCase().replaceAll(" ", "_")); 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/jamasoftware/services/restclient/util/DateUtil.java: -------------------------------------------------------------------------------- 1 | package com.jamasoftware.services.restclient.util; 2 | 3 | import java.text.DateFormat; 4 | import java.text.ParseException; 5 | import java.text.SimpleDateFormat; 6 | import java.util.Date; 7 | 8 | public class DateUtil { 9 | private static DateFormat dateFormatFull; 10 | private static DateFormat dateFormatTerse; 11 | private static DateFormat monthDayYearFormat; 12 | public static Date parseDate(String date) throws ParseException { 13 | if(date == null || date.length() == 0) return null; 14 | if(dateFormatFull == null) { 15 | dateFormatFull = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 16 | } 17 | try { 18 | return dateFormatFull.parse(date); 19 | } catch (ParseException e) { 20 | return parseDateTerse(date); 21 | } 22 | } 23 | 24 | private static Date parseDateTerse(String date) throws ParseException { 25 | if(dateFormatTerse == null) { 26 | dateFormatTerse = new SimpleDateFormat("yyyy-MM-dd"); 27 | } 28 | return dateFormatTerse.parse(date); 29 | } 30 | 31 | public static String monthDayYear(Date date) { 32 | if(monthDayYearFormat == null) { 33 | monthDayYearFormat = new SimpleDateFormat("MM/dd/yyyy"); 34 | } 35 | return monthDayYearFormat.format(date); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test/restclient/jamadomain/lazyresources/JamaItemTest.java: -------------------------------------------------------------------------------- 1 | package restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.JamaConfig; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.httpconnection.TestHttpClient; 6 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 7 | import com.jamasoftware.services.restclient.jamadomain.fields.RichTextField; 8 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItem; 9 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItemType; 10 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaProject; 11 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaUser; 12 | import com.jamasoftware.services.restclient.jamadomain.values.*; 13 | import junit.framework.TestCase; 14 | 15 | import java.lang.reflect.Field; 16 | import java.util.Date; 17 | import java.util.concurrent.TimeUnit; 18 | 19 | public class JamaItemTest extends TestCase { 20 | 21 | public void testRetrieval() throws RestClientException { 22 | JamaConfig jamaConfig = new JamaConfig(); 23 | jamaConfig.setBaseUrl("test"); 24 | jamaConfig.setHttpClient(new TestHttpClient()); 25 | jamaConfig.setPassword(""); 26 | jamaConfig.setUsername(""); 27 | jamaConfig.setResourceTimeOut(1); 28 | 29 | JamaInstance jamaInstance = new JamaInstance(jamaConfig); 30 | JamaItem jamaItem = jamaInstance.getItem(2210690); 31 | 32 | assertEquals("Test Item", jamaItem.getName().getValue()); 33 | assertEquals("GID-AFD-113502", jamaItem.getGlobalId()); 34 | assertEquals("Z-AFD-1", jamaItem.getDocumentKey()); 35 | assertEquals(JamaItemType.class, jamaItem.getItemType().getClass()); 36 | assertEquals(JamaProject.class, jamaItem.getProject().getClass()); 37 | assertNull(jamaItem.getChildItemType()); 38 | Date createdDate = new Date(1478733450000L); 39 | assertEquals(createdDate, jamaItem.getCreatedDate()); 40 | assertNull(jamaItem.getModifiedDate()); 41 | assertEquals(JamaUser.class, jamaItem.getCreatedBy().getClass()); 42 | assertEquals(jamaItem.getSequence(), "1.1"); 43 | //TODO assertEquals(jamaItem.getParent().getName(), "AllFieldsDemos"); 44 | assertFalse(jamaItem.isLocked()); 45 | assertEquals(createdDate, jamaItem.lastLockedDate()); 46 | assertEquals(createdDate, jamaItem.lastLockedDate()); 47 | assertNull(jamaItem.lockedBy()); 48 | assertEquals(2210690, jamaItem.getId().intValue()); 49 | try { 50 | refresh(jamaItem); 51 | fields(jamaItem); 52 | } catch (Exception e) { 53 | throw new RestClientException(e); 54 | } 55 | 56 | } 57 | 58 | public void refresh(JamaItem jamaItem) throws Exception { 59 | Field field = JamaItem.class.getDeclaredField("name"); 60 | field.setAccessible(true); 61 | TextFieldValue oldNameValue = new TextFieldValue(); 62 | oldNameValue.setValue("oldName"); 63 | field.set(jamaItem, oldNameValue); 64 | assertEquals("oldName", field.get(jamaItem).toString()); 65 | TimeUnit.SECONDS.sleep(1); 66 | assertEquals("Test Item", jamaItem.getName().getValue()); 67 | } 68 | 69 | public void checkField(Class clazz, String value, JamaFieldValue fieldValue) { 70 | assertTrue(clazz.isInstance(fieldValue)); 71 | assertEquals(value, fieldValue.toString()); 72 | } 73 | 74 | public void fields(JamaItem jamaItem) throws Exception { 75 | checkField(TextFieldValue.class, "Z-AFD-1", jamaItem.getFieldValueByName("documentKey")); 76 | checkField(RichTextFieldValue.class, "

Description

\n", jamaItem.getFieldValueByName("description")); 77 | //TODO: test with multiSelectFieldValue 78 | checkField(FlagFieldValue.class, "true", jamaItem.getFieldValueByName("flag")); 79 | assertEquals(new Date(1478592000000L), jamaItem.getFieldValueByName("date").getValue()); 80 | checkField(DateFieldValue.class, "Tue Nov 08 00:00:00 PST 2016", jamaItem.getFieldValueByName("date")); 81 | checkField(URLFieldValue.class, "http://www.google.com", jamaItem.getFieldValueByName("url")); 82 | //TODO: test with userFieldValue, PickListFieldValue, ReleaseFieldValue 83 | checkField(RollupFieldValue.class, "50", jamaItem.getFieldValueByName("rollup")); 84 | checkField(CalculatedFieldValue.class, "3", jamaItem.getFieldValueByName("calculated")); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /test/restclient/jamadomain/lazyresources/JamaItemTypeTest.java: -------------------------------------------------------------------------------- 1 | package restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.JamaConfig; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.httpconnection.TestHttpClient; 6 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 7 | import com.jamasoftware.services.restclient.jamadomain.fields.*; 8 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItemType; 9 | import junit.framework.TestCase; 10 | 11 | import java.lang.reflect.Field; 12 | import java.util.ArrayList; 13 | import java.util.concurrent.TimeUnit; 14 | 15 | public class JamaItemTypeTest extends TestCase { 16 | 17 | public void testItemType() throws RestClientException { 18 | JamaConfig jamaConfig = new JamaConfig(); 19 | jamaConfig.setBaseUrl("test"); 20 | jamaConfig.setHttpClient(new TestHttpClient()); 21 | jamaConfig.setPassword(""); 22 | jamaConfig.setUsername(""); 23 | jamaConfig.setResourceTimeOut(1); 24 | 25 | JamaInstance jamaInstance = new JamaInstance(jamaConfig); 26 | JamaItemType jamaItemType= jamaInstance.getItemType(89011); 27 | 28 | assertEquals(89011, jamaItemType.getId().intValue()); 29 | assertEquals("TC", jamaItemType.getTypeKey().toString()); 30 | assertEquals("Test Case", jamaItemType.getDisplay().toString()); 31 | assertEquals("Test Cases", jamaItemType.getDisplayPlural().toString()); 32 | assertEquals(JamaItemType.class, jamaItemType.getClass()); 33 | //TODO: need to getImage and check it is of type ItemTypeImage 34 | assertEquals(ArrayList.class, jamaItemType.getFields().getClass()); 35 | try { 36 | refresh(jamaItemType); 37 | fields(jamaItemType); 38 | } catch (Exception e) { 39 | throw new RestClientException(e); 40 | } 41 | System.out.println("Done"); 42 | } 43 | 44 | public void refresh(JamaItemType jamaItemType) throws Exception { 45 | 46 | Field field = JamaItemType.class.getDeclaredField("display"); 47 | field.setAccessible(true); 48 | String oldName = "oldName"; 49 | field.set(jamaItemType, oldName); 50 | assertEquals("oldName", field.get(jamaItemType)); 51 | TimeUnit.SECONDS.sleep(1); 52 | assertEquals("Test Case", jamaItemType.getDisplay()); 53 | } 54 | 55 | public void checkField(Class clazz, JamaField fieldValue) { 56 | assertTrue(clazz.isInstance(fieldValue)); 57 | } 58 | 59 | public void fields(JamaItemType jamaItemType) throws Exception, RestClientException { 60 | checkField(RichTextField.class, jamaItemType.getField("description")); 61 | checkField(TextField.class, jamaItemType.getField("name")); 62 | checkField(TestCaseStatusField.class, jamaItemType.getField("testCaseStatus")); 63 | checkField(TestCaseStepsField.class, jamaItemType.getField("testCaseSteps")); 64 | checkField(UserField.class, jamaItemType.getField("assignedTo")); 65 | checkField(PickListField.class, jamaItemType.getField("priority")); 66 | checkField(IntegerField.class, jamaItemType.getField("custom_number")); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /test/restclient/jamadomain/lazyresources/JamaProjectTest.java: -------------------------------------------------------------------------------- 1 | package restclient.jamadomain.lazyresources; 2 | 3 | import com.jamasoftware.services.restclient.JamaConfig; 4 | import com.jamasoftware.services.restclient.exception.RestClientException; 5 | import com.jamasoftware.services.restclient.httpconnection.TestHttpClient; 6 | import com.jamasoftware.services.restclient.jamadomain.core.JamaInstance; 7 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaItem; 8 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaProject; 9 | import com.jamasoftware.services.restclient.jamadomain.lazyresources.JamaUser; 10 | import junit.framework.TestCase; 11 | 12 | import java.util.Date; 13 | import java.util.List; 14 | 15 | public class JamaProjectTest extends TestCase { 16 | 17 | public void testProject() throws RestClientException { 18 | 19 | JamaConfig jamaConfig = new JamaConfig(); 20 | jamaConfig.setBaseUrl("test"); 21 | jamaConfig.setHttpClient(new TestHttpClient()); 22 | jamaConfig.setPassword(""); 23 | jamaConfig.setUsername(""); 24 | jamaConfig.setResourceTimeOut(1); 25 | 26 | JamaInstance jamaInstance = new JamaInstance(jamaConfig); 27 | JamaProject jamaProject = jamaInstance.getProject(20183); 28 | 29 | assertEquals("CoveragePlus - Agile", jamaProject.getName()); 30 | assertEquals("CPA", jamaProject.getProjectKey()); 31 | assertEquals(20183, jamaProject.getId().intValue()); 32 | assertEquals("This project is constructed using artifacts that are most common when using an Agile project methodology.", 33 | jamaProject.getDescription()); 34 | assertTrue(Date.class.isInstance(jamaProject.getCreatedDate())); 35 | Date createdDate = new Date(1323785140000L); 36 | assertEquals(createdDate, jamaProject.getCreatedDate()); 37 | assertEquals(JamaProject.class, jamaProject.getClass()); 38 | assertFalse(jamaProject.isFolder()); 39 | assertTrue(JamaUser.class.isInstance(jamaProject.getCreatedBy())); 40 | List children = jamaProject.getChildren(); 41 | System.out.println("done"); 42 | 43 | // assertFalse(jamaProject.isLocked()); 44 | // assertEquals(createdDate, jamaProject.lastLockedDate()); 45 | // assertEquals(createdDate, jamaProject.lastLockedDate()); 46 | // assertNull(jamaProject.lockedBy()); 47 | // assertEquals(2210690, jamaProject.getId().intValue()); 48 | // try { 49 | // refresh(jamaProject); 50 | // fields(jamaProject); 51 | // } catch (Exception e) { 52 | // throw new RestClientException(e); 53 | // } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /testData/testApiPayloads.json: -------------------------------------------------------------------------------- 1 | { 2 | 3 | } --------------------------------------------------------------------------------