Server code

12.5 Server code

A mobile application often relies on server-side resources, and our Field Service Appli- cation is no exception. Since this is not a book on server-side development tech- niques, server-related code, and discussion, things will be presented briefly and matter of factly. We will introduce the UI and the accompanying database structure that makes up our list of job entries, and then we’ll review the two server-side transactions that concern the Android application.

12.5.1 Dispatcher user interface Before jumping into any server code–specific items, it is important to understand how

the application is organized. All jobs entered by a dispatcher are assigned to a particu- lar mobile technician. That identifier is interpreted as an email address, as seen in the Android example where the user ID was used throughout the application. Once the user ID is specified, all of the records revolve around that data element. For example, figure 12.14 demonstrates this by showing the jobs assigned to the author, fable- son@msiservices.com.

Figure 12.14 The server-side dispatcher screen

Server code

NOTE This application is available for testing the sample application yourself. It is located at http://android12.msi-wireless.com . Simply sign on and add jobs for your email address.

Let’s now turn our attention to the underlying data structure, which contains the list of jobs.

12.5.2 Database As mentioned earlier in the architecture section, the database in use in this applica-

tion is MySQL, with a single database table called tbl_jobs. The SQL to create this table is provided in listing 12.20.

Listing 12.20 Data definition for tbl_jobs

CREATE TABLE IF NOT EXISTS 'tbl_jobs' ( 'jobid' int(11) NOT NULL auto_increment,

B Unique record id

'status' varchar(10) NOT NULL default 'OPEN', 'identifier' varchar(50) NOT NULL,

C User identification

'address' varchar(50) NOT NULL, 'city' varchar(30) NOT NULL, 'state' varchar(2) NOT NULL, 'zip' varchar(10) NOT NULL, 'customer' varchar(50) NOT NULL, 'product' varchar(50) NOT NULL, 'producturl' varchar(100) NOT NULL,

D Product URL

'comments' varchar(100) NOT NULL, UNIQUE KEY 'jobid' ('jobid')

) ENGINE=MyISAM DEFAULT CHARSET=ascii AUTO_INCREMENT=25 ;

Each row in this table is uniquely identified by the jobid B , which is an auto-incre- menting integer field. The identifier field C corresponds to the user ID/email of the assigned mobile technician. The producturl field D is designed to be a specific

URL to assist the mobile technician in the field to quickly gain access to helpful infor- mation to assist in completing the assigned job.

The next section provides a road map to the server code.

12.5.3 PHP dispatcher code The server-side dispatcher system is written in PHP and contains a number of files

working together to create the application. Table 12.3 presents a brief synopsis of each source file to help you navigate the application should you choose to host a version of this application yourself.

Table 12.3 Server-side source code Source File

Description

addjob.php Form for entering new job information closejob.php

Used by Android application to submit signature db.php

Database connection info

C HAPTER 12 Putting it all together–the Field Service Application

Table 12.3 Server-side source code (continued) Source File

Description

export.php

Used to export list of jobs to a csv file

footer.php Used to create a consistent look and feel for the footer of each page getjoblist.php

Used by Android application to request job XML stream

header.php Used to create a consistent look and feel for the header of each page index.php

Home page, including search form

manage.php

Used to delete jobs on the web application

savejob.php

Used to save a new job (called from addjob.php)

showjob.php Used to display job details and load into a form for updating showjobs.php

Displays all jobs for a particular user

updatejob.php

Used to save updates to a job

utils.php Contains various routines for interacting with the database

Of all of these files, only two actually concern the Android application. These are dis- cussed in the next section.

12.5.4 PHP mobile integration code When the Android application runs the RefreshJobs Activity, the server side gener-

ates an XML stream. Without going into excessive detail on the server-side code, the getjoblist.php file is explained in listing 12.21.

Listing 12.21 getjoblist.php

Database routines

require('db.php');

Extract the

require('utils.php');

Helper routines

user identifier

$theuser = $_GET['identifier']; print (getJobsXML($theuser));

Build list of jobs

B for this user

The getJobsXML B function retrieves data from the database and formats each row

into an XML representation. It wraps the list of XML-wrapped job records in the <job- list> tags along with the <?xml ...> header declaration to generate the expected XML structure used by the Android application. Remember, this is the data ultimately parsed by the SAX-based JobListHandler class, as shown in listing 12.12.

The other transaction that is important to our Android Field Service Application is the closejob.php file, examined in listing 12.22.

Listing 12.22 closejob.php

<? require('db.php');

Summary

require('utils.php');

B Read in image data

$data = file_get_contents('php://input'); $jobid = $_GET['jobid'];

C Get the job ID

$f = fopen("~/pathtofiles/sigs/".$jobid.".jpg","w"); fwrite($f,$data);

Write out the

fclose($f);

D image data

print(closeJob($_GET['jobid']));

E Close the job

The POST-ed image data is read via the file_get_contents() function B . The secret

is the special identifier of php://input. This is the equivalent of a binary read. This data is read into a variable named $data. The jobid is extracted from the query

String C . The image file is written out to a directory that contains signatures as JPEG files, keyed by the jobid as part of the filename D . When a job has been closed and

the signature is requested by the Android application, it is this file that is requested in

the Android browser. The closeJob function E (implemented in utils.php) updates

the database to mark the selected job as CLOSED. That wraps up the review of the source code for this chapter’s sample application.