/*
* Author: Bo Maryniuk <bo@suse.de>
*
* Copyright (c) 2013 Bo Maryniuk. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY BO MARYNIUK "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.spreadbible.ctlib;
import com.spreadbible.ctlib.entities.CDBPerson;
import com.spreadbible.ctlib.entities.CHKDocument;
import com.spreadbible.ctlib.entities.ServiceEntitiesUtil;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Check-in Service
*
* @author bo
*/
public class CheckInService {
public void initDatabase() throws Exception {
HashMap params = new HashMap();
params.put("database", CTLib.getMapper().getDatabaseName());
params.put("doctable", "OT_CHKN_DOCUMENTS");
params.put("maptable", "OT_CHKN_DOC");
ResultSet result = CTLib.getMapper().call("checkin.chk-init-check", params);
if (!result.next() || result.getInt("tbl") == 0) {
CTLib.getMapper().close(result);
CTLib.getMapper().close(CTLib.getMapper().call("checkin.chk-init-db", null));
}
}
/**
* Get check-in documents.
*
* @return
*/
public List<CHKDocument> getDocuments() throws Exception {
List<CHKDocument> documents = new ArrayList<CHKDocument>();
ResultSet result = CTLib.getMapper().call("checkin.chk-get-documents", null);
while (result.next()) {
CHKDocument doc = new CHKDocument();
doc.setId(result.getInt("id"));
doc.setTitle(result.getString("title"));
doc.setDescription(result.getString("description"));
doc.setArchived(result.getDate("archived"));
doc.setCreated(result.getDate("created"));
documents.add(doc);
}
return documents;
}
/**
* Fill-in the document with checked-in attendees.
*
* @param document
* @return
*/
public CHKDocument fillDocument(CHKDocument document) throws Exception {
HashMap params = new HashMap();
params.put("docid", document.getId());
ResultSet result = CTLib.getMapper().call("checkin.chk-get-related-people", params);
while (result.next()) {
document.pushPerson(ServiceEntitiesUtil.obtainCDBPerson(result)).popNewPerson();
}
return document;
}
/**
* Save the document to the database.
*
* @param document
*/
public void setDocument(CHKDocument document) throws Exception {
HashMap params = new HashMap();
params.put("docid", document.getId());
params.put("title", document.getTitle());
params.put("description", document.getDescription());
params.put("hashcode", document.hashCode());
String op = "update";
if (document.getId() == null) {
op = "new";
} else if (document.isDeleted()) {
op = "delete";
} else if (document.getArchived() != null) {
op = "archive";
}
CTLib.getMapper().call(String.format("checkin.chk-%s-document", op), params);
// Get ID
if (op.equals("new")) {
// get by hashcode just created above
ResultSet result = CTLib.getMapper().call("checkin.chk-pick-document", params);
if (result.next()) {
document.setId(result.getInt("id"));
} else {
throw new Exception("Failed to initialize new document.");
}
}
this.fillDocument(document);
}
/**
* Add the person to the document.
* @param person
*/
public void addPerson(CHKDocument document) throws Exception {
CDBPerson person = document.popNewPerson();
if (person != null) {
HashMap params = new HashMap();
params.put("docid", document.getId());
params.put("uid", person.getId());
CTLib.getMapper().call("checkin.chk-add-person", params);
} else {
System.err.println("Person is null!!");
}
}
}