@Test
  public void should_fail_to_persist_if_conceptSetRow_introduces_cycle() throws Exception {
    ConceptSetRow row1 = new ConceptSetRow();
    row1.name = "ConceptA";
    row1.conceptClass = "New Class";
    row1.description = "some description";
    List<KeyValue> children = new ArrayList<>();
    children.add(new KeyValue("1", "Child1"));
    children.add(new KeyValue("2", "Child2"));
    row1.children = children;

    Messages persistErrorMessages = conceptSetPersister.persist(row1);
    assertTrue(persistErrorMessages.isEmpty());
    Context.openSession();
    Context.authenticate("admin", "test");
    Concept persistedConcept = conceptService.getConceptByName(row1.name);
    assertNotNull(persistedConcept);

    ConceptSetRow row2 = new ConceptSetRow();
    row2.name = "Child2";
    row2.conceptClass = "New Class";
    row2.description = "some description";
    List<KeyValue> children1 = new ArrayList<>();
    children1.add(new KeyValue("1", "ConceptA"));
    children1.add(new KeyValue("2", "Child3"));
    row2.children = children1;

    Messages persistErrorMessages1 = conceptSetPersister.persist(row2);
    assertFalse(persistErrorMessages1.isEmpty());

    Context.flushSession();
    Context.closeSession();
  }
  @Test
  public void addSetMembers() throws Exception {
    ConceptSetRow conceptRow = new ConceptSetRow();
    conceptRow.name = "New concept";
    conceptRow.conceptClass = "New Class";
    conceptRow.description = "some description";

    List<KeyValue> children = new ArrayList<>();
    children.add(new KeyValue("1", "Child1"));
    children.add(new KeyValue("2", "Child2"));
    conceptRow.children = children;
    Messages persistErrorMessages = conceptSetPersister.persist(conceptRow);
    assertTrue(persistErrorMessages.isEmpty());
    Context.openSession();
    Context.authenticate("admin", "test");
    Concept persistedConcept = conceptService.getConceptByName(conceptRow.name);
    assertNotNull(persistedConcept);
    assertEquals(conceptRow.name, persistedConcept.getName(Context.getLocale()).getName());
    assertEquals(conceptRow.conceptClass, persistedConcept.getConceptClass().getName());
    assertEquals("some description", persistedConcept.getDescription().getDescription());

    assertEquals(2, persistedConcept.getSetMembers().size());
    assertEquals("some description", persistedConcept.getDescription().getDescription());
    assertEquals(0, persistedConcept.getSynonyms().size());
    assertTrue(persistedConcept.isSet());
    Context.flushSession();
    Context.closeSession();
  }
 @Test
 public void shouldPersistNewConceptSetWithNameClassDescriptionInputOnly() throws Exception {
   ConceptSetRow conceptRow = new ConceptSetRow();
   conceptRow.name = "New concept";
   conceptRow.conceptClass = "New Class";
   conceptRow.description = "some description";
   Messages persistErrorMessages = conceptSetPersister.persist(conceptRow);
   assertTrue(persistErrorMessages.isEmpty());
   Context.openSession();
   Context.authenticate("admin", "test");
   Concept persistedConcept = conceptService.getConceptByName(conceptRow.name);
   assertNotNull(persistedConcept);
   assertEquals(conceptRow.name, persistedConcept.getName(Context.getLocale()).getName());
   assertEquals(conceptRow.conceptClass, persistedConcept.getConceptClass().getName());
   assertEquals("some description", persistedConcept.getDescription().getDescription());
   assertEquals(0, persistedConcept.getSynonyms().size());
   assertTrue(persistedConcept.isSet());
   Context.flushSession();
   Context.closeSession();
 }
  private void loadPatientButtonActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_loadPatientButtonActionPerformed
    try {
      String url =
          "jdbc:mysql://localhost:3306/gold18?autoReconnect=true&"
              + "sessionVariables=storage_engine=InnoDB&useUnicode=true&characterEncoding=UTF-8";
      String username = "******";
      String password = "******";

      Properties properties = new Properties();
      properties.setProperty("module.ignore_core_status", "true");

      Context.startup(url, username, password, properties);
      Context.openSession();
      Context.authenticate("admin", "test");

      Patient patient = Context.getPatientService().getPatient(6);

      firstNameField.setText(patient.getGivenName());
      middleNameField.setText(patient.getMiddleName());
      lastNameField.setText(patient.getFamilyName());

      for (int i = 0; i < genderComboBox.getItemCount(); i++) {
        Object element = genderComboBox.getItemAt(i);
        if (StringUtils.startsWith(element.toString(), patient.getGender()))
          genderComboBox.setSelectedIndex(i);
      }

      DateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy");
      dobTextField.setText(dateFormat.format(patient.getBirthdate()));

      Context.closeSession();
    } catch (DatabaseUpdateException e) {
      e.printStackTrace();
    } catch (InputRequiredException e) {
      e.printStackTrace();
    }
  } // GEN-LAST:event_loadPatientButtonActionPerformed
 @Before
 public void setUp() throws Exception {
   Context.authenticate("admin", "test");
   executeDataSet("conceptSetup.xml");
 }
 public void authenticate(String username, String password) {
   Context.authenticate(username, password);
 }
  @RequestMapping(method = RequestMethod.POST)
  public String loginUser(
      ModelMap model,
      HttpSession session,
      HttpServletRequest request,
      HttpServletResponse response) {

    String redirect = null;

    try {
      String username = request.getParameter("uname");
      String password = request.getParameter("pw");

      // get the place to redirect: for touch screen this is simple
      redirect = determineRedirect(request);

      // only try to authenticate if they actually typed in a username
      if (username != null && username.length() > 0) {

        Context.authenticate(username, password);

        if (Context.isAuthenticated()) {

          User user = Context.getAuthenticatedUser();

          if (user.getUserProperties() != null
              && !user.getUserProperties().containsKey("keyboardType")) {
            user.getUserProperties().put("keyboardType", "QWERTY"); // ABC is the other option
            user = Context.getUserService().saveUser(user, null);
          }
          session.setAttribute("keyboardType", user.getUserProperty("keyboardType"));

          // load the user's default locale if possible
          if (user.getUserProperties() != null) {
            if (user.getUserProperties()
                .containsKey(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE)) {
              String localeString =
                  user.getUserProperty(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE);
              Locale locale = null;
              if (localeString.length() == 5) {
                // user's locale is language_COUNTRY (i.e. en_US)
                String lang = localeString.substring(0, 2);
                String country = localeString.substring(3, 5);
                locale = new Locale(lang, country);
              } else {
                // user's locale is only the language (language plus greater than 2 char country
                // code
                locale = new Locale(localeString);
              }
              OpenmrsCookieLocaleResolver oclr = new OpenmrsCookieLocaleResolver();
              oclr.setLocale(request, response, locale);
            }
          }

          // In case the user has no preferences, make sure that the context has some locale set
          if (Context.getLocale() == null) {
            Context.setLocale(OpenmrsConstants.GLOBAL_DEFAULT_LOCALE);
          }
        }

        if (log.isDebugEnabled()) {
          log.debug("Redirecting after login to: " + redirect);
          log.debug("Locale address: " + request.getLocalAddr());
        }

        response.sendRedirect(redirect);

        // return redirect;
      }
    } catch (ContextAuthenticationException e) {
      // set the error message for the user telling them
      // to try again
      // session.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "auth.password.invalid");
      log.error("failed to authenticate: ", e);
    } catch (Exception e) {
      log.error("Uexpected auth error", e);
    }

    // send the user back the login page because they either
    // had a bad password or are locked out

    session.setAttribute(
        WebConstants.OPENMRS_MSG_ATTR,
        Context.getMessageSourceService().getMessage("rwandaprimarycare.loginFailed"));
    session.setAttribute(WebConstants.OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR, redirect);

    return "/module/rwandaprimarycare/login";
  }