Last active
December 14, 2015 23:09
-
-
Save kastork/5163454 to your computer and use it in GitHub Desktop.
Liferay Batch user addition using Groovy and the Liferay JSON-WS API
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
// We assume that the input file 'users.csv' is a three field | |
// comma separated list of users | |
// John Q. User, john@example.com, password_ofjohn | |
// | |
// Password can be empty, but the csv still needs a field there | |
// | |
// John Q. User, john@example.com, | |
// (note the trailing comma) | |
// | |
// The name field must have at least two tokens. If there are three or more | |
// tokens, then the first is used as first name, the last is used as last name | |
// and everything else is used as the middle name. | |
// | |
// This uses the batch invoker documented here | |
// http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/json-web-services | |
// | |
// If any user record from the csv causes an exception in Liferay, the whole batch fails. | |
// | |
@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='0.7.2') | |
@Grab('com.xlson.groovycsv:groovycsv:1.0') | |
import static com.xlson.groovycsv.CsvParser.parseCsv | |
import wslite.rest.* | |
import wslite.http.auth.* | |
import groovy.json.JsonBuilder | |
println "This is the add user script" | |
def csv = new File('users.csv').text | |
println csv | |
def data = parseCsv(csv, readFirstLine:true, | |
columnNames:['name', 'email', 'pwd']) | |
def client = new RESTClient("http://localhost:8080/api/secure/jsonws/") | |
client.authorization = new HTTPBasicAuthorization("test@liferay.com", "test") | |
client.httpClient.sslTrustAllCerts = true | |
def queryTemplate = [ | |
companyId: '10154', | |
autoPassword:'true', | |
password1:'', | |
password2:'', | |
autoScreenName:'true', | |
screenName:'', | |
emailAddress:'testy@example.com', | |
facebookId:'0', | |
openId:'', | |
locale:'en', | |
firstName:'Testy', | |
middleName:'the', | |
lastName:'Tester', | |
prefixId:'0', | |
suffixId:'0', | |
male:'true', | |
birthdayMonth:'0', | |
birthdayDay:'1', | |
birthdayYear:'1970', | |
jobTitle:'', | |
groupIds:'', | |
organizationIds:'', | |
roleIds:'', | |
userGroupIds:'', | |
sendEmail:'false' | |
] | |
def batchData = [] | |
for(line in data) { | |
def newUserQuery = queryTemplate.clone() | |
def names = line.name.split(' ') | |
def fname = names[0] | |
def lname = names[-1] | |
def mname = "" | |
if(names.length > 2) { | |
mname = names[1..-2].join(' ') | |
} | |
println "$fname : $mname : $lname" | |
newUserQuery.emailAddress = line.email | |
newUserQuery.firstName = fname | |
newUserQuery.middleName = mname | |
newUserQuery.lastName = lname | |
if(line.pwd == '') { | |
newUserQuery.autoPassword='true' | |
} else { | |
newUserQuery.autoPassword='false' | |
} | |
newUserQuery.password1 = line.pwd | |
newUserQuery.password2 = line.pwd | |
batchData << ["/user/add-user":newUserQuery] | |
} | |
JsonBuilder builder = new JsonBuilder() | |
builder(batchData) | |
def response = client.post( path:'/invoke') { | |
type "text/plain" | |
charset "utf-8" | |
text builder.toString() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment