Advanced I/O Functionの設定
次に、functionコンポーネントを設定してTo-Doリストアプリケーションのコーディングを開始します。
functionsディレクトリのfunctions/ToDoListには以下が含まれています:
- ToDoList.java メインfunctionファイル
- catalyst-config.json 設定ファイル
- lib ディレクトリ内のJavaライブラリファイル
- .classpath および .project 依存関係ファイル
ToDoList.java ファイルにコードを追加します。
Advanced I/O functionにおけるサーバーとData Store間のルーティングを処理する3つのAPIは以下のとおりです:
- GET /todo: Data Storeの_TodoItems_テーブルからTo-Doリストアイテムを取得します
- POST /todo: Data Storeに新しいリストアイテムを作成して保存します
- DELETE /todo: Data Storeからリストアイテムを削除します
次に、functionファイルにコードを追加しましょう。Javaコードをコピーして、プロジェクトのfunctions/TodoListディレクトリにあるToDoList.javaに貼り付け、ファイルを保存します。任意のIDEを使用してアプリケーションのファイルを操作できます。
Note: このセクションに記載されたコードを十分に理解するようにしてください。次のセクションでfunctionとクライアントのアーキテクチャについて説明します。
ToDoList.java
copy
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.InputStreamReader;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.catalyst.advanced.CatalystAdvancedIOHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zc.component.object.ZCObject;
import com.zc.component.object.ZCRowObject;
import com.zc.component.object.ZCTable;
import com.zc.component.zcql.ZCQL;
public class ToDoList implements CatalystAdvancedIOHandler {
private static final String GET = "GET";
private static final String POST = "POST";
private static final String DELETE = "DELETE";
private JSONObject responseData = new JSONObject();
private static final Logger LOGGER = Logger.getLogger(ToDoList.class.getName());
@SuppressWarnings("unchecked")
@Override
public void runner(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
String uri = request.getRequestURI();
String method = request.getMethod();
// GETメソッドはData StoreのTodoItemsテーブルからデータを取得します。
if (method.equals(GET) && uri.equals("/all")) {
Integer page = Integer.parseInt(request.getParameter("page"));
Integer perPage = Integer.parseInt(request.getParameter("perPage"));
ArrayList<ZCRowObject> rowList = ZCQL.getInstance().executeQuery("SELECT COUNT(ROWID) FROM TodoItems");
Integer totalTodos = Integer.parseInt(new ObjectMapper().writeValueAsString(rowList.get(0).get("TodoItems", "COUNT(ROWID)")));
Boolean hasMore = totalTodos > page * perPage;
ArrayList<HashMap<String, String>> todoItems = new ArrayList<>();
ZCQL.getInstance().executeQuery(String.format("SELECT ROWID, Notes FROM TodoItems LIMIT %d, %d",
(page - 1) * perPage, perPage)).forEach(row -> {
HashMap<String, String> item = new HashMap<>();
item.put("id", row.get("TodoItems", "ROWID").toString());
item.put("notes", row.get("TodoItems", "Notes").toString());
todoItems.add(item);
});
response.setStatus(200);
responseData.put("status", "success");
responseData.put("data", new JSONObject() {
{
put("hasMore", hasMore);
put("todoItems", todoItems);
}
});
// POSTメソッドはData StoreのTodoItemsテーブルに新しいToDoアイテムを追加します。
} else if (method.equals(POST) && uri.equals("/add")) {
JSONParser jsonParser = new JSONParser();
ServletInputStream requestBody = request.getInputStream();
JSONObject jsonObject = (JSONObject) jsonParser.parse(new InputStreamReader(requestBody, "UTF-8"));
String notes = jsonObject.get("notes").toString();
ZCRowObject row = ZCRowObject.getInstance();
row.set("Notes", notes);
ZCRowObject todoItem = ZCObject.getInstance().getTable("TodoItems").insertRow(row);
response.setStatus(200);
responseData.put("status", "success");
responseData.put("data", new JSONObject() {
{
put("todoItem", new JSONObject() {
{
put("id", todoItem.get("ROWID").toString());
put("notes", todoItem.get("Notes").toString());
}
});
}
});
// DELETEメソッドはIDを指定してData StoreからToDoアイテムを削除します。
} else if (method.equals(DELETE) && uri.matches("/\\d+")) {
ZCTable table = ZCObject.getInstance().getTable("TodoItems");
table.deleteRow(Long.parseLong(uri.substring(1)));
response.setStatus(200);
responseData.put("status", "success");
responseData.put("data", new JSONObject() {
{
put("todoItem", new JSONObject() {
{
put("id", uri.substring(1));
}
});
}
});
} else {
response.setStatus(404);
responseData.put("status", "failure");
responseData.put("message", "Please check if the URL trying to access is a correct one");
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Exception in Main", e);
responseData.put("status", "failure");
responseData.put("message", "We're unable to process the request.");
}
response.setContentType("application/json");
response.getWriter().write(responseData.toString());
}
}
functionsディレクトリの設定が完了しました。
最終更新日 2026-03-05 11:43:24 +0530 IST