throws Exception {
logger.info("Preparing worksheet :" + kind);
// Search Spreadsheet
FeedURLFactory urlFactory = FeedURLFactory.getDefault();
SpreadsheetQuery spreadsheetQuery =
new SpreadsheetQuery(urlFactory.getSpreadsheetsFeedUrl());
SpreadsheetFeed spreadsheetFeed = ss.query(spreadsheetQuery, SpreadsheetFeed.class);
SpreadsheetEntry spreadsheetEntry = null;
for (SpreadsheetEntry entry : spreadsheetFeed.getEntries()) {
if (ssKey.equals(entry.getKey())) {
spreadsheetEntry = entry;
logger.info("Spreadsheet:" + ssKey + " is found.");
break;
}
}
if (spreadsheetEntry == null) {
throw new RuntimeException("Cannot find spreadsheet:" + ssKey);
}
// Modify a worksheet's column size
URL worksheetFeedUrl = spreadsheetEntry.getWorksheetFeedUrl();
WorksheetFeed worksheetFeed = ss.getFeed(worksheetFeedUrl, WorksheetFeed.class);
WorksheetEntry worksheetEntry = null;
for (WorksheetEntry worksheet : worksheetFeed.getEntries()) {
if (kind.equals(worksheet.getTitle().getPlainText())) {
worksheetEntry = worksheet;
break;
}
}
if (worksheetEntry == null) {
throw new RuntimeException("Cannot find worksheet:" + kind);
}
worksheetEntry.setColCount(properties.size() + 1);
worksheetEntry.update();
logger.info("Worksheet:" + kind + "'s column-size is set to " + (properties.size() + 1));
FeedURLFactory factory = FeedURLFactory.getDefault();
URL tableFeedUrl = factory.getTableFeedUrl(ssKey);
// Check if already exists.
TableEntry tableEntry = null;
TableFeed feed = ss.getFeed(tableFeedUrl, TableFeed.class);
for (TableEntry entry : feed.getEntries()) {
if (entry.getTitle().getPlainText().equals(kind)) {
tableEntry = entry;
logger.info("TableFeed:" + kind + " already exists.");
break;
}
}
// Add TableFeed
if (tableEntry == null) {
tableEntry = new TableEntry();
tableEntry.setTitle(new PlainTextConstruct(kind));
tableEntry.setWorksheet(new Worksheet(kind));
tableEntry.setHeader(new Header(1));
Data tableData = new Data();
tableData.setNumberOfRows(0);
tableData.setStartIndex(2);
tableData.setInsertionMode(InsertionMode.INSERT);
// Create a title row
tableData.addColumn(new Column("A", Entity.KEY_RESERVED_PROPERTY));
for (int i = 0; i < properties.size(); i++) {
String index = number2columnName(i + 1);
String columnName = properties.get(i).getName();
tableData.addColumn(new Column(index, columnName));
}
tableEntry.setData(tableData);
tableEntry = ss.insert(tableFeedUrl, tableEntry);
logger.info("Craeted tableFeed:" + kind);
}
String[] split = tableEntry.getId().split("/");
final String tableId = split[split.length - 1];
// Add a "valueType" row (the cells are filled with "*" to be replaced)
int numberOfRows = tableEntry.getData().getNumberOfRows();
if (numberOfRows == 0) {
RecordEntry newEntry = new RecordEntry();
newEntry.addField(new Field(null, Entity.KEY_RESERVED_PROPERTY, VALUE_TYPE));
for (int i = 0; i < properties.size(); i++) {
GbProperty gbProperty = properties.get(i);
String columnName = gbProperty.getName();
newEntry.addField(new Field(null, columnName, VALUE_TYPE_NOT_SET));
}
URL recordFeedUrl = factory.getRecordFeedUrl(ssKey, tableId);
ss.insert(recordFeedUrl, newEntry);
logger.info("Inserted TypeValue row in :" + kind);
}
return tableId;
}