MarketListingpublic class MarketListing extends Object A utility class for creating and verifying marketplace listings.
For details visit http://wiki.developers.facebook.com/index.php/Marketplace_Listing_Attributes |
Fields Summary |
---|
public static final String | CONDITION_ANYSpecifies a condition of 'any' | public static final String | CONDITION_NEWSpecifies a condition of 'new' | public static final String | CONDITION_USEDSpecified a condition of 'used' | protected static final String | CATEGORY_ATTRIB | protected static final String | SUBCATEGORY_ATTRIB | protected static final String | TITLE_ATTRIB | protected static final String | DESCRIPTION_ATTRIB | protected JSONObject | attribs |
Constructors Summary |
---|
private MarketListing()
//empty constructor not allowed
| public MarketListing(String title, String description, MarketListingCategory category, MarketListingSubcategory subcategory)Constructor.
this.attribs = new JSONObject();
this.setAttribute(TITLE_ATTRIB, title);
this.setAttribute(DESCRIPTION_ATTRIB, description);
this.setAttribute(CATEGORY_ATTRIB, category.getName());
this.setAttribute(SUBCATEGORY_ATTRIB, subcategory.getName());
| MarketListing(String json)
try {
this.attribs = new JSONObject(json);
}
catch (Exception e) {
this.attribs = new JSONObject();
}
|
Methods Summary |
---|
private boolean | checkString(java.lang.String attrName)
String input = this.getAttribute(attrName);
if ((input == null) || ("".equals(input))) {
return false;
}
return true;
| java.lang.String | getAttribs()
return attribs.toString();
| public java.lang.String | getAttribute(java.lang.String name)Retrieve the value of the specified attribute.
String result = null;
try {
result = (String)attribs.get(name);
}
catch (Exception ignored) {
//do nothing
}
return result;
| public java.lang.String | getCategory()
return this.getAttribute(CATEGORY_ATTRIB);
| public java.lang.String | getDescription()
return this.getAttribute(DESCRIPTION_ATTRIB);
| public java.lang.String | getSubCategory()
return this.getAttribute(SUBCATEGORY_ATTRIB);
| public java.lang.String | getTitle()
return this.getAttribute(TITLE_ATTRIB);
| public void | removeAttribute(java.lang.CharSequence attr)Remove an attribute from this listing.
this.attribs.remove(attr.toString());
| public void | setAttribute(java.lang.String name, java.lang.String value)Set an attribute for this listing. Attributes are used to specify optional listing information (for
example, 'price', 'isbn', 'condition', etc.). The specific attributes required by Facebook vary
depending upon the category of listing being posted. For more details, visit:
http://wiki.developers.facebook.com/index.php/Marketplace_Listing_Attributes
try {
attribs.put(name, value);
}
catch (Exception e) {
System.out.println("Exception when setting listing attribute!");
e.printStackTrace();
}
| public void | setCategory(java.lang.String category)Set the category of the listing
this.setAttribute(CATEGORY_ATTRIB, category);
| public void | setDescription(java.lang.String description)Set the listing description.
this.setAttribute(DESCRIPTION_ATTRIB, description);
| public void | setSubCategory(java.lang.String subCategory)Set the subcategory of the listing
this.setAttribute(SUBCATEGORY_ATTRIB, subCategory);
| public void | setTitle(java.lang.String title)Set the listing title.
this.setAttribute(TITLE_ATTRIB, title);
| boolean | verify()
if (!checkString(TITLE_ATTRIB)) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'title' attribute may not be null or empty!");
}
if (!checkString(DESCRIPTION_ATTRIB)) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'description' attribute may not be null or empty!");
}
if (!checkString(CATEGORY_ATTRIB)) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'category' attribute may not be null or empty!");
}
if (!checkString(SUBCATEGORY_ATTRIB)) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'subcategory' attribute may not be null or empty!");
}
//XXX: uncomment to force strict validation (requires all attributes mentioned in the Facebook docs)
/*
String category = this.getAttribute(CATEGORY_ATTRIB);
String subcat = this.getAttribute(SUBCATEGORY_ATTRIB);
if (category.equals(MarketListingCategory.FORSALE.getName())) {
if (!checkString("price")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'price' attribute is required when selling an item!");
}
if ((subcat.equals(MarketListingSubcategory.ELECTRONICS)) || (subcat.equals(MarketListingSubcategory.FURNITURE))
|| (subcat.equals(MarketListingSubcategory.AUTOS)) || (subcat.equals(MarketListingSubcategory.BOOKS))) {
if (!checkString("condition")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'condition' attribute is required whenever selling books, electronics, cars, or furniture!");
}
}
if (subcat.equals(MarketListingSubcategory.BOOKS)) {
if ((!checkString("isbn")) || (this.getAttribute("isbn").length() != 13)) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'isbn' attribute is required when selling a book, and it must be exactly 13 digits long!");
}
}
}
if ((category.equals(MarketListingCategory.HOUSING)) || (category.equals(MarketListingCategory.HOUSING_WANTED))) {
//num_beds, num_baths, dogs, cats, smoking, square_footage, street, crossstreet, postal
if (! checkString("num_beds")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'num_beds' attribute is required for all housing listings!");
}
if (! checkString("num_baths")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'num_baths' attribute is required for all housing listings!");
}
if (! checkString("dogs")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'dogs' attribute is required for all housing listings!");
}
if (! checkString("cats")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'cats' attribute is required for all housing listings!");
}
if (! checkString("smoking")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'smoking' attribute is required for all housing listings!");
}
if (! checkString("square_footage")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'square_footage' attribute is required for all housing listings!");
}
if (! checkString("street")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'street' attribute is required for all housing listings!");
}
if (! checkString("crossstreet")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'crossstreet' attribute is required for all housing listings!");
}
if (! checkString("postal")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'postal' attribute is required for all housing listings!");
}
if ((subcat.equals(MarketListingSubcategory.SUBLETS)) || (subcat.equals(MarketListingSubcategory.RENTALS))) {
if (!checkString("rent")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'rent' attribute is required for all rentals and sublets!");
}
}
if (subcat.equals(MarketListingSubcategory.REAL_ESTATE)) {
if (!checkString("price")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'price' attribute is required for all real-estate listings!");
}
}
}
if (category.equals(MarketListingCategory.JOBS)) {
//pay, full, intern, summer, nonprofit, pay_type
if (!checkString("pay")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'pay' attribute is required for all job postings!");
}
if (!checkString("full")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'full' attribute is required for all job postings!");
}
if (!checkString("intern")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'intern' attribute is required for all job postings!");
}
if (!checkString("summer")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'summer' attribute is required for all job postings!");
}
if (!checkString("nonprofit")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'nonprofit' attribute is required for all job postings!");
}
if (!checkString("pay_type")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'pay_type' attribute is required for all job postings!");
}
}
if (category.equals(MarketListingCategory.FORSALE_WANTED)) {
if ((subcat.equals(MarketListingSubcategory.BOOKS_WANTED)) || (subcat.equals(MarketListingSubcategory.FURNITURE_WANTED))
|| (subcat.equals(MarketListingSubcategory.AUTOS_WANTED)) || (subcat.equals(MarketListingSubcategory.ELECTRONICS_WANTED))) {
if (!checkString("condition")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'condition' attribute is required whenever seeking books, burniture, electronics, or cars!");
}
if (subcat.equals(MarketListingSubcategory.ELECTRONICS_WANTED)) {
if (!checkString("isbn")) {
throw new FacebookException(ErrorCode.GEN_INVALID_PARAMETER, "The 'isbn' attribute is required when requesting a book!");
}
}
}
}
*/
return true;
|
|