Commit 043807a8 by Kunj

Added offline functionality for complete app.

parents 2d8285a1 b7a69aa5
Showing with 1177 additions and 235 deletions
...@@ -30,11 +30,11 @@ android { ...@@ -30,11 +30,11 @@ android {
buildToolsVersion "24.0.1" buildToolsVersion "24.0.1"
defaultConfig { defaultConfig {
applicationId "com.vsoft.uoflservicenow" applicationId "com.vsoft.servicenow"
minSdkVersion 9 minSdkVersion 9
targetSdkVersion 24 targetSdkVersion 24
versionCode 1 versionCode 1
versionName "0.0.20" versionName "0.0.29"
multiDexEnabled true multiDexEnabled true
} }
buildTypes { buildTypes {
...@@ -50,6 +50,15 @@ android { ...@@ -50,6 +50,15 @@ android {
signingConfig signingConfigs.config signingConfig signingConfigs.config
} }
} }
productFlavors {
uofl{
applicationId "com.vsoft.servicenow.uofl"
}
citrix {
applicationId "com.vsoft.servicenow.citrix"
}
}
} }
dependencies { dependencies {
......
package com.vsoft.servicenow;
/**
* @author Kunj on 05/01/17.
*/
public class AppConfig {
public static final String APP_INTERNAL_NAME = "Citrix";
public static final String DOMAIN_PRODUCTION = "https://ven01199.service-now.com/";
public static final String DOMAIN_TEST = "https://ven01199.service-now.com/";
public static final String LOGIN_CLIENT_ID_PRODUCTION = "ac0dd3408c1031006907010c2cc6ef6d";
public static final String LOGIN_CLIENT_SECRET_PRODUCTION = "oklj6znxv3o9jmyn2mlp";
public static final String LOGIN_CLIENT_ID_TEST = "ac0dd3408c1031006907010c2cc6ef6d";
public static final String LOGIN_CLIENT_SECRET_TEST = "dh85bcz3vx4dvgqys610";
/**
* Web services urls
*/
/*Catalogue Category API */
public static final String URL_GET_CATALOGUE = "api/vsng2/app_apis/category";
/*Catalogue Category Items API */
public static final String URL_GET_CATALOGUE_ITEM = "api/vsng2/app_apis/catalog_item";
/*Variable form API */
public static final String URL_GET_VARIABLE = "/api/vsng2/app_apis/variables";
public static final String URL_GET_UI_POLICY = "/api/vsng2/app_apis/uipolicy";
public static final String URL_GET_VARIABLE_CHOICE = "/api/vsng2/app_apis/question_choice";
public static final String URL_POST_CATALOGUE_ITEM = "api/vsng2/app_apis";
}
package com.vsoft.servicenow.ui;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.analytics.Tracker;
import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.servicenow.R;
import com.vsoft.servicenow.utils.PrefManager;
import com.vsoft.servicenow.utils.Util;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* Created by Kunj on 11/8/16.
*/
public class HomeScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
ButterKnife.bind(this);
CatalogueApplication application = (CatalogueApplication) getApplication();
Tracker tracker = application.getDefaultTracker();
// Send initial screen view hit.
Util.sendScreenName(tracker, getString(R.string.home_screen_string));
}
@OnClick(R.id.home_screen_order_service)
void orderServiceClicked() {
startActivity(new Intent(HomeScreen.this, CatalogueScreen.class));
}
@OnClick(R.id.home_screen_my_incident)
void myIncidentClicked() {
startActivity(new Intent(HomeScreen.this, MyIncidentScreen.class));
}
@OnClick(R.id.home_screen_my_request)
void myRequestClicked() {
startActivity(new Intent(HomeScreen.this, MyRequestActivity.class));
}
@OnClick(R.id.home_screen_report_incident)
void reportIncidentClicked() {
startActivity(new Intent(HomeScreen.this, ReportIncidentScreen.class));
}
@OnClick(R.id.home_screen_logout_image_view)
void logoutOnClicked() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.home_screen_logout_confirmation_msg_string)
.setCancelable(false)
.setPositiveButton(R.string.ok_string, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_ACCESS_TOKEN, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_REFRESH_TOKEN, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_LAST_NAME, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_SYS_ID, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_FIRST_NAME, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_ID, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_FULL_NAME, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_EMAIL_ID, "");
Intent loginIntent = new Intent(HomeScreen.this, LoginScreen.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5dp" />
<solid
android:color="@color/back_button_bg_color"/>
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/login_screen_login_button_background_color" />
<corners android:radius="10dp" />
</shape>
\ No newline at end of file
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5dp" />
<solid
android:color="@color/login_screen_login_button_background_color"/>
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/login_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbarStyle="insideOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/login_screen_top_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/login_screen_top_image" />
<LinearLayout
android:layout_below="@id/login_screen_top_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/login_screen_username_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/extra_large_margin"
android:layout_marginRight="@dimen/extra_large_margin"
android:background="@drawable/username_under_bg_box"
android:drawableLeft="@mipmap/ic_user_icon"
android:hint="@string/username_string"
android:lines="1"
android:padding="@dimen/normal_margin"
android:singleLine="true" />
<EditText
android:id="@+id/login_screen_password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/extra_large_margin"
android:layout_marginRight="@dimen/extra_large_margin"
android:background="@drawable/username_under_bg_box"
android:drawableLeft="@mipmap/ic_password_icon"
android:hint="@string/password_string"
android:inputType="textPassword"
android:lines="1"
android:padding="@dimen/normal_margin"
android:singleLine="true" />
<TextView
android:id="@+id/login_screen_login_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/large_margin"
android:layout_marginLeft="@dimen/extra_large_margin"
android:layout_marginRight="@dimen/extra_large_margin"
android:layout_marginTop="@dimen/normal_margin"
android:background="@drawable/login_bg"
android:gravity="center"
android:paddingBottom="@dimen/normal_margin"
android:paddingTop="@dimen/normal_margin"
android:text="@string/login_screen_login_string"
android:textColor="@android:color/white"
android:textSize="@dimen/large_text_size" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:background="@color/login_screen_bottom_color"/>
</RelativeLayout>
</ScrollView>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
style = "@style/LightBackgroundStyle"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="@color/tool_bar_title_color"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/small_margin"
android:layout_marginRight="@dimen/small_margin">
<ImageView
android:id="@+id/nav_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/home_icon" />
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="@string/app_name"
android:textColor="@android:color/white" />
<ImageView
android:id="@+id/home_screen_logout_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/logout_icon" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="@dimen/extra_large_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/home_screen_report_incident"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/report_incident"/>
<ImageView
android:id="@+id/home_screen_order_service"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/order_services"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="@dimen/extra_large_margin">
<ImageView
android:id="@+id/home_screen_my_incident"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/my_incidents"/>
<ImageView
android:id="@+id/home_screen_my_request"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/my_requests"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
style="@style/LightBackgroundStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="@drawable/splash_citrix_text_image"/>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tool_bar_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:minHeight="?attr/actionBarSize"
android:padding="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:theme="@style/ToolBarStyle"
style="@style/BlackBackgroundStyle"/>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="home_screen_icon_array">
<item>@drawable/report_incident</item>
<item>@drawable/order_services</item>
<item>@drawable/my_incidents</item>
<item>@drawable/my_requests</item>
</array>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="login_screen_login_button_background_color">#187cba</color>
<color name="login_screen_bottom_color">#ff9d1c</color>
<color name="screen_bg_color">#393e4f</color>
<color name="back_button_bg_color">#e01d24</color>
</resources>
<resources>
<string name="app_name">Citrix ServiceNow</string>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>
</style>
<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<!-- Required for pre-Lollipop. -->
<item name="overlapAnchor">false</item>
<!-- Required for Lollipop. -->
<item name="android:overlapAnchor">false</item>
</style>
<style name="BlackBackgroundStyle" parent="@style/Theme.AppCompat">
<item name="android:background">@android:color/black</item>
</style>
<!-- ToolBar -->
<style name="ToolBarStyle" parent="Theme.AppCompat">
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
<item name="actionMenuTextColor">@android:color/white</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Replace placeholder ID with your tracking ID -->
<string name="ga_trackingId">UA-83545030-1</string>
<!-- Enable automatic activity tracking -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- Enable automatic exception tracking -->
<bool name="ga_reportUncaughtExceptions">true</bool>
</resources>
\ No newline at end of file
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vsoft.uoflservicenow"> package="com.vsoft.servicenow">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<supports-screens <supports-screens
android:anyDensity="true" android:anyDensity="true"
android:largeScreens="true" android:largeScreens="true"
...@@ -43,14 +45,14 @@ ...@@ -43,14 +45,14 @@
android:screenSize="xlarge" /> android:screenSize="xlarge" />
</compatible-screens> </compatible-screens>
<application <application
android:name="com.vsoft.uoflservicenow.CatalogueApplication" android:name="com.vsoft.servicenow.CatalogueApplication"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity <activity
android:name="com.vsoft.uoflservicenow.ui.LoginScreen" android:name="com.vsoft.servicenow.ui.SplashScreen"
android:label="@string/app_name" android:label="@string/app_name"
android:screenOrientation="portrait" android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateHidden"> android:windowSoftInputMode="adjustResize|stateHidden">
...@@ -60,27 +62,31 @@ ...@@ -60,27 +62,31 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity <activity
android:name="com.vsoft.uoflservicenow.ui.HomeScreen" android:name="com.vsoft.servicenow.ui.LoginScreen"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateHidden"/>
<activity
android:name="com.vsoft.servicenow.ui.HomeScreen"
android:screenOrientation="portrait" /> android:screenOrientation="portrait" />
<activity <activity
android:name="com.vsoft.uoflservicenow.ui.CatalogueScreen" android:name="com.vsoft.servicenow.ui.CatalogueScreen"
android:screenOrientation="portrait" /> android:screenOrientation="portrait" />
<activity <activity
android:name="com.vsoft.uoflservicenow.ui.CatalogueItemScreen" android:name="com.vsoft.servicenow.ui.CatalogueItemScreen"
android:screenOrientation="portrait" /> android:screenOrientation="portrait" />
<activity <activity
android:name="com.vsoft.uoflservicenow.ui.CatalogueVariableScreen" android:name="com.vsoft.servicenow.ui.CatalogueVariableScreen"
android:screenOrientation="portrait" android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize" /> android:windowSoftInputMode="stateHidden|adjustResize" />
<activity <activity
android:name="com.vsoft.uoflservicenow.ui.ReportIncidentScreen" android:name="com.vsoft.servicenow.ui.ReportIncidentScreen"
android:screenOrientation="portrait" android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize"/> android:windowSoftInputMode="stateHidden|adjustResize"/>
<activity <activity
android:name="com.vsoft.uoflservicenow.ui.MyRequestActivity" android:name="com.vsoft.servicenow.ui.MyRequestActivity"
android:screenOrientation="portrait" /> android:screenOrientation="portrait" />
<activity <activity
android:name="com.vsoft.uoflservicenow.ui.MyIncidentScreen" android:name="com.vsoft.servicenow.ui.MyIncidentScreen"
android:screenOrientation="portrait"/> android:screenOrientation="portrait"/>
<service android:name=".service.SyncService" /> <service android:name=".service.SyncService" />
......
package com.vsoft.uoflservicenow; package com.vsoft.servicenow;
import android.app.Application; import android.app.Application;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
...@@ -10,13 +10,16 @@ import android.net.ConnectivityManager; ...@@ -10,13 +10,16 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import com.crashlytics.android.Crashlytics; import com.crashlytics.android.Crashlytics;
import com.google.android.gms.analytics.GoogleAnalytics; import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker; import com.google.android.gms.analytics.Tracker;
import com.vsoft.uoflservicenow.db.DBManager; import com.vsoft.servicenow.db.DBManager;
import com.vsoft.uoflservicenow.service.SyncService; import com.vsoft.servicenow.service.SyncService;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.ui.LoginScreen;
import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.utils.PrefManager;
import io.fabric.sdk.android.Fabric; import io.fabric.sdk.android.Fabric;
...@@ -27,7 +30,7 @@ public class CatalogueApplication extends Application { ...@@ -27,7 +30,7 @@ public class CatalogueApplication extends Application {
private static Context mContext; private static Context mContext;
private Tracker mTracker; private Tracker mTracker;
private BroadcastReceiver mSyncBroadCastReceiver = new BroadcastReceiver() { private BroadcastReceiver mSyncBroadCastSyncReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getStringExtra(Constants.APPLICATION_BROADCAST_DATA_ACTION); String action = intent.getStringExtra(Constants.APPLICATION_BROADCAST_DATA_ACTION);
...@@ -40,13 +43,37 @@ public class CatalogueApplication extends Application { ...@@ -40,13 +43,37 @@ public class CatalogueApplication extends Application {
} }
}; };
private BroadcastReceiver mSyncBroadCastLoginReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getStringExtra(Constants.APPLICATION_BROADCAST_DATA_ACTION);
Log.d(Constants.TAG, "CatalogueApplication: onReceive: action: "+action);
if(Constants.ACTION_PROMPT_LOGIN.equals(action)) {
Toast.makeText(CatalogueApplication.this, R.string.prompt_relogin_login_expired, Toast.LENGTH_SHORT).show();
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_ACCESS_TOKEN, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_REFRESH_TOKEN, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_LAST_NAME, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_SYS_ID, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_FIRST_NAME, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_USER_ID, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_USER_FULL_NAME, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_USER_EMAIL_ID, "");
Intent loginIntent = new Intent(CatalogueApplication.this, LoginScreen.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
}
}
};
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
Fabric.with(this, new Crashlytics()); Fabric.with(this, new Crashlytics());
mContext = getApplicationContext(); mContext = getApplicationContext();
LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadCastReceiver, LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadCastLoginReceiver,
new IntentFilter(Constants.APPLICATION_BROADCAST_INTENT));
LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadCastSyncReceiver,
new IntentFilter(Constants.APPLICATION_BROADCAST_INTENT)); new IntentFilter(Constants.APPLICATION_BROADCAST_INTENT));
/*Database is created*/ /*Database is created*/
...@@ -56,7 +83,8 @@ public class CatalogueApplication extends Application { ...@@ -56,7 +83,8 @@ public class CatalogueApplication extends Application {
@Override @Override
public void onTerminate() { public void onTerminate() {
super.onTerminate(); super.onTerminate();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadCastReceiver); LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadCastLoginReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadCastSyncReceiver);
} }
public static Context getContext() { public static Context getContext() {
......
package com.vsoft.uoflservicenow.adapters; package com.vsoft.servicenow.adapters;
import android.content.Context; import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -9,9 +9,9 @@ import android.widget.ImageView; ...@@ -9,9 +9,9 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import com.vsoft.uoflservicenow.R; import com.vsoft.servicenow.R;
import com.vsoft.uoflservicenow.db.models.Catalogue; import com.vsoft.servicenow.db.models.Catalogue;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.adapters; package com.vsoft.servicenow.adapters;
import android.content.Context; import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -9,9 +9,9 @@ import android.widget.ImageView; ...@@ -9,9 +9,9 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import com.vsoft.uoflservicenow.R; import com.vsoft.servicenow.R;
import com.vsoft.uoflservicenow.db.models.CatalogueItem; import com.vsoft.servicenow.db.models.CatalogueItem;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.adapters; package com.vsoft.servicenow.adapters;
import android.content.Context; import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -7,9 +7,9 @@ import android.view.ViewGroup; ...@@ -7,9 +7,9 @@ import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import android.widget.TextView; import android.widget.TextView;
import com.vsoft.uoflservicenow.R; import com.vsoft.servicenow.utils.Util;
import com.vsoft.uoflservicenow.db.models.Incident; import com.vsoft.servicenow.R;
import com.vsoft.uoflservicenow.utils.Util; import com.vsoft.servicenow.db.models.Incident;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.adapters; package com.vsoft.servicenow.adapters;
import android.content.Context; import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
...@@ -10,8 +10,8 @@ import android.widget.BaseAdapter; ...@@ -10,8 +10,8 @@ import android.widget.BaseAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.vsoft.uoflservicenow.R; import com.vsoft.servicenow.R;
import com.vsoft.uoflservicenow.db.models.MyRequest; import com.vsoft.servicenow.db.models.MyRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api; package com.vsoft.servicenow.api;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
......
package com.vsoft.uoflservicenow.api; package com.vsoft.servicenow.api;
import android.util.Base64; import android.text.TextUtils;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -22,8 +22,7 @@ import retrofit2.converter.gson.GsonConverterFactory; ...@@ -22,8 +22,7 @@ import retrofit2.converter.gson.GsonConverterFactory;
public class RestClient { public class RestClient {
public static Retrofit getInitializedRestAdapter(String username, String password) { public static Retrofit getInitializedRestAdapter(final String accessToken) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level // set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.BODY); logging.setLevel(HttpLoggingInterceptor.Level.BODY);
...@@ -31,17 +30,19 @@ public class RestClient { ...@@ -31,17 +30,19 @@ public class RestClient {
.connectTimeout(10, TimeUnit.SECONDS) .connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS); .readTimeout(30, TimeUnit.SECONDS);
String credentials = username + ":" + password;
final String basic =
"Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
// add your other interceptors // add your other interceptors
Interceptor interceptor = new Interceptor() { Interceptor interceptor = new Interceptor() {
@Override @Override
public Response intercept(Chain chain) { public Response intercept(Chain chain) {
Request original = chain.request(); Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder() Request.Builder requestBuilder = original.newBuilder();
.header(Constants.API_HEADER_PARAM_AUTHORIZATION, basic);
if(!TextUtils.isEmpty(accessToken)) {
final String bearer = "Bearer " + accessToken;
requestBuilder.header(Constants.API_HEADER_PARAM_AUTHORIZATION, bearer);
}
requestBuilder.header("Accept", "application/json"); requestBuilder.header("Accept", "application/json");
requestBuilder.header("Content-Type", "application/json"); requestBuilder.header("Content-Type", "application/json");
requestBuilder.method(original.method(), original.body()); requestBuilder.method(original.method(), original.body());
......
package com.vsoft.uoflservicenow.api.interfaces; package com.vsoft.servicenow.api.interfaces;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
...@@ -16,13 +16,7 @@ import retrofit2.http.Query; ...@@ -16,13 +16,7 @@ import retrofit2.http.Query;
public interface CatalogueCategoryApi { public interface CatalogueCategoryApi {
// Get Catalogue API // Get Catalogue API
@GET(Constants.URL_GET_CATALOGUE) @GET(Constants.URL_GET_CATALOGUE)
Call<ResponseBody> getCatalogue(@Query(Constants.URL_PARAM_SYSPRM_QUERY) String sysparmQuery, Call<ResponseBody> getCatalogue(@Query(Constants.URL_PARAM_SYS_ID) String sysId);
@Query(Constants.URL_PARAM_SYSPRM_FIELDS) String sysParmFields,
@Query(Constants.URL_PARAM_SYSPRM_DISPLAY_VALUE) String sysParmDisplayValue);
// Get Catalogue Order API
@GET(Constants.URL_GET_CATALOGUE_ORDER)
Call<ResponseBody> getCatalogueOrder();
} }
package com.vsoft.uoflservicenow.api.interfaces; package com.vsoft.servicenow.api.interfaces;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
...@@ -16,9 +16,7 @@ import retrofit2.http.Query; ...@@ -16,9 +16,7 @@ import retrofit2.http.Query;
public interface CatalogueCategoryItemApi { public interface CatalogueCategoryItemApi {
// Get Catalogue Item API // Get Catalogue Item API
@GET(Constants.URL_GET_CATALOGUE_ITEM) @GET(Constants.URL_GET_CATALOGUE_ITEM)
Call<ResponseBody> getCatalogueItem(@Query(Constants.URL_PARAM_SYSPRM_QUERY) String sysparmQuery, Call<ResponseBody> getCatalogueItem(@Query(Constants.URL_PARAM_SYS_ID) String sysId);
@Query(Constants.URL_PARAM_SYSPRM_FIELDS) String sysParmFields,
@Query(Constants.URL_PARAM_SYSPRM_DISPLAY_VALUE) String sysParmDisplayValue);
} }
package com.vsoft.uoflservicenow.api.interfaces; package com.vsoft.servicenow.api.interfaces;
import com.vsoft.uoflservicenow.db.models.CatalogueItem; import com.vsoft.servicenow.db.models.CatalogueVariable;
import com.vsoft.uoflservicenow.db.models.CatalogueVariable; import com.vsoft.servicenow.db.models.CatalogueItem;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import java.util.Map; import java.util.Map;
...@@ -30,8 +30,7 @@ public interface CatalogueVariableApi { ...@@ -30,8 +30,7 @@ public interface CatalogueVariableApi {
// Get Variable Choices API // Get Variable Choices API
@GET(Constants.URL_GET_VARIABLE_CHOICE) @GET(Constants.URL_GET_VARIABLE_CHOICE)
Call<ResponseBody> getVariableChoice(@Query(Constants.URL_PARAM_SYSPRM_QUERY) String sysparmQuery, Call<ResponseBody> getVariableChoice(@Query(Constants.URL_PARAM_SYS_ID) String sysId);
@Query(Constants.URL_PARAM_SYSPRM_FIELDS) String sysParmFields);
// Post catalogue item API // Post catalogue item API
@POST(Constants.URL_POST_CATALOGUE_ITEM) @POST(Constants.URL_POST_CATALOGUE_ITEM)
......
package com.vsoft.uoflservicenow.api.interfaces; package com.vsoft.servicenow.api.interfaces;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
......
package com.vsoft.uoflservicenow.api.interfaces; package com.vsoft.servicenow.api.interfaces;
import com.vsoft.uoflservicenow.db.models.LoginItem; import com.vsoft.servicenow.api.pojos.LoginApiResponse;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
...@@ -20,9 +20,19 @@ public interface LoginApi { ...@@ -20,9 +20,19 @@ public interface LoginApi {
// Post Login item API // Post Login item API
@FormUrlEncoded @FormUrlEncoded
@POST(Constants.URL_POST_LOGIN_ITEM) @POST(Constants.URL_POST_LOGIN_ITEM)
Call<ResponseBody> postLoginValues(@Field(LoginItem.Json.GRANT_TYPE) String grantType, @Field(LoginItem.Json.CLIENT_ID) String clientId, Call<ResponseBody> postLoginValues(@Field(LoginApiResponse.Json.GRANT_TYPE) String grantType,
@Field(LoginItem.Json.CLIENT_CECRET) String clientSecret, @Field(LoginItem.Json.USER_NAME) String userName, @Field(LoginApiResponse.Json.CLIENT_ID) String clientId,
@Field(LoginItem.Json.PASSWORD) String password); @Field(LoginApiResponse.Json.CLIENT_SECRET) String clientSecret,
@Field(LoginApiResponse.Json.USER_NAME) String userName,
@Field(LoginApiResponse.Json.PASSWORD) String password);
@FormUrlEncoded
@POST(Constants.URL_REFRESH_LOGIN)
Call<ResponseBody> refreshLogin(@Field(LoginApiResponse.Json.GRANT_TYPE) String grantType,
@Field(LoginApiResponse.Json.CLIENT_ID) String clientId,
@Field(LoginApiResponse.Json.CLIENT_SECRET) String clientSecret,
@Field(LoginApiResponse.Json.REFRESH_TOKEN) String refreshToken);
} }
package com.vsoft.uoflservicenow.api.interfaces; package com.vsoft.servicenow.api.interfaces;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
......
package com.vsoft.uoflservicenow.api.interfaces; package com.vsoft.servicenow.api.interfaces;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
......
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.Catalogue; import com.vsoft.servicenow.db.models.Catalogue;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.CatalogueItem; import com.vsoft.servicenow.db.models.CatalogueItem;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.CatalogueOrder; import com.vsoft.servicenow.db.models.CatalogueOrder;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.CatalogueVariable; import com.vsoft.servicenow.db.models.CatalogueVariableResponse;
import com.vsoft.uoflservicenow.db.models.CatalogueVariableSet;
import java.util.List;
/** /**
* @since 1.0 * @since 1.0
...@@ -11,6 +8,6 @@ import java.util.List; ...@@ -11,6 +8,6 @@ import java.util.List;
* *
*/ */
public interface GetCatalogueVariableApiListener { public interface GetCatalogueVariableApiListener {
void onDoneApiCall(List<CatalogueVariableSet> variableSetList, List<CatalogueVariable> variableList); void onDoneApiCall(CatalogueVariableResponse catalogueVariableResponse);
void onFailApiCall(); void onFailApiCall();
} }
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.Incident; import com.vsoft.servicenow.db.models.Incident;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.MyRequest; import com.vsoft.servicenow.db.models.MyRequest;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.Reference; import com.vsoft.servicenow.db.models.Reference;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.UiPolicyItem; import com.vsoft.servicenow.db.models.UiPolicyItem;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.UserApiValues; import com.vsoft.servicenow.db.models.UserApiValues;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.servicenow.api.pojos.LoginApiResponse;
/** /**
* @since 1.0 * @since 1.0
...@@ -6,6 +8,6 @@ package com.vsoft.uoflservicenow.api.listeners.get; ...@@ -6,6 +8,6 @@ package com.vsoft.uoflservicenow.api.listeners.get;
* *
*/ */
public interface GetUserLoginApiListener { public interface GetUserLoginApiListener {
void onDoneApiCall(); void onDoneApiCall(LoginApiResponse loginApiResponse);
void onFailApiCall(); void onFailApiCall();
} }
package com.vsoft.uoflservicenow.api.listeners.get; package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.VariableChoice; import com.vsoft.servicenow.db.models.VariableChoice;
import java.util.List; import java.util.List;
......
package com.vsoft.uoflservicenow.api.listeners.post; package com.vsoft.servicenow.api.listeners.post;
/** /**
* @since 1.0 * @since 1.0
......
package com.vsoft.uoflservicenow.api.listeners.post; package com.vsoft.servicenow.api.listeners.post;
/** /**
* @since 1.0 * @since 1.0
......
package com.vsoft.uoflservicenow.api.listeners.post; package com.vsoft.servicenow.api.listeners.post;
/** /**
* @since 1.0 * @since 1.0
......
package com.vsoft.uoflservicenow.api.managers; package com.vsoft.servicenow.api.managers;
import android.content.Context;
import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
...@@ -6,12 +9,14 @@ import com.google.gson.JsonDeserializationContext; ...@@ -6,12 +9,14 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer; import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.vsoft.uoflservicenow.api.RestClient; import com.vsoft.servicenow.api.RestClient;
import com.vsoft.uoflservicenow.api.interfaces.CatalogueCategoryItemApi; import com.vsoft.servicenow.api.interfaces.CatalogueCategoryItemApi;
import com.vsoft.uoflservicenow.api.listeners.get.GetCatalogueItemApiListener; import com.vsoft.servicenow.api.listeners.get.GetCatalogueItemApiListener;
import com.vsoft.uoflservicenow.db.models.CatalogueItem; import com.vsoft.servicenow.db.models.CatalogueItem;
import com.vsoft.uoflservicenow.utils.CatalogueLog; import com.vsoft.servicenow.enums.SyncStatus;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.utils.PrefManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
...@@ -33,17 +38,16 @@ import retrofit2.Retrofit; ...@@ -33,17 +38,16 @@ import retrofit2.Retrofit;
*/ */
public class CatalogueItemApiManager { public class CatalogueItemApiManager {
public static void getCatalogueItems(String catalogueSysId, GetCatalogueItemApiListener listener) { public static void getCatalogueItems(Context context, String catalogueSysId, GetCatalogueItemApiListener listener) {
CatalogueLog.d("CatalogueItemApiManager: getCatalogueItems: "); CatalogueLog.d("CatalogueItemApiManager: getCatalogueItems: ");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(CatalogueItem.Json.URL_PARAM_CATALOGUE_SYSPRM_QUERY_VALUE);
stringBuilder.append("=");
stringBuilder.append(catalogueSysId);
stringBuilder.append("^active=true");
CatalogueLog.d("CatalogueItemApiManager: getCatalogueItems: request parameter: "+stringBuilder.toString());
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD); String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
Call<ResponseBody> call = retrofit.create(CatalogueCategoryItemApi.class).getCatalogueItem(stringBuilder.toString(), "sys_id,short_description,name,description,icon", "true"); if(accessToken.isEmpty()) {
listener.onFailApiCall();
return;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(CatalogueCategoryItemApi.class).getCatalogueItem(catalogueSysId);
try { try {
//Retrofit synchronous call //Retrofit synchronous call
Response<ResponseBody> response = call.execute(); Response<ResponseBody> response = call.execute();
...@@ -114,7 +118,20 @@ public class CatalogueItemApiManager { ...@@ -114,7 +118,20 @@ public class CatalogueItemApiManager {
listener.onFailApiCall(); listener.onFailApiCall();
} }
} else { } else {
listener.onFailApiCall(); CatalogueLog.d("CatalogueItemApiManager: getCatalogueItems: response is not success");
if (response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
Log.d(Constants.TAG, "refresh token: " + PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if (status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
getCatalogueItems(context, catalogueSysId, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
}
} else {
listener.onFailApiCall();
}
} }
} catch (IOException e) { } catch (IOException e) {
CatalogueLog.e("CatalogueItemApiManager: getCatalogueItems: IOException: ", e); CatalogueLog.e("CatalogueItemApiManager: getCatalogueItems: IOException: ", e);
...@@ -124,4 +141,4 @@ public class CatalogueItemApiManager { ...@@ -124,4 +141,4 @@ public class CatalogueItemApiManager {
listener.onFailApiCall(); listener.onFailApiCall();
} }
} }
} }
\ No newline at end of file
package com.vsoft.uoflservicenow.api.managers; package com.vsoft.servicenow.api.managers;
import android.content.Context;
import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
...@@ -6,13 +9,15 @@ import com.google.gson.JsonDeserializationContext; ...@@ -6,13 +9,15 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer; import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.vsoft.uoflservicenow.api.RestClient; import com.vsoft.servicenow.api.interfaces.IncidentApi;
import com.vsoft.uoflservicenow.api.interfaces.IncidentApi; import com.vsoft.servicenow.api.listeners.get.GetIncidentApiListener;
import com.vsoft.uoflservicenow.api.listeners.get.GetIncidentApiListener; import com.vsoft.servicenow.utils.Constants;
import com.vsoft.uoflservicenow.api.listeners.post.PostIncidentApiListener; import com.vsoft.servicenow.api.RestClient;
import com.vsoft.uoflservicenow.db.models.Incident; import com.vsoft.servicenow.api.listeners.post.PostIncidentApiListener;
import com.vsoft.uoflservicenow.utils.CatalogueLog; import com.vsoft.servicenow.db.models.Incident;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.enums.SyncStatus;
import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.servicenow.utils.PrefManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
...@@ -34,13 +39,19 @@ import retrofit2.Retrofit; ...@@ -34,13 +39,19 @@ import retrofit2.Retrofit;
*/ */
public class IncidentApiManager { public class IncidentApiManager {
public static void getIncident(GetIncidentApiListener listener) { public static void getIncident(Context context, GetIncidentApiListener listener) {
CatalogueLog.d("IncidentApiManager: getIncident: "); CatalogueLog.d("IncidentApiManager: getIncident: ");
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("caller_id=javascript:gs.getUserID()"); stringBuilder.append("caller_id=javascript:gs.getUserID()");
CatalogueLog.d("IncidentApiManager: getIncident: request parameter: "+stringBuilder.toString()); CatalogueLog.d("IncidentApiManager: getIncident: request parameter: "+stringBuilder.toString());
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD); String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
if(accessToken.isEmpty()) {
listener.onFailApiCall();
return;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(IncidentApi.class).getIncident(stringBuilder.toString(), ""); Call<ResponseBody> call = retrofit.create(IncidentApi.class).getIncident(stringBuilder.toString(), "");
try { try {
//Retrofit synchronous call //Retrofit synchronous call
...@@ -113,7 +124,20 @@ public class IncidentApiManager { ...@@ -113,7 +124,20 @@ public class IncidentApiManager {
listener.onFailApiCall(); listener.onFailApiCall();
} }
} else { } else {
listener.onFailApiCall(); CatalogueLog.d("IncidentApiManager: getIncident: response is not success");
if (response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
Log.d(Constants.TAG, "refresh token: " + PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if (status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
getIncident(context, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
}
} else {
listener.onFailApiCall();
}
} }
} catch (IOException e) { } catch (IOException e) {
CatalogueLog.e("IncidentApiManager: getIncident: IOException: ", e); CatalogueLog.e("IncidentApiManager: getIncident: IOException: ", e);
...@@ -124,22 +148,15 @@ public class IncidentApiManager { ...@@ -124,22 +148,15 @@ public class IncidentApiManager {
} }
} }
public static void submitIncidentForm(Incident incident, String userId, PostIncidentApiListener listener) { public static void submitIncidentForm(Context context, String incidentJsonString, PostIncidentApiListener listener) {
CatalogueLog.d("submitIncidentForm: incidentJson" + incident); CatalogueLog.d("submitIncidentForm: incidentJson" + incidentJsonString);
String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
Gson gson = new GsonBuilder() if(accessToken.isEmpty()) {
.excludeFieldsWithoutExposeAnnotation() listener.onFailApiCall();
.create(); return;
JSONObject incidentJson = incident.toJson(gson.toJson(incident));
try {
incidentJson.put(Incident.Json.OPENED_AT, userId);
incidentJson.put(Incident.Json.CALLER_ID, userId);
} catch (JSONException e) {
CatalogueLog.e(e.toString());
} }
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD); Call<ResponseBody> call = retrofit.create(IncidentApi.class).submitIncident(incidentJsonString);
Call<ResponseBody> call = retrofit.create(IncidentApi.class).submitIncident(incidentJson.toString());
try { try {
//Retrofit synchronous call //Retrofit synchronous call
Response<ResponseBody> response = call.execute(); Response<ResponseBody> response = call.execute();
...@@ -156,7 +173,20 @@ public class IncidentApiManager { ...@@ -156,7 +173,20 @@ public class IncidentApiManager {
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
listener.onFailApiCall(); CatalogueLog.d("IncidentApiManager: submitIncidentForm: response is not success");
if(response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if(status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
submitIncidentForm(context, incidentJsonString, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
listener.onFailApiCall();
}
} else {
listener.onFailApiCall();
}
} }
} catch (IOException e) { } catch (IOException e) {
CatalogueLog.e("IncidentApiManager: submitIncidentForm: IOException: ", e); CatalogueLog.e("IncidentApiManager: submitIncidentForm: IOException: ", e);
...@@ -166,4 +196,4 @@ public class IncidentApiManager { ...@@ -166,4 +196,4 @@ public class IncidentApiManager {
listener.onFailApiCall(); listener.onFailApiCall();
} }
} }
} }
\ No newline at end of file
package com.vsoft.servicenow.api.managers;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.vsoft.servicenow.api.RestClient;
import com.vsoft.servicenow.api.interfaces.LoginApi;
import com.vsoft.servicenow.api.listeners.get.GetUserLoginApiListener;
import com.vsoft.servicenow.api.pojos.LoginApiResponse;
import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.enums.SyncStatus;
import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.servicenow.utils.PrefManager;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.lang.reflect.Type;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
/**
* Created by kvemulavada on 8/29/2016.
*/
public class LoginApiManager {
public static void submitLoginValues(String grantType, String clientId, String clientSecret, String userName, String password, GetUserLoginApiListener listener) {
final Retrofit retrofit = RestClient.getInitializedRestAdapterWithOutAuthorizationHeader();
Call<ResponseBody> call = retrofit.create(LoginApi.class).postLoginValues(grantType, clientId, clientSecret, userName, password);
try {
//Retrofit synchronous call
Response<ResponseBody> response = call.execute();
if (response.isSuccessful()) {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
JSONObject error = jsonObject.optJSONObject(Constants.RESPONSE_ERROR_OBJECT_NAME);
if (error == null) {
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(long.class, new JsonDeserializer<Long>() {
@Override
public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
long value = 0;
try {
value = json.getAsLong();
} catch (NumberFormatException e) {
CatalogueLog.d("LoginApiManager: getCatalogues: deserialize: long.class: NumberFormatException: ");
}
return value;
}
})
.registerTypeAdapter(int.class, new JsonDeserializer<Integer>() {
@Override
public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
int value = 0;
try {
value = json.getAsInt();
} catch (NumberFormatException e) {
CatalogueLog.d("LoginApiManager: submitLoginValues: deserialize: int.class: NumberFormatException: ");
}
return value;
}
})
.registerTypeAdapter(float.class, new JsonDeserializer<Float>() {
@Override
public Float deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
float value = 0;
try {
value = json.getAsFloat();
} catch (NumberFormatException e) {
CatalogueLog.e("LoginApiManager: submitLoginValues: deserialize: float.class: NumberFormatException: ", e);
}
return value;
}
})
.create();
LoginApiResponse loginApiResponse = gson.fromJson(jsonObject.toString(), LoginApiResponse.class);
listener.onDoneApiCall(loginApiResponse);
} else
listener.onFailApiCall();
} catch (JSONException e) {
CatalogueLog.e("LoginApiManager: submitLoginValues: onResponse: ", e);
listener.onFailApiCall();
} catch (IOException e) {
CatalogueLog.e("LoginApiManager: submitLoginValues: onResponse: ", e);
listener.onFailApiCall();
}
} else {
listener.onFailApiCall();
}
} catch (IOException e) {
CatalogueLog.e("LoginApiManager: submitLoginValues: IOException: ", e);
listener.onFailApiCall();
} catch (NullPointerException e) {
CatalogueLog.e("LoginApiManager: submitLoginValues: IOException: ", e);
listener.onFailApiCall();
}
}
public static SyncStatus refreshLogin(Context context, String refreshToken) {
CatalogueLog.d("LoginApiManager: refreshLogin");
final Retrofit retrofit = RestClient.getInitializedRestAdapterWithOutAuthorizationHeader();
Call<ResponseBody> call = retrofit.create(LoginApi.class).refreshLogin(LoginApiResponse.Json.REFRESH_TOKEN,
Constants.LOGIN_CLIENT_ID,
Constants.LOGIN_CLIENT_SECRET,
refreshToken);
try {
Response<ResponseBody> response = call.execute();
if (response.isSuccessful()) {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
CatalogueLog.d("refreshLogin: successfully refreshed login.");
PrefManager.setSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN, jsonObject.getString(LoginApiResponse.Json.JSON_ACCESS_TOKEN));
return SyncStatus.SUCCESS;
} catch(JSONException e) {
CatalogueLog.e("refreshLogin: JSONException: ", e);
return SyncStatus.FAIL;
}
} else {
CatalogueLog.d("refreshLogin: failed to refresh token...");
if(response.code() == 401) {
CatalogueLog.d("---- 401, unSetting access and refresh token, prompt user to login again");
PrefManager.setSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN, "");
PrefManager.setSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN, "");
Intent intent = new Intent(Constants.APPLICATION_BROADCAST_INTENT);
intent.putExtra(Constants.APPLICATION_BROADCAST_DATA_ACTION, Constants.ACTION_PROMPT_LOGIN);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
return SyncStatus.FAIL;
}
} catch(IOException e) {
CatalogueLog.e("refreshLogin: IOException: ", e);
return SyncStatus.FAIL;
}
}
}
package com.vsoft.uoflservicenow.api.managers; package com.vsoft.servicenow.api.managers;
import android.content.Context;
import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
...@@ -6,20 +9,24 @@ import com.google.gson.JsonDeserializationContext; ...@@ -6,20 +9,24 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer; import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.vsoft.uoflservicenow.api.RestClient; import com.vsoft.servicenow.db.models.MyRequest;
import com.vsoft.uoflservicenow.api.interfaces.MyRequestApi; import com.vsoft.servicenow.api.RestClient;
import com.vsoft.uoflservicenow.api.listeners.get.GetMyRequestApiListener; import com.vsoft.servicenow.api.interfaces.MyRequestApi;
import com.vsoft.uoflservicenow.db.models.MyRequest; import com.vsoft.servicenow.api.listeners.get.GetMyRequestApiListener;
import com.vsoft.uoflservicenow.enums.SyncStatus; import com.vsoft.servicenow.enums.SyncStatus;
import com.vsoft.uoflservicenow.utils.CatalogueLog; import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.utils.PrefManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Response; import retrofit2.Response;
...@@ -30,12 +37,17 @@ import retrofit2.Retrofit; ...@@ -30,12 +37,17 @@ import retrofit2.Retrofit;
*/ */
public class MyRequestApiManager { public class MyRequestApiManager {
public static SyncStatus getMyrequests(GetMyRequestApiListener listener) { public static SyncStatus getMyRequests(Context context, GetMyRequestApiListener listener) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(MyRequest.Json.URL_PARAM_CATALOGUE_SYSPRM_QUERY_VALUE); stringBuilder.append(MyRequest.Json.URL_PARAM_CATALOGUE_SYSPRM_QUERY_VALUE);
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD); String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
if(accessToken.isEmpty()) {
listener.onFailApiCall();
return SyncStatus.FAIL;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(MyRequestApi.class).getMyRequest(stringBuilder.toString(), "true"); Call<ResponseBody> call = retrofit.create(MyRequestApi.class).getMyRequest(stringBuilder.toString(), "true");
try { try {
//Retrofit synchronous call //Retrofit synchronous call
...@@ -109,7 +121,22 @@ public class MyRequestApiManager { ...@@ -109,7 +121,22 @@ public class MyRequestApiManager {
return SyncStatus.FAIL; return SyncStatus.FAIL;
} }
} else { } else {
return SyncStatus.FAIL; CatalogueLog.d("MyRequestApiManager: getMyRequest: response is not success");
if(response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if(status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
return getMyRequests(context, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
listener.onFailApiCall();
return SyncStatus.FAIL;
}
} else {
listener.onFailApiCall();
return SyncStatus.FAIL;
}
} }
} catch (IOException e) { } catch (IOException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogues: IOException: ", e); CatalogueLog.e("CatalogueApiManager: getCatalogues: IOException: ", e);
......
package com.vsoft.uoflservicenow.api.managers; package com.vsoft.servicenow.api.managers;
import android.content.Context;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
...@@ -6,12 +8,13 @@ import com.google.gson.JsonDeserializationContext; ...@@ -6,12 +8,13 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer; import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.vsoft.uoflservicenow.api.RestClient; import com.vsoft.servicenow.api.interfaces.UserApi;
import com.vsoft.uoflservicenow.api.interfaces.UserApi; import com.vsoft.servicenow.api.listeners.get.GetUserDetailApiListener;
import com.vsoft.uoflservicenow.api.listeners.get.GetUserDetailApiListener; import com.vsoft.servicenow.api.RestClient;
import com.vsoft.uoflservicenow.db.models.UserApiValues; import com.vsoft.servicenow.db.models.UserApiValues;
import com.vsoft.uoflservicenow.utils.CatalogueLog; import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.utils.PrefManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
...@@ -31,9 +34,14 @@ import retrofit2.Retrofit; ...@@ -31,9 +34,14 @@ import retrofit2.Retrofit;
* Created by kvemulavada on 8/31/2016. * Created by kvemulavada on 8/31/2016.
*/ */
public class UserApiManager { public class UserApiManager {
public static void getUserDetailResponse(String userName, GetUserDetailApiListener listener) { public static void getUserDetailResponse(Context context, String userName, GetUserDetailApiListener listener) {
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD); String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
if(accessToken.isEmpty()) {
listener.onFailApiCall();
return;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(UserApi.class).getuserDetails(userName); Call<ResponseBody> call = retrofit.create(UserApi.class).getuserDetails(userName);
try { try {
//Retrofit synchronous call //Retrofit synchronous call
......
package com.vsoft.uoflservicenow.api.managers; package com.vsoft.servicenow.api.managers;
import android.content.Context;
import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
...@@ -6,14 +9,16 @@ import com.google.gson.JsonDeserializationContext; ...@@ -6,14 +9,16 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer; import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.vsoft.uoflservicenow.api.RestClient; import com.vsoft.servicenow.api.interfaces.CatalogueVariableApi;
import com.vsoft.uoflservicenow.api.interfaces.CatalogueVariableApi; import com.vsoft.servicenow.api.listeners.get.GetReferenceApiListener;
import com.vsoft.uoflservicenow.api.listeners.get.GetReferenceApiListener; import com.vsoft.servicenow.api.listeners.get.GetVariableChoiceApiListener;
import com.vsoft.uoflservicenow.api.listeners.get.GetVariableChoiceApiListener; import com.vsoft.servicenow.db.models.VariableChoice;
import com.vsoft.uoflservicenow.db.models.Reference; import com.vsoft.servicenow.utils.Constants;
import com.vsoft.uoflservicenow.db.models.VariableChoice; import com.vsoft.servicenow.api.RestClient;
import com.vsoft.uoflservicenow.utils.CatalogueLog; import com.vsoft.servicenow.db.models.Reference;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.enums.SyncStatus;
import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.servicenow.utils.PrefManager;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
...@@ -32,20 +37,21 @@ import retrofit2.Retrofit; ...@@ -32,20 +37,21 @@ import retrofit2.Retrofit;
/** /**
* @author Kunj on 11/8/16. * @author Kunj on 11/8/16.
* *bkunj@vscbkunj@vsc
*/ */
public class VariableChoiceApiManager { public class VariableChoiceApiManager {
public static void getVariableChoice(String variableSysId, GetVariableChoiceApiListener listener) { public static void getVariableChoice(Context context, String variableSysId, GetVariableChoiceApiListener listener) {
CatalogueLog.d("VariableChoiceApiManager: getVariableChoice: "); CatalogueLog.d("VariableChoiceApiManager: getVariableChoice: ");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(VariableChoice.Json.URL_PARAM_VARIABLE_CHOICE_SYSPRM_QUERY_VALUE);
stringBuilder.append("=");
stringBuilder.append(variableSysId);
CatalogueLog.d("VariableChoiceApiManager: getVariableChoice: request parameter: "+stringBuilder.toString());
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD); String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
Call<ResponseBody> call = retrofit.create(CatalogueVariableApi.class).getVariableChoice(stringBuilder.toString(), "text,value,order,misc"); if(accessToken.isEmpty()) {
listener.onFailApiCall();
return;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(CatalogueVariableApi.class).getVariableChoice(variableSysId);
try { try {
//Retrofit synchronous call //Retrofit synchronous call
Response<ResponseBody> response = call.execute(); Response<ResponseBody> response = call.execute();
...@@ -116,7 +122,20 @@ public class VariableChoiceApiManager { ...@@ -116,7 +122,20 @@ public class VariableChoiceApiManager {
listener.onFailApiCall(); listener.onFailApiCall();
} }
} else { } else {
listener.onFailApiCall(); CatalogueLog.d("VariableChoiceApiManager: getVariableChoice: response is not success");
if(response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if(status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
getVariableChoice(context, variableSysId, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
listener.onFailApiCall();
}
} else {
listener.onFailApiCall();
}
} }
} catch (IOException e) { } catch (IOException e) {
CatalogueLog.e("VariableChoiceApiManager: getVariableChoice: IOException: ", e); CatalogueLog.e("VariableChoiceApiManager: getVariableChoice: IOException: ", e);
...@@ -127,10 +146,16 @@ public class VariableChoiceApiManager { ...@@ -127,10 +146,16 @@ public class VariableChoiceApiManager {
} }
} }
public static void getReference(String tableName, Map<String,String> queryMap, GetReferenceApiListener listener) { public static void getReference(Context context, String tableName, Map<String,String> queryMap, GetReferenceApiListener listener) {
CatalogueLog.d("VariableChoiceApiManager: getReference: tableName: "+tableName); CatalogueLog.d("VariableChoiceApiManager: getReference: tableName: "+tableName);
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD); String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
if(accessToken.isEmpty()) {
listener.onFailApiCall();
return;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(CatalogueVariableApi.class).getReference(Constants.URL_GET_REFERENCE + tableName, queryMap); Call<ResponseBody> call = retrofit.create(CatalogueVariableApi.class).getReference(Constants.URL_GET_REFERENCE + tableName, queryMap);
try { try {
//Retrofit synchronous call //Retrofit synchronous call
...@@ -202,7 +227,20 @@ public class VariableChoiceApiManager { ...@@ -202,7 +227,20 @@ public class VariableChoiceApiManager {
listener.onFailApiCall(); listener.onFailApiCall();
} }
} else { } else {
listener.onFailApiCall(); CatalogueLog.d("VariableChoiceApiManager: getReference: response is not success");
if(response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if(status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
getReference(context, tableName, queryMap, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
listener.onFailApiCall();
}
} else {
listener.onFailApiCall();
}
} }
} catch (IOException e) { } catch (IOException e) {
CatalogueLog.e("VariableChoiceApiManager: getReference: IOException: ", e); CatalogueLog.e("VariableChoiceApiManager: getReference: IOException: ", e);
...@@ -212,4 +250,4 @@ public class VariableChoiceApiManager { ...@@ -212,4 +250,4 @@ public class VariableChoiceApiManager {
listener.onFailApiCall(); listener.onFailApiCall();
} }
} }
} }
\ No newline at end of file
package com.vsoft.servicenow.api.pojos;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by kunj on 14/12/2016.
*/
public class LoginApiResponse {
@SerializedName("access_token")
@Expose
private String accessToken;
@SerializedName("refresh_token")
@Expose
private String refreshToken;
@SerializedName("scope")
@Expose
private String scope;
@SerializedName("token_type")
@Expose
private String tokenType;
@SerializedName("expires_in")
@Expose
private Integer expiresIn;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getRefreshToken() {
return refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getTokenType() {
return tokenType;
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public Integer getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(Integer expiresIn) {
this.expiresIn = expiresIn;
}
public static class Json {
public static final String GRANT_TYPE = "grant_type";
public static final String CLIENT_ID = "client_id";
public static final String CLIENT_SECRET = "client_secret";
public static final String USER_NAME = "username";
public static final String PASSWORD = "password";
public static final String REFRESH_TOKEN = "refresh_token";
public static final String JSON_ACCESS_TOKEN = "access_token";
}
}
package com.vsoft.uoflservicenow.db; package com.vsoft.servicenow.db;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
...@@ -6,8 +6,8 @@ import android.database.sqlite.SQLiteDatabase; ...@@ -6,8 +6,8 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import com.vsoft.uoflservicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import com.vsoft.uoflservicenow.utils.DBConstants; import com.vsoft.servicenow.utils.DBConstants;
/** /**
...@@ -33,6 +33,9 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants { ...@@ -33,6 +33,9 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants {
createVariableChoiceTable(db); createVariableChoiceTable(db);
createMyIncidentTable(db); createMyIncidentTable(db);
createMyRequestTable(db); createMyRequestTable(db);
createMyRequestItemTable(db);
createMyRequestOrderTable(db);
createMyRequestOpenedByTable(db);
createUiPolicyItemTable(db); createUiPolicyItemTable(db);
createUiPolicyActionTable(db); createUiPolicyActionTable(db);
createCatalogueItemInputTable(db); createCatalogueItemInputTable(db);
...@@ -117,12 +120,39 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants { ...@@ -117,12 +120,39 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants {
db.execSQL("create table " + TABLE_MY_REQUEST + "(" db.execSQL("create table " + TABLE_MY_REQUEST + "("
+ REQUEST_ID + " integer primary key autoincrement, " + REQUEST_ID + " integer primary key autoincrement, "
+ REQUEST_NUMBER + " text, " + REQUEST_NUMBER + " text, "
+ REQUEST_SHORT_DESCRIPTION + " text, " + REQUEST_SHORT_DESCRIPTION + " real, "
+ REQUEST_SYS_UPDATED_ON + " real, " + REQUEST_SYS_UPDATED_ON + " real, "
+ REQUEST_APPROVAL + " text, " + REQUEST_APPROVAL + " text, "
+ REQUEST_STAGE + " text, "
+ REQUEST_OPENED_BY_VALUE + " real, "
+ REQUEST_ORDER_DISPLAY_VALUE + " real, "
+ REQUEST_SYNC_DIRTY + " integer default " + SYNC_FLAG_NONE + ");"); + REQUEST_SYNC_DIRTY + " integer default " + SYNC_FLAG_NONE + ");");
} }
private void createMyRequestItemTable(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_MY_REQUEST_DESCRIPTION + "("
+ MY_REQUEST_DESCRIPTION_ID + " integer primary key autoincrement, "
+ MY_REQUEST_DESCRIPTION_DISPLAY_VALUE + " text, "
+ MY_REQUEST_DESCRIPTION_LINK + " real, "
+ MY_REQUEST_DESCRIPTION_SYNC_DIRTY + " integer default " + SYNC_FLAG_NONE + ");");
}
private void createMyRequestOpenedByTable(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_MY_REQUEST_OPENED_BY + "("
+ MY_REQUEST_OPENED_BY_ID + " integer primary key autoincrement, "
+ MY_REQUEST_OPENED_BY_DISPLAY_VALUE + " text, "
+ MY_REQUEST_OPENED_BY_LINK + " real, "
+ MY_REQUEST_OPENED_BY_SYNC_DIRTY + " integer default " + SYNC_FLAG_NONE + ");");
}
private void createMyRequestOrderTable(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_MY_REQUEST_DETAIL + "("
+ MY_REQUEST_ORDER_ID + " integer primary key autoincrement, "
+ MY_REQUEST_ORDER_DISPLAY_VALUE + " text, "
+ MY_REQUEST_ORDER_LINK + " real, "
+ MY_REQUEST_ORDER_SYNC_DIRTY + " integer default " + SYNC_FLAG_NONE + ");");
}
private void createUiPolicyItemTable(SQLiteDatabase db) { private void createUiPolicyItemTable(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_UI_POLICY_ITEM + "(" db.execSQL("create table " + TABLE_UI_POLICY_ITEM + "("
+ UI_POLICY_ITEM_ID + " integer primary key autoincrement, " + UI_POLICY_ITEM_ID + " integer primary key autoincrement, "
......
package com.vsoft.uoflservicenow.db.managers; package com.vsoft.servicenow.db.managers;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import com.vsoft.uoflservicenow.CatalogueApplication; import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.uoflservicenow.db.models.Attachment; import com.vsoft.servicenow.db.models.Attachment;
import com.vsoft.uoflservicenow.utils.DBConstants; import com.vsoft.servicenow.utils.DBConstants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -109,13 +109,12 @@ public class AttachmentManager implements DBConstants { ...@@ -109,13 +109,12 @@ public class AttachmentManager implements DBConstants {
return attachment; return attachment;
} }
public static List<Attachment> getDirtyAttachment(long itemInputId) { public static List<Attachment> getDirtyAttachment() {
SQLiteDatabase db = CatalogueApplication.getDatabase(); SQLiteDatabase db = CatalogueApplication.getDatabase();
if(db!=null) { if(db!=null) {
Cursor c = db.rawQuery("select * from " + TABLE_ATTACHMENT Cursor c = db.rawQuery("select * from " + TABLE_ATTACHMENT
+ " where " + ATTACHMENT_ITEM_INPUT_ID + "=" + itemInputId + " where " + ATTACHMENT_SYNC_DIRTY + ">" + SYNC_FLAG_NONE , null);
+ " and " + ATTACHMENT_SYNC_DIRTY + ">" + SYNC_FLAG_NONE , null);
ArrayList<Attachment> attachmentList; ArrayList<Attachment> attachmentList;
if (c.getCount() > 0) { if (c.getCount() > 0) {
attachmentList = new ArrayList<>(c.getCount()); attachmentList = new ArrayList<>(c.getCount());
......
package com.vsoft.uoflservicenow.db.managers; package com.vsoft.servicenow.db.managers;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import com.vsoft.uoflservicenow.CatalogueApplication; import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.uoflservicenow.db.models.CatalogueItemInput; import com.vsoft.servicenow.db.models.CatalogueItemInput;
import com.vsoft.uoflservicenow.utils.DBConstants; import com.vsoft.servicenow.utils.DBConstants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -97,12 +97,12 @@ public class CatalogueItemInputManager implements DBConstants { ...@@ -97,12 +97,12 @@ public class CatalogueItemInputManager implements DBConstants {
} }
} }
public static CatalogueItemInput get(long catalogueId) { public static CatalogueItemInput get(long catalogueInputId) {
SQLiteDatabase db = CatalogueApplication.getDatabase(); SQLiteDatabase db = CatalogueApplication.getDatabase();
CatalogueItemInput catalogueItemInput = null; CatalogueItemInput catalogueItemInput = null;
if (db != null) { if (db != null) {
Cursor c = db.rawQuery("select * from " + TABLE_CATALOGUE_ITEM_INPUT + " where " + CATALOGUE_ITEM_INPUT_ID + "=" + catalogueId, null); Cursor c = db.rawQuery("select * from " + TABLE_CATALOGUE_ITEM_INPUT + " where " + CATALOGUE_ITEM_INPUT_ID + "=" + catalogueInputId, null);
if (c.moveToFirst()) { if (c.moveToFirst()) {
CatalogueItemInput.CatalogueItemInputBuilder builder = CatalogueItemInput.CatalogueItemInputBuilder.aCatalogueItemInput(); CatalogueItemInput.CatalogueItemInputBuilder builder = CatalogueItemInput.CatalogueItemInputBuilder.aCatalogueItemInput();
fillAllItemInputDetails(c, builder); fillAllItemInputDetails(c, builder);
......
package com.vsoft.uoflservicenow.db.managers; package com.vsoft.servicenow.db.managers;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import com.vsoft.uoflservicenow.CatalogueApplication; import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.uoflservicenow.db.models.CatalogueItem; import com.vsoft.servicenow.db.models.CatalogueItem;
import com.vsoft.uoflservicenow.utils.DBConstants; import com.vsoft.servicenow.utils.DBConstants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
......
package com.vsoft.uoflservicenow.db.managers; package com.vsoft.servicenow.db.managers;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import com.vsoft.uoflservicenow.CatalogueApplication; import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.uoflservicenow.db.models.Catalogue; import com.vsoft.servicenow.db.models.Catalogue;
import com.vsoft.uoflservicenow.utils.DBConstants; import com.vsoft.servicenow.utils.DBConstants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
......
package com.vsoft.uoflservicenow.db.managers; package com.vsoft.servicenow.db.managers;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import com.vsoft.uoflservicenow.CatalogueApplication; import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.uoflservicenow.db.models.CatalogueVariable; import com.vsoft.servicenow.db.models.CatalogueVariable;
import com.vsoft.uoflservicenow.enums.ViewType; import com.vsoft.servicenow.enums.ViewType;
import com.vsoft.uoflservicenow.utils.CatalogueLog; import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.uoflservicenow.utils.DBConstants; import com.vsoft.servicenow.utils.DBConstants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -50,7 +50,6 @@ public class CatalogueVariableManager implements DBConstants { ...@@ -50,7 +50,6 @@ public class CatalogueVariableManager implements DBConstants {
} }
public static int update(CatalogueVariable catalogueVariable, List<String> column, int syncDirty) { public static int update(CatalogueVariable catalogueVariable, List<String> column, int syncDirty) {
CatalogueLog.e("CatalogueVariableManager: update");
SQLiteDatabase db = CatalogueApplication.getDatabase(); SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) { if (db != null) {
catalogueVariable.setSyncDirty(syncDirty); catalogueVariable.setSyncDirty(syncDirty);
......
package com.vsoft.uoflservicenow.db.managers; package com.vsoft.servicenow.db.managers;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import com.vsoft.uoflservicenow.CatalogueApplication; import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.uoflservicenow.db.models.Incident; import com.vsoft.servicenow.db.models.Incident;
import com.vsoft.uoflservicenow.enums.Impact; import com.vsoft.servicenow.enums.Impact;
import com.vsoft.uoflservicenow.utils.DBConstants; import com.vsoft.servicenow.utils.DBConstants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
......
package com.vsoft.servicenow.db.managers;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.servicenow.db.models.MyRequestDisplayValue;
import com.vsoft.servicenow.utils.DBConstants;
import java.util.List;
/**
*
* @author Kunj on 18-11-2016.
*/
public class MyRequestDescriptionDisplayValueManager implements DBConstants {
public static long save(MyRequestDisplayValue myRequestDisplayValue, int syncDirty) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
myRequestDisplayValue.setSyncDirty(syncDirty);
long id = db.insert(TABLE_MY_REQUEST_DESCRIPTION, null, getContentValues(myRequestDisplayValue));
myRequestDisplayValue.setId(id);
return id;
} else {
return -1;
}
}
public static int delete(MyRequestDisplayValue myRequestDisplayValue) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
return db.delete(TABLE_MY_REQUEST_DESCRIPTION, MY_REQUEST_DESCRIPTION_ID + "=" + myRequestDisplayValue.getId(), null);
}
return -1;
}
public static int update(MyRequestDisplayValue myRequestDisplayValue, int syncDirty) {
return update(myRequestDisplayValue, null, syncDirty);
}
public static int update(MyRequestDisplayValue myRequestDisplayValue, List<String> column, int syncDirty) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
myRequestDisplayValue.setSyncDirty(syncDirty);
if (column == null || column.size() == 0) {
return db.update(TABLE_MY_REQUEST_DESCRIPTION, getContentValues(myRequestDisplayValue), MY_REQUEST_DESCRIPTION_ID + "=" +
myRequestDisplayValue.getId(), null);
} else {
ContentValues contentValues = new ContentValues(column.size());
contentValues.put(MY_REQUEST_DESCRIPTION_SYNC_DIRTY, myRequestDisplayValue.getSyncDirty());
for (int i = 0; i < column.size(); i++) {
String columnName = column.get(i);
if (MY_REQUEST_DESCRIPTION_LINK.equals(columnName)) {
contentValues.put(MY_REQUEST_DESCRIPTION_LINK, myRequestDisplayValue.getLink());
} else if (MY_REQUEST_DESCRIPTION_DISPLAY_VALUE.equals(columnName)) {
contentValues.put(MY_REQUEST_DESCRIPTION_DISPLAY_VALUE, myRequestDisplayValue.getDisplayValue());
}
}
return db.update(TABLE_MY_REQUEST_DESCRIPTION, contentValues, MY_REQUEST_DESCRIPTION_ID + "=" + myRequestDisplayValue.getId(),
null);
}
} else {
return -1;
}
}
public static void handleGetRequestItem(MyRequestDisplayValue serverMyRequestDisplayValue, long requestId) {
MyRequestDisplayValue localMyRequestDisplayValue = get(requestId);
if (localMyRequestDisplayValue != null) {
delete(localMyRequestDisplayValue);
} else {
save(serverMyRequestDisplayValue, SYNC_FLAG_NONE);
}
}
public static MyRequestDisplayValue get(long id) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
MyRequestDisplayValue myRequestDisplayValue = null;
if (db != null) {
Cursor c = db.rawQuery("select * from " + TABLE_MY_REQUEST_DESCRIPTION + " where " + MY_REQUEST_DESCRIPTION_ID + "=" + id, null);
if (c.moveToFirst()) {
MyRequestDisplayValue.MyRequestDisplayValueBuilder builder = MyRequestDisplayValue.MyRequestDisplayValueBuilder.aMyRequest();
fillAllRequestItemDetails(c, builder);
myRequestDisplayValue = builder.build();
}
c.close();
}
return myRequestDisplayValue;
}
private static void fillAllRequestItemDetails(Cursor c, MyRequestDisplayValue.MyRequestDisplayValueBuilder builder) {
builder.setId(c.getLong(INDEX_MY_REQUEST_DESCRIPTION_ID));
builder.setDisplayValue(c.getString(INDEX_MY_REQUEST_DESCRIPTION_DISPLAY_VALUE));
builder.setLink(c.getString(INDEX_MY_REQUEST_DESCRIPTION_LINK));
builder.setSyncDirty(c.getInt(INDEX_MY_REQUEST_DESCRIPTION_SYNC_DIRTY));
}
private static ContentValues getContentValues(MyRequestDisplayValue myRequestDisplayValue) {
ContentValues cv = new ContentValues(MY_REQUEST_DESCRIPTION_COLUMN_COUNT - 1);
cv.put(MY_REQUEST_DESCRIPTION_DISPLAY_VALUE, myRequestDisplayValue.getDisplayValue());
cv.put(MY_REQUEST_DESCRIPTION_LINK, myRequestDisplayValue.getLink());
cv.put(MY_REQUEST_DESCRIPTION_SYNC_DIRTY, myRequestDisplayValue.getSyncDirty());
return cv;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment