<commit_before>package com.sixsq.slipstream.user;

/*
 * +=================================================================+
 * SlipStream Server (WAR)
 * =====
 * Copyright (C) 2013 SixSq Sarl (sixsq.com)
 * =====
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * -=================================================================-
 */

import org.restlet.data.Form;
import org.restlet.data.Status;
import org.restlet.resource.ResourceException;

import com.sixsq.slipstream.exceptions.NotFoundException;
import com.sixsq.slipstream.exceptions.SlipStreamClientException;
import com.sixsq.slipstream.exceptions.ValidationException;
import com.sixsq.slipstream.persistence.User;
import com.sixsq.slipstream.persistence.User.State;
import com.sixsq.slipstream.persistence.UserParameter;

/**
 * For tests documentation
 * 
 * @see UserFormProcessorTest
 * 
 */
public class UserFormProcessor extends FormProcessor<User, UserParameter> {

	public UserFormProcessor(User user) {
		super(user);
	}

	public UserFormProcessor(User parametrized, User user) {
		super(user);
		setParametrized(parametrized);
	}

	@Override
	protected void parseForm() throws ValidationException, NotFoundException {

		super.parseForm();

		String name = getForm().getFirstValue("name");

		setParametrized(getOrCreateParameterized(name));
		User user = (User) getParametrized();
		user.setName(name);

		user.setFirstName(getForm().getFirstValue("firstname"));
		user.setLastName(getForm().getFirstValue("lastname"));
		user.setEmail(getForm().getFirstValue("email"));
		parseState(getForm(), user);

		User dbUser = User.loadByName(name);

		processPassword(getForm(), dbUser);
		processIsSuper(getForm());
	}

	private void parseState(Form form, User user) {
		String state = form.getFirstValue("state");
		if (state != null) {
			user.setState(State.valueOf(state));
		}
	}

	@Override
	protected User getOrCreateParameterized(String name)
			throws ValidationException {
		User user = getParametrized();
		if (user == null) {
			user = new User(name);
		}
		return user;
	}

	@Override
	protected UserParameter createParameter(String name, String value,
			String description) throws SlipStreamClientException {<commit_msg>Added missing organization<commit_after>package com.sixsq.slipstream.user;

/*
 * +=================================================================+
 * SlipStream Server (WAR)
 * =====
 * Copyright (C) 2013 SixSq Sarl (sixsq.com)
 * =====
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * -=================================================================-
 */

import org.restlet.data.Form;
import org.restlet.data.Status;
import org.restlet.resource.ResourceException;

import com.sixsq.slipstream.exceptions.NotFoundException;
import com.sixsq.slipstream.exceptions.SlipStreamClientException;
import com.sixsq.slipstream.exceptions.ValidationException;
import com.sixsq.slipstream.persistence.User;
import com.sixsq.slipstream.persistence.User.State;
import com.sixsq.slipstream.persistence.UserParameter;

/**
 * For tests documentation
 * 
 * @see UserFormProcessorTest
 * 
 */
public class UserFormProcessor extends FormProcessor<User, UserParameter> {

	public UserFormProcessor(User user) {
		super(user);
	}

	public UserFormProcessor(User parametrized, User user) {
		super(user);
		setParametrized(parametrized);
	}

	@Override
	protected void parseForm() throws ValidationException, NotFoundException {

		super.parseForm();

		String name = getForm().getFirstValue("name");

		setParametrized(getOrCreateParameterized(name));
		User user = (User) getParametrized();
		user.setName(name);

		user.setFirstName(getForm().getFirstValue("firstname"));
		user.setLastName(getForm().getFirstValue("lastname"));
		user.setEmail(getForm().getFirstValue("email"));
		user.setOrganization(getForm().getFirstValue("organization"));
		parseState(getForm(), user);

		User dbUser = User.loadByName(name);

		processPassword(getForm(), dbUser);
		processIsSuper(getForm());
	}

	private void parseState(Form form, User user) {
		String state = form.getFirstValue("state");
		if (state != null) {
			user.setState(State.valueOf(state));
		}
	}

	@Override
	protected User getOrCreateParameterized(String name)
			throws ValidationException {
		User user = getParametrized();
		if (user == null) {
			user = new User(name);
		}
		return user;
	}

	@Override
	protected UserParameter createParameter(String name, String value,
			String description) throws SlipStreamClientException {
{'fraction_non_alphanumeric': 0.15383218307301125, 'fraction_numerical': 0.002905920813657828, 'mean_word_length': 5.229638009049774, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5936356', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1535, 'n_words': 401}