/** * Returns a list of all active and finished one-time orders going back n number of months, for * all direct immediate of the given parent user id. This is useful for determining usage across * all child users. * * @param parentUserId parent user id * @param itemId item id of order lines * @param months previous number of months to include (1 = 1 month period starting from today) * @return list of found one-time orders, empty list if none found */ @SuppressWarnings("unchecked") public List<OrderLineDTO> findOnetimeByParentUserItem( Integer parentUserId, Integer itemId, Integer months) { UserDTO parent = new UserBL(parentUserId).getEntity(); if (parent == null || parent.getCustomer() == null) { LOG.warn("Parent user " + parentUserId + " does not exist or is not a customer!"); return Collections.emptyList(); } final String hql = "select line " + " from OrderLineDTO line " + " where line.deleted = 0 " + " and line.item.id = :itemId " + " and line.purchaseOrder.baseUserByUserId.customer.parent.id = :parentId" + " and line.purchaseOrder.orderPeriod.id = :period " + " and (line.purchaseOrder.orderStatus.orderStatusFlag = :active_status" + " or line.purchaseOrder.orderStatus.orderStatusFlag = :finished_status)" + " and line.purchaseOrder.deleted = 0 " + " and line.purchaseOrder.createDate > :startdate "; Query query = getSession().createQuery(hql); query.setParameter("itemId", itemId); query.setParameter("parentId", parent.getCustomer().getId()); query.setParameter("period", Constants.ORDER_PERIOD_ONCE); query.setParameter("active_status", OrderStatusFlag.INVOICE.ordinal()); query.setParameter("finished_status", OrderStatusFlag.FINISHED); DateMidnight startdate = new DateMidnight().minusMonths(months); query.setParameter("startdate", startdate.toDate()); return query.list(); }
public List<int[]> getSkyline(int[][] buildings) { if (buildings == null || buildings.length == 0) { return Collections.emptyList(); } final PriorityQueue<Building> endings = new PriorityQueue<>((v1, v2) -> Integer.compare(v1.to, v2.to)); final PriorityQueue<Integer> heights = new PriorityQueue<>((v1, v2) -> Integer.compare(v2, v1)); final List<int[]> result = new ArrayList<>(); // iterate over each of the building for (int[] build : buildings) { final Building building = new Building(build); while (endings.size() > 0 && endings.peek().to < building.from) { removeBuildings(endings, heights, result); } if (heights.size() == 0 || building.height > heights.peek()) { result.add(new int[] {building.from, building.height}); } heights.add(building.height); endings.add(building); } while (endings.size() > 0) { removeBuildings(endings, heights, result); } return result; }
/* * Returns a keystore entry alias and a list of target keystores. * When the supplied alias prefix identifies a keystore then that single * keystore is returned. When no alias prefix is supplied then all the * keystores are returned. */ private AbstractMap.SimpleEntry<String, Collection<KeyStore>> getKeystoresForReading( String alias) { String[] splits = alias.split(this.entryNameSeparatorRegEx, 2); if (splits.length == 2) { // prefixed alias KeyStore keystore = keystores.get(splits[0]); if (keystore != null) { return new AbstractMap.SimpleEntry<>( splits[1], (Collection<KeyStore>) Collections.singleton(keystore)); } } else if (splits.length == 1) { // unprefixed alias // Check all keystores for the first occurrence of the alias return new AbstractMap.SimpleEntry<>(alias, keystores.values()); } return new AbstractMap.SimpleEntry<>( "", (Collection<KeyStore>) Collections.<KeyStore>emptyList()); }
/** * Base fragment for displaying a list of items that loads with a progress bar * visible * * @param <E> */ public abstract class ItemListFragment<E> extends Fragment implements LoaderCallbacks<List<E>> { private static final String FORCE_REFRESH = "forceRefresh"; /** * @param args bundle passed to the loader by the LoaderManager * @return true if the bundle indicates a requested forced refresh of the * items */ protected static boolean isForceRefresh(final Bundle args) { return args != null && args.getBoolean(FORCE_REFRESH, false); } /** * List items provided to {@link #onLoadFinished(Loader, List)} */ protected List<E> items = Collections.emptyList(); /** * List view */ protected ListView listView; /** * Empty view */ protected TextView emptyView; /** * Progress bar */ protected ProgressBar progressBar; /** * Is the list currently shown? */ protected boolean listShown; @Override public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (!items.isEmpty()) { setListShown(true, false); } getLoaderManager().initLoader(0, null, this); } @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { return inflater.inflate(layout.item_list, null); } /** * Detach from list view. */ @Override public void onDestroyView() { listShown = false; emptyView = null; progressBar = null; listView = null; super.onDestroyView(); } @Override public void onViewCreated(final View view, final Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); listView = (ListView) view.findViewById(android.R.id.list); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { onListItemClick((ListView) parent, view, position, id); } }); progressBar = (ProgressBar) view.findViewById(id.pb_loading); emptyView = (TextView) view.findViewById(android.R.id.empty); configureList(getActivity(), getListView()); } /** * Configure list after view has been created * * @param activity * @param listView */ protected void configureList(final Activity activity, final ListView listView) { listView.setAdapter(createAdapter()); } @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(final Menu optionsMenu, final MenuInflater inflater) { inflater.inflate(R.menu.bootstrap, optionsMenu); } @Override public boolean onOptionsItemSelected(final MenuItem item) { if (!isUsable()) { return false; } switch (item.getItemId()) { case id.refresh: forceRefresh(); return true; case R.id.logout: logout(); return true; default: return super.onOptionsItemSelected(item); } } protected abstract LogoutService getLogoutService(); private void logout() { getLogoutService().logout(new Runnable() { @Override public void run() { // Calling a refresh will force the service to look for a logged in user // and when it finds none the user will be requested to log in again. forceRefresh(); } }); } /** * Force a refresh of the items displayed ignoring any cached items */ protected void forceRefresh() { final Bundle bundle = new Bundle(); bundle.putBoolean(FORCE_REFRESH, true); refresh(bundle); } /** * Refresh the fragment's list */ public void refresh() { refresh(null); } private void refresh(final Bundle args) { if (!isUsable()) { return; } getActionBarActivity().setSupportProgressBarIndeterminateVisibility(true); getLoaderManager().restartLoader(0, args, this); } private ActionBarActivity getActionBarActivity() { return ((ActionBarActivity) getActivity()); } /** * Get error message to display for exception * * @param exception * @return string resource id */ protected abstract int getErrorMessage(final Exception exception); public void onLoadFinished(final Loader<List<E>> loader, final List<E> items) { getActionBarActivity().setSupportProgressBarIndeterminateVisibility(false); final Exception exception = getException(loader); if (exception != null) { showError(getErrorMessage(exception)); showList(); return; } this.items = items; getListAdapter().getWrappedAdapter().setItems(items.toArray()); showList(); } /** * Create adapter to display items * * @return adapter */ protected HeaderFooterListAdapter<SingleTypeAdapter<E>> createAdapter() { final SingleTypeAdapter<E> wrapped = createAdapter(items); return new HeaderFooterListAdapter<SingleTypeAdapter<E>>(getListView(), wrapped); } /** * Create adapter to display items * * @param items * @return adapter */ protected abstract SingleTypeAdapter<E> createAdapter(final List<E> items); /** * Set the list to be shown */ protected void showList() { setListShown(true, isResumed()); } @Override public void onLoaderReset(final Loader<List<E>> loader) { // Intentionally left blank } /** * Show exception in a Toast * * @param message */ protected void showError(final int message) { Toaster.showLong(getActivity(), message); } /** * Get exception from loader if it provides one by being a * {@link ThrowableLoader} * * @param loader * @return exception or null if none provided */ protected Exception getException(final Loader<List<E>> loader) { if (loader instanceof ThrowableLoader) { return ((ThrowableLoader<List<E>>) loader).clearException(); } else { return null; } } /** * Refresh the list with the progress bar showing */ protected void refreshWithProgress() { items.clear(); setListShown(false); refresh(); } /** * Get {@link ListView} * * @return listView */ public ListView getListView() { return listView; } /** * Get list adapter * * @return list adapter */ @SuppressWarnings("unchecked") protected HeaderFooterListAdapter<SingleTypeAdapter<E>> getListAdapter() { if (listView != null) { return (HeaderFooterListAdapter<SingleTypeAdapter<E>>) listView .getAdapter(); } return null; } /** * Set list adapter to use on list view * * @param adapter * @return this fragment */ protected ItemListFragment<E> setListAdapter(final ListAdapter adapter) { if (listView != null) { listView.setAdapter(adapter); } return this; } private ItemListFragment<E> fadeIn(final View view, final boolean animate) { if (view != null) { if (animate) { view.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in)); } else { view.clearAnimation(); } } return this; } private ItemListFragment<E> show(final View view) { ViewUtils.setGone(view, false); return this; } private ItemListFragment<E> hide(final View view) { ViewUtils.setGone(view, true); return this; } /** * Set list shown or progress bar show * * @param shown * @return this fragment */ public ItemListFragment<E> setListShown(final boolean shown) { return setListShown(shown, true); } /** * Set list shown or progress bar show * * @param shown * @param animate * @return this fragment */ public ItemListFragment<E> setListShown(final boolean shown, final boolean animate) { if (!isUsable()) { return this; } if (shown == listShown) { if (shown) { // List has already been shown so hide/show the empty view with // no fade effect if (items.isEmpty()) { hide(listView).show(emptyView); } else { hide(emptyView).show(listView); } } return this; } listShown = shown; if (shown) { if (!items.isEmpty()) { hide(progressBar).hide(emptyView).fadeIn(listView, animate) .show(listView); } else { hide(progressBar).hide(listView).fadeIn(emptyView, animate) .show(emptyView); } } else { hide(listView).hide(emptyView).fadeIn(progressBar, animate) .show(progressBar); } return this; } /** * Set empty text on list fragment * * @param message * @return this fragment */ protected ItemListFragment<E> setEmptyText(final String message) { if (emptyView != null) { emptyView.setText(message); } return this; } /** * Set empty text on list fragment * * @param resId * @return this fragment */ protected ItemListFragment<E> setEmptyText(final int resId) { if (emptyView != null) { emptyView.setText(resId); } return this; } /** * Callback when a list view item is clicked * * @param l * @param v * @param position * @param id */ public void onListItemClick(final ListView l, final View v, final int position, final long id) { } /** * Is this fragment still part of an activity and usable from the UI-thread? * * @return true if usable on the UI-thread, false otherwise */ protected boolean isUsable() { return getActivity() != null; } }