Stitches Cookbook
The following snippets are from the stitches unit tests. There are 52+ tests at this point. They would be the best resource if you are trying to figure out how to do something.
Create a content definition:
ContentDefRequest cdreq = new ContentDefRequest()
ContentDefHolder cdh = new ContentDefHolder()
cdreq.contentDefHolder = cdh
cdh.setName(name)
ContentDefResponse cdres = stitchesService.saveContentDef(cdreq)
Long contentDefId = cdres.getContentDefId();
Add an attribute to your content definition:
ContentDefRequest attrRequest = new ContentDefRequest()
AttributeHolder attributeHolder =new AttributeHolder()
attrRequest.attributeHolder = attributeHolder
attributeHolder.setName(args.attributeName)
attributeHolder.setValueType(args.valueType)
attributeHolder.setMimeType(args.mimeType)
//binaryType is default to None
attributeHolder.setBinaryType(args.binaryType);
if (args.publishToAmazonS3) {
attributeHolder.publishToAmazonS3 = args.publishToAmazonS3
}
attrRequest.setContentDefId(args.contentDefId)
ContentDefResponse attrResponse = stitchesService.addAttribute(attrRequest)
Long attributeId = attrResponse.attributeHolder.id
Create a content:
Long getContentId(Map parms) {
ContentRequest contentRequest = new ContentRequest()
ContentHolder contentHolder = new ContentHolder()
contentRequest.contentHolder = contentHolder
assert parms.contentDefId != null
assert parms.name != null
contentHolder.setContentDefId(parms.contentDefId)
contentHolder.setName(parms.name)
ContentResponse cres = stitchesService.saveContent(contentRequest)
assertEquals "200" , cres.responseCode
Long contentId = cres.contentHolders.get(0).id
assertTrue contentId > 0
return contentId;
}
Create a content version:
Long getContentVersionId(Long contentId) {
ContentVersionRequest request = new ContentVersionRequest()
request.contentVersionHolder = new ContentVersionHolder()
request.contentVersionHolder.contentId = contentId
request.contentVersionHolder.name = "another version"
ContentVersionResponse response = stitchesService.saveContentVersion(request)
assertEquals response.responseCode, "200"
assertTrue response.contentVersionHolders.get(0).id > 0
return response.contentVersionHolders.get(0).id
}
Store an attribute value:
AttributeValuesRequest getAttributeValuesRequest(Map args) {
AttributeValuesRequest request = new AttributeValuesRequest()
request.setContentVersionId(args.contentVersionId)
AttributeValueHolder avh = new AttributeValueHolder()
avh.attributeId = args.attributeId
if (args.filePath) {
byte[] bytes = getBytes(args.filePath)
avh.setBinaryData(bytes)
}
avh.setValueAsString(args.valueAsString)
avh.setValueAsInteger(args.valueAsInteger)
avh.setLatitude(args.latitude)
avh.setLongitude(args.longitude)
request.attributeValueHolders.add(avh)
return request
}
Long getAttributeValueId(Map args) {
AttributeValuesRequest request = getAttributeValuesRequest(args)
AttributeValuesResponse response = stitchesService.saveAttributeValues(request)
assertEquals "200" ,response.responseCode
request = new AttributeValuesRequest()
request.setContentVersionId(args.contentVersionId)
request.attributeIds.add(args.attributeId)
response = stitchesService.retrieveAttributeValues(request)
assertEquals "200" ,response.responseCode
return response.attributeValueHolders.get(0).id
}
Remove an attribute value
void testRemoveByAttributeValueId() {
//create a content definition
Long contentDefId = getContentDefId(name:"testRemoveByAttributeValueId")
//create an attribute
Long attributeId = createAttribute( attributeName:"testRemoveByAttributeValueId",valueType:"Binary",contentDefId:contentDefId,required:true,mimeType:"image/jpg",binaryType:"Image")
//create a content
Long contentId = getContentId(contentDefId:contentDefId,name:"testRemoveByAttributeValueId");
//create a content version
Long contentVersionId = getContentVersionId(contentId)
//create an attribute value
Long attributeValueId = getAttributeValueId(contentVersionId:contentVersionId,attributeId:attributeId,filePath:sampleImagePath)
AttributeValuesRequest request = new AttributeValuesRequest()
request.setContentVersionId(contentVersionId)
request.attributeValueIds.add(attributeValueId)
//call to remove attribute value
AttributeValuesResponse response = stitchesService.removeAttributeValues(request)
assertEquals response.responseCode, "200"
assertTrue response.attributeValueHolders.size() > 0
}
Retrieve an attribute value
void testRetrieveByAttributeValueId() {
Long contentDefId = getContentDefId(name:"testRetrieveByAttributeValueId")
Long contentId = getContentId(contentDefId:contentDefId,name:"testRetrieveByAttributeValueId");
Long contentVersionId = getContentVersionId(contentId)
Long attributeId = createAttribute( attributeName:"testRetrieveByAttributeValueId",valueType:"Binary",contentDefId:contentDefId,required:true,mimeType:"image/jpg",binaryType:"Image")
Long attributeValueId = getAttributeValueId(contentVersionId:contentVersionId,attributeId:attributeId,filePath:sampleImagePath)
AttributeValuesRequest request = new AttributeValuesRequest()
request.setContentVersionId(contentVersionId)
request.attributeValueIds.add(attributeValueId)
AttributeValuesResponse response = stitchesService.removeAttributeValues(request)
assertEquals response.responseCode, "200"
}
Search by keyword
void testSearchContentByKeywordInPDF() {
Long contentVersionId = setupPdfContent(name:"testSearchContentByKeywordInPDF")
//do a keyword search
SearchRequest searchRequest = new SearchRequest()
searchRequest.setKeywords("Convention over Configuration")
SearchResponse searchResponse = stitchesService.search(searchRequest)
assertEquals searchResponse.responseCode, "200"
assertTrue searchResponse.searchResults.size() > 0
//assertEquals searchResponse.searchResults.get(0).contentVersionId , contentVersionId
//let's do a keyword search that should fail
searchRequest = new SearchRequest()
searchRequest.setKeywords("kalamozoo")
searchResponse = stitchesService.search(searchRequest)
assertEquals searchResponse.responseCode, "200"
assertTrue searchResponse.searchResults.size() == 0
}
Search by folder
void testSearchContentFolder() {
Long folderId = getFolderId(name:"testSearchContentFolder")
Long contentDefId = getContentDefId(name:"testSearchContentFolder")
Long contentId = getContentId(contentDefId:contentDefId,name:"testSearchContentFolder")
createFolderContent(folderId,contentId)
SearchRequest request = new SearchRequest()
request.setFolderId(folderId)
SearchResponse response = stitchesService.search(request)
assertEquals response.responseCode, "200"
assertTrue response.searchResults.size() > 0
}
Perform a nearby search (longitude latitude)
void testZipCodes() {
//create a content def of a zipcode
Long contentDefId = getContentDefId(name:"zipcode")
//create a zip code attribute
Long zipAttributeId = createAttribute(contentDefId:contentDefId,attributeName:"zipcode",valueType:"String")
//create a long lat attribute
Long longLatAttributeId = createAttribute(contentDefId:contentDefId,attributeName:"long-lat",valueType:"Longitude & Latitude",required:true)
//import from csv file a bunch of zip codes with long lats
runImport(contentDefId:contentDefId,zipAttributeId:zipAttributeId,longLatAttributeId:longLatAttributeId)
//let's do a nearby search
//this is one of the first zipcodes, the first few zipcodes
//have same long lat, so if we do a nearbysearch, we should get a few back
//city state latitude longitude
//Portsmouth NH 43.005895 -71.013202
//this will setup the search request
SearchRequest searchRequest = new SearchRequest()
searchRequest.longitude = -71.013202
searchRequest.latitude = 43.005895
searchRequest.mileRadius = 30.0
def longlats = LongLat.findAll()
longlats.each {
println it.id + " " + it.longitude + " " + it.latitude
}
SearchResponse searchResponse = stitchesService.search(searchRequest)
assertEquals "200",searchResponse.responseCode
assertTrue searchResponse.searchResults.size() > 0
}


Comments
Post new comment