+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.dao.aaf.cass;\r
-\r
-import java.nio.ByteBuffer;\r
-import java.util.Date;\r
-import java.util.List;\r
-import java.util.UUID;\r
-\r
-import org.onap.aaf.authz.env.AuthzTrans;\r
-import org.onap.aaf.authz.layer.Result;\r
-import org.onap.aaf.dao.CassDAOImpl;\r
-import org.onap.aaf.dao.DAOException;\r
-import org.onap.aaf.dao.Loader;\r
-\r
-import com.datastax.driver.core.Cluster;\r
-import com.datastax.driver.core.ResultSet;\r
-import com.datastax.driver.core.Row;\r
-\r
-/**\r
- * FutureDAO stores Construction information to create \r
- * elements at another time.\r
- * \r
- * 8/20/2013\r
- */\r
-public class FutureDAO extends CassDAOImpl<AuthzTrans,FutureDAO.Data> {\r
- private static final String TABLE = "future";\r
- private final HistoryDAO historyDAO;\r
-// private static String createString;\r
- private PSInfo psByStartAndTarget;\r
- \r
- public FutureDAO(AuthzTrans trans, Cluster cluster, String keyspace) {\r
- super(trans, FutureDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));\r
- historyDAO = new HistoryDAO(trans, this);\r
- init(trans);\r
- }\r
-\r
- public FutureDAO(AuthzTrans trans, HistoryDAO hDAO) {\r
- super(trans, FutureDAO.class.getSimpleName(),hDAO, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));\r
- historyDAO=hDAO;\r
- init(trans);\r
- }\r
-\r
- public static final int KEYLIMIT = 1;\r
- public static class Data {\r
- public UUID id;\r
- public String target;\r
- public String memo;\r
- public Date start;\r
- public Date expires;\r
- public ByteBuffer construct; // this is a blob in cassandra\r
- }\r
-\r
- private static class FLoader extends Loader<Data> {\r
- public FLoader() {\r
- super(KEYLIMIT);\r
- }\r
-\r
- public FLoader(int keylimit) {\r
- super(keylimit);\r
- }\r
-\r
- @Override\r
- public Data load(Data data, Row row) {\r
- data.id = row.getUUID(0);\r
- data.target = row.getString(1);\r
- data.memo = row.getString(2);\r
- data.start = row.getDate(3);\r
- data.expires = row.getDate(4);\r
- data.construct = row.getBytes(5);\r
- return data;\r
- }\r
-\r
- @Override\r
- protected void key(Data data, int idx, Object[] obj) {\r
- obj[idx] = data.id;\r
- }\r
-\r
- @Override\r
- protected void body(Data data, int _idx, Object[] obj) {\r
- int idx = _idx;\r
-\r
- obj[idx] = data.target;\r
- obj[++idx] = data.memo;\r
- obj[++idx] = data.start;\r
- obj[++idx] = data.expires;\r
- obj[++idx] = data.construct;\r
- }\r
- }\r
-\r
- private void init(AuthzTrans trans) {\r
- // Set up sub-DAOs\r
- String[] helpers = setCRUD(trans, TABLE, Data.class, new FLoader(KEYLIMIT));\r
-\r
- // Uh, oh. Can't use "now()" in Prepared Statements (at least at this level)\r
-// createString = "INSERT INTO " + TABLE + " ("+helpers[FIELD_COMMAS] +") VALUES (now(),";\r
-//\r
-// // Need a specialty Creator to handle the "now()"\r
-// replace(CRUD.Create, new PSInfo(trans, "INSERT INTO future (" + helpers[FIELD_COMMAS] +\r
-// ") VALUES(now(),?,?,?,?,?)",new FLoader(0)));\r
- \r
- // Other SELECT style statements... match with a local Method\r
- psByStartAndTarget = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] +\r
- " FROM future WHERE start <= ? and target = ? ALLOW FILTERING", new FLoader(2) {\r
- @Override\r
- protected void key(Data data, int _idx, Object[] obj) {\r
- int idx = _idx;\r
-\r
- obj[idx]=data.start;\r
- obj[++idx]=data.target;\r
- }\r
- },readConsistency);\r
- \r
-\r
- }\r
-\r
- public Result<List<Data>> readByStartAndTarget(AuthzTrans trans, Date start, String target) throws DAOException {\r
- return psByStartAndTarget.read(trans, R_TEXT, new Object[]{start, target});\r
- }\r
-\r
- /**\r
- * Override create to add secondary ID to Subject in History, and create Data.ID, if it is null\r
- */\r
- public Result<FutureDAO.Data> create(AuthzTrans trans, FutureDAO.Data data, String id) {\r
- // If ID is not set (typical), create one.\r
- if(data.id==null) {\r
- StringBuilder sb = new StringBuilder(trans.user());\r
- sb.append(data.target);\r
- sb.append(System.currentTimeMillis());\r
- data.id = UUID.nameUUIDFromBytes(sb.toString().getBytes());\r
- }\r
- Result<ResultSet> rs = createPS.exec(trans, C_TEXT, data);\r
- if(rs.notOK()) {\r
- return Result.err(rs);\r
- }\r
- wasModified(trans, CRUD.create, data, null, id);\r
- return Result.ok(data); \r
- }\r
-\r
- /**\r
- * Log Modification statements to History\r
- *\r
- * @param modified which CRUD action was done\r
- * @param data entity data that needs a log entry\r
- * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data\r
- */\r
- @Override\r
- protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {\r
- boolean memo = override.length>0 && override[0]!=null;\r
- boolean subject = override.length>1 && override[1]!=null;\r
- HistoryDAO.Data hd = HistoryDAO.newInitedData();\r
- hd.user = trans.user();\r
- hd.action = modified.name();\r
- hd.target = TABLE;\r
- hd.subject = subject?override[1]:"";\r
- hd.memo = memo?String.format("%s by %s", override[0], hd.user):data.memo;\r
- \r
- if(historyDAO.create(trans, hd).status!=Status.OK) {\r
- trans.error().log("Cannot log to History");\r
- }\r
- }\r
- \r
-}\r