<commit_before>/*
 * Copyright (C) 2016 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.opendatakit.database;

import android.content.ContentValues;
import android.database.sqlite.SQLiteException;
import android.os.ParcelUuid;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.Log;

import org.opendatakit.aggregate.odktables.rest.entity.RowFilterScope;
import org.opendatakit.common.android.data.ColumnList;
import org.opendatakit.common.android.data.OrderedColumns;
import org.opendatakit.common.android.data.TableDefinitionEntry;
import org.opendatakit.common.android.data.UserTable;
import org.opendatakit.common.android.exception.ActionNotAuthorizedException;
import org.opendatakit.common.android.exception.ServicesAvailabilityException;
import org.opendatakit.common.android.provider.DataTableColumns;
import org.opendatakit.database.service.BindArgs;
import org.opendatakit.database.service.KeyValueStoreEntry;
import org.opendatakit.database.service.OdkDbChunk;
import org.opendatakit.database.service.OdkDbHandle;
import org.opendatakit.database.service.OdkDbTable;
import org.opendatakit.database.service.OdkDbInterface;
import org.opendatakit.database.service.TableHealthInfo;
import org.opendatakit.database.service.queries.OdkDbAbstractQuery;
import org.opendatakit.database.service.queries.OdkDbResumableQuery;
import org.opendatakit.database.service.queries.OdkDbSimpleQuery;
import org.opendatakit.database.service.queries.QueryBounds;
import org.opendatakit.database.utilities.OdkDbChunkUtil;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class OdkDbSerializedInterface {

   private static final String TAG = OdkDbSerializedInterface.class.getSimpleName();

   private OdkDbInterface dbInterface;

   public OdkDbSerializedInterface(OdkDbInterface dbInterface) throws IllegalArgumentException {
      if (dbInterface == null) {
         throw new IllegalArgumentException("Database Interface must not be null");
      }

      this.dbInterface = dbInterface;
   }

   public OdkDbInterface getDbInterface() {
      return dbInterface;
   }

   public void setDbInterface(OdkDbInterface dbInterface) {
      if (dbInterface == null) {
         throw new IllegalArgumentException("Database Interface must not be null");
      }

      this.dbInterface = dbInterface;
   }

   private void rethrowNotAuthorizedRemoteException(Exception e)
       throws IllegalArgumentException, IllegalStateException, SQLiteException,
       ActionNotAuthorizedException, ServicesAvailabilityException {
      if ( (e instanceof IllegalStateException) || (e instanceof RemoteException) ) {
         String prefix = "via RemoteException on OdkDbInterface: ";
         String msg = e.getMessage();
         int idx = msg.indexOf(':');
         if (idx == -1) {
            throw new ServicesAvailabilityException(prefix + msg);
         }
         String exceptionName = msg.substring(0, idx);
         String message = msg.substring(idx + 2);
         if (!exceptionName.startsWith("org.opendatakit|")) {
            throw new ServicesAvailabilityException(prefix + msg);
         }
         exceptionName = exceptionName.substring(exceptionName.indexOf('|') + 1);
         if (exceptionName.equals(ActionNotAuthorizedException.class.getName())) {
            throw new ActionNotAuthorizedException(prefix + message);
         }
         if (exceptionName.equals(IllegalArgumentException.class.getName())) {
            throw new IllegalArgumentException(prefix + message);
         }
         if (exceptionName.equals(SQLiteException.class.getName())) {
            throw new SQLiteException(prefix + message);
         }
         // should only throw illegal argument exceptions.
         // anything else is not properly detected in the server layer
         throw new IllegalStateException(prefix + msg);
      } else {
         throw new IllegalStateException(
             "not IllegalStateException or RemoteException on OdkDbInterface: " +
             e.getClass().getName() + ": " + e.toString());
      }
   }


   private void rethrowAlwaysAllowedRemoteException(Exception e)
       throws IllegalArgumentException, IllegalStateException, SQLiteException,
       ServicesAvailabilityException {
      if ( (e instanceof IllegalStateException) || (e instanceof RemoteException) ) {
         String prefix = "via RemoteException on OdkDbInterface: ";
         String msg = e.getMessage();
         int idx = msg.indexOf(':');
         if (idx == -1) {
            throw new ServicesAvailabilityException(prefix + msg);
         }
         String exceptionName = msg.substring(0, idx);
         String message = msg.substring(idx + 2);
         if (!exceptionName.startsWith("org.opendatakit|")) {
            throw new ServicesAvailabilityException(prefix + msg);
         }
         exceptionName = exceptionName.substring(exceptionName.indexOf('|') + 1);
         if (exceptionName.equals(IllegalArgumentException.class.getName())) {
            throw new IllegalArgumentException(prefix + message);
         }
         if (exceptionName.equals(SQLiteException.class.getName())) {
            throw new SQLiteException(prefix + message);
         }
         // should only throw illegal argument exceptions.
         // anything else is not properly detected in the server layer<commit_msg>If service times out, the client-side exception will not have any message. Throw IllegalStateException back to caller.<commit_after>/*
 * Copyright (C) 2016 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.opendatakit.database;

import android.content.ContentValues;
import android.database.sqlite.SQLiteException;
import android.os.ParcelUuid;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.Log;

import org.opendatakit.aggregate.odktables.rest.entity.RowFilterScope;
import org.opendatakit.common.android.data.ColumnList;
import org.opendatakit.common.android.data.OrderedColumns;
import org.opendatakit.common.android.data.TableDefinitionEntry;
import org.opendatakit.common.android.data.UserTable;
import org.opendatakit.common.android.exception.ActionNotAuthorizedException;
import org.opendatakit.common.android.exception.ServicesAvailabilityException;
import org.opendatakit.common.android.provider.DataTableColumns;
import org.opendatakit.database.service.BindArgs;
import org.opendatakit.database.service.KeyValueStoreEntry;
import org.opendatakit.database.service.OdkDbChunk;
import org.opendatakit.database.service.OdkDbHandle;
import org.opendatakit.database.service.OdkDbTable;
import org.opendatakit.database.service.OdkDbInterface;
import org.opendatakit.database.service.TableHealthInfo;
import org.opendatakit.database.service.queries.OdkDbAbstractQuery;
import org.opendatakit.database.service.queries.OdkDbResumableQuery;
import org.opendatakit.database.service.queries.OdkDbSimpleQuery;
import org.opendatakit.database.service.queries.QueryBounds;
import org.opendatakit.database.utilities.OdkDbChunkUtil;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class OdkDbSerializedInterface {

   private static final String TAG = OdkDbSerializedInterface.class.getSimpleName();

   private OdkDbInterface dbInterface;

   public OdkDbSerializedInterface(OdkDbInterface dbInterface) throws IllegalArgumentException {
      if (dbInterface == null) {
         throw new IllegalArgumentException("Database Interface must not be null");
      }

      this.dbInterface = dbInterface;
   }

   public OdkDbInterface getDbInterface() {
      return dbInterface;
   }

   public void setDbInterface(OdkDbInterface dbInterface) {
      if (dbInterface == null) {
         throw new IllegalArgumentException("Database Interface must not be null");
      }

      this.dbInterface = dbInterface;
   }

   private void rethrowNotAuthorizedRemoteException(Exception e)
       throws IllegalArgumentException, IllegalStateException, SQLiteException,
       ActionNotAuthorizedException, ServicesAvailabilityException {
      if ( (e instanceof IllegalStateException) || (e instanceof RemoteException) ) {
         String prefix = "via RemoteException on OdkDbInterface: ";
         String msg = e.getMessage();
         int idx = msg.indexOf(':');
         if (idx == -1) {
            throw new ServicesAvailabilityException(prefix + msg);
         }
         String exceptionName = msg.substring(0, idx);
         String message = msg.substring(idx + 2);
         if (!exceptionName.startsWith("org.opendatakit|")) {
            throw new ServicesAvailabilityException(prefix + msg);
         }
         exceptionName = exceptionName.substring(exceptionName.indexOf('|') + 1);
         if (exceptionName.equals(ActionNotAuthorizedException.class.getName())) {
            throw new ActionNotAuthorizedException(prefix + message);
         }
         if (exceptionName.equals(IllegalArgumentException.class.getName())) {
            throw new IllegalArgumentException(prefix + message);
         }
         if (exceptionName.equals(SQLiteException.class.getName())) {
            throw new SQLiteException(prefix + message);
         }
         // should only throw illegal argument exceptions.
         // anything else is not properly detected in the server layer
         throw new IllegalStateException(prefix + msg);
      } else {
         throw new IllegalStateException(
             "not IllegalStateException or RemoteException on OdkDbInterface: " +
             e.getClass().getName() + ": " + e.toString());
      }
   }


   private void rethrowAlwaysAllowedRemoteException(Exception e)
       throws IllegalArgumentException, IllegalStateException, SQLiteException,
       ServicesAvailabilityException {
      if ( (e instanceof IllegalStateException) || (e instanceof RemoteException) ) {
         String prefix = "via RemoteException on OdkDbInterface: ";
         String msg = e.getMessage();
         if ( msg == null ) {
            throw new IllegalStateException(prefix + e.toString());
         }
         int idx = msg.indexOf(':');
         if (idx == -1) {
            throw new ServicesAvailabilityException(prefix + msg);
         }
         String exceptionName = msg.substring(0, idx);
         String message = msg.substring(idx + 2);
         if (!exceptionName.startsWith("org.opendatakit|")) {
            throw new ServicesAvailabilityException(prefix + msg);
         }
         exceptionName = exceptionName.substring(exceptionName.indexOf('|') + 1);
         if (exceptionName.equals(IllegalArgumentException.class.getName())) {
            throw new IllegalArgumentException(prefix + message);
         }
         if (exceptionName.equals(SQLiteException.class.getName())) {
            throw new SQLiteException(prefix + message);
         }
         // should only throw illegal argument exceptions.
         // anything else is not properly detected in the server layer
{'fraction_non_alphanumeric': 0.08765964504893017, 'fraction_numerical': 0.0026538397744236193, 'mean_word_length': 3.5712661106899164, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6747946', 'n_tokens_mistral': 3145, 'n_tokens_neox': 3090, 'n_words': 782}