ItEr10S08AdministracionGruposItEr09S09: Link for editing worker from LocationGroups. It uses matrix parameters for specifying the worker.

This commit is contained in:
Óscar González Fernández 2009-05-26 12:32:54 +02:00 committed by Javier Moran Rua
parent 6af12a9eba
commit 38f05bfc80
9 changed files with 182 additions and 2 deletions

View file

@ -0,0 +1,33 @@
package org.navalplanner.web.common;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
/**
* Extracts <a href="http://www.w3.org/DesignIssues/MatrixURIs.html">matrix
* parameters</a>. <br />
* @author Óscar González Fernández <ogonzalez@igalia.com>
*/
public class MatrixParameters {
private static Pattern matrixParamPattern = Pattern
.compile(";([^/=;]+)=?([^/;]+)?");
public static Map<String, String> extract(HttpServletRequest request) {
return extract(request.getRequestURI());
}
public static Map<String, String> extract(String string) {
Map<String, String> result = new HashMap<String, String>();
Matcher matcher = matrixParamPattern.matcher(string);
while (matcher.find()) {
result.put(matcher.group(1), matcher.group(2));
}
return result;
}
}

View file

@ -59,6 +59,12 @@ public class CriterionWorkersController extends GenericForwardComposer {
return criterionsModel.isChangeAssignmentsDisabled();
}
public void goToEditPage(Resource resource) {
System.out.println("going to edit page for: " + resource);
desktop.getExecution().sendRedirect(
"/resources/worker/worker.zul;edit=" + resource.getId());
}
@Override
public void doAfterCompose(final Component comp) throws Exception {
super.doAfterCompose(comp);

View file

@ -29,4 +29,6 @@ public interface IWorkerModel {
Set<CriterionSatisfaction> getCriterionSatisfactions(Worker worker);
Worker findResource(long workerId);
}

View file

@ -1,12 +1,16 @@
package org.navalplanner.web.resources.worker;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.validator.InvalidValue;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.resources.entities.Worker;
import org.navalplanner.web.common.IMessagesForUser;
import org.navalplanner.web.common.Level;
import org.navalplanner.web.common.MatrixParameters;
import org.navalplanner.web.common.MessagesForUser;
import org.navalplanner.web.common.OnlyOneVisible;
import org.navalplanner.web.common.Util;
@ -119,6 +123,15 @@ public class WorkerCRUDController extends GenericForwardComposer {
if (messagesContainer == null)
throw new RuntimeException("messagesContainer is needed");
messages = new MessagesForUser(messagesContainer);
Map<String, String> matrixParameters = MatrixParameters
.extract((HttpServletRequest) execution.getNativeRequest());
if (matrixParameters.containsKey("create")) {
goToCreateForm();
} else if (matrixParameters.containsKey("edit")) {
goToEditForm(workerModel.findResource(Long
.parseLong(matrixParameters.get("edit"))));
}
}
private LocalizationsController createLocalizationsController(

View file

@ -284,4 +284,13 @@ public class WorkerModel implements IWorkerModel {
return worker != null && worker.getId() == null;
}
@Override
public Worker findResource(long workerId) {
try {
return (Worker) resourceService.findResource(workerId);
} catch (InstanceNotFoundException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -2,12 +2,14 @@
<grid model="@{controller.workers.workersForCurrentCriterion}"
mold="paging" pageSize="5" apply="${controller.workers}">
<columns>
<column label="First Name" sort="auto(firstName)" />
<column label="Surname" sort="auto(surname)" />
<column label="Operacións" />
<column label="Nome" sort="auto(firstName)" />
<column label="Apellidos" sort="auto(surname)" />
<column label="nif" sort="auto(nif)" />
</columns>
<rows>
<row self="@{each='worker'}" value="@{worker}">
<button label="Edición" onClick="controller.workers.goToEditPage(self.getParent().getValue());"></button>
<label value="@{worker.firstName}" />
<label value="@{worker.surname}" />
<label value="@{worker.nif}" />

View file

@ -0,0 +1,40 @@
package org.navalplanner.web.common;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import java.util.Map;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
/**
* Tests that {@link MatrixParameters} supports extracting several
* matrix parameters <br />
* @author Óscar González Fernández <ogonzalez@igalia.com>
*/
@RunWith(Theories.class)
public class MatrixParametersSupportSeveralParameters {
@DataPoint
public static String TWO_PARAMETERS_IN_THE_END = "/prueba/prueba.php;color=red;size=2";
@DataPoint
public static String TWO_PARAMETERS_IN_THE_MIDDLE = "/prueba;color=red;size=2/prueba.php";
@DataPoint
public static String TWO_PARAMETERS_IN_DIFFERENT_LOCATIONS = "/prueba;color=red/prueba.php;size=2";
@Theory
public void oneParameterEqualToRed(String parameter) {
Map<String, String> params = MatrixParameters
.extract(parameter);
assertThat(params.size(), equalTo(2));
assertThat(params.get("color"), equalTo("red"));
assertThat(params.get("size"), equalTo("2"));
}
}

View file

@ -0,0 +1,38 @@
package org.navalplanner.web.common;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import java.util.Map;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
/**
* Test that {@link MatrixParameters} support one parameter<br />
* @author Óscar González Fernández <ogonzalez@igalia.com>
*/
@RunWith(Theories.class)
public class MatrixParametersSupportsOneParameterTheory {
@DataPoint
public static String ONE_PARAMETER_IN_THE_END = "/prueba/prueba.php;color=red";
@DataPoint
public static String ONE_PARAMETER_IN_THE_MIDDLE = "/prueba;color=red/prueba.php";
@DataPoint
public static String ONE_PARAMETER_IN_THE_MIDDLE_WITH_SEMICOLON_AFTER = "/prueba;color=red;/prueba.php";
@Theory
public void oneParameterEqualToRed(String parameter) {
Map<String, String> params = MatrixParameters
.extract(parameter);
assertThat(params.size(), equalTo(1));
assertThat(params.keySet().iterator().next(), equalTo("color"));
assertThat(params.get("color"), equalTo("red"));
}
}

View file

@ -0,0 +1,37 @@
package org.navalplanner.web.common;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
/**
* Tests that {@link MatrixParameters} supports matrix parameters
* without value <br />
* @author Óscar González Fernández <ogonzalez@igalia.com>
*/
@RunWith(Theories.class)
public class MatrixParametersSupportsParametersWithoutValue {
@DataPoint
public static String ONE_PARAMETER_WITHOUT_VALUE_AT_THE_END = "/blabalba/eo/prueba;create";
@DataPoint
public static String ONE_PARAMETER_WITHOUT_VALUE_IN_THE_MIDDLE = "/blabalba;create/eo/prueba";
@Theory
public void testTheory(String parameter) {
Map<String, String> map = MatrixParameters.extract(parameter);
assertThat(map.size(), equalTo(1));
assertTrue(map.containsKey("create"));
assertNull(map.get("create"));
}
}