Created
February 25, 2017 20:54
-
-
Save parahall/7856437221bec3adb3ee1e538813f5cf to your computer and use it in GitHub Desktop.
SendOrderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SendOrderService extends IntentService { | |
@Override protected void onHandleIntent(Intent intent) { | |
int orderId = intent.getIntExtra(ORDER_ID, 0); | |
if (orderId == 0 || orderId == -1) { | |
return; | |
} | |
Cursor c = null; | |
try { | |
c = getContentResolver().query( | |
KolGeneContract.OrderEntry.buildOrderUriWithIdAndStatus(orderId, Order.NOT_SYNCED), null, | |
null, null, null); | |
if (c == null) return; | |
Order order = new Order(); | |
if (c.moveToFirst()) { | |
order.getSelfFromCursor(c, order); | |
} else { | |
return; | |
} | |
OrderCreate orderCreate = order.createPostOrder(order); | |
List<LocationId> locationIds = new LabLocation().getLocationIds(this, order.getLocalId()); | |
orderCreate.setLabLocations(locationIds); | |
Response<Order> response = orderApi.createOrder(orderCreate).execute(); | |
if (response.isSuccessful()) { | |
if (response.code() == 201) { | |
Order responseOrder = response.body(); | |
responseOrder.setLocalId(orderId); | |
responseOrder.setSync(Order.SYNCED); | |
ContentValues values = responseOrder.getContentValues(responseOrder); | |
Uri uri = getContentResolver().update( | |
KolGeneContract.OrderEntry.buildOrderUriWithId(order.getLocalId()), values); | |
return; | |
} | |
} else { | |
if (response.code() == 401) { | |
ClientUtils.broadcastUnAuthorizedIntent(this); | |
return; | |
} | |
} | |
} catch (IOException e) { | |
} finally { | |
if (c != null && !c.isClosed()) { | |
c.close(); | |
} | |
} | |
SyncOrderService.scheduleOrderSending(getApplicationContext(), orderId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment