@Test
  public void testFetchByPrimaryKeyExisting() throws Exception {
    Address newAddress = addAddress();

    Address existingAddress = _persistence.fetchByPrimaryKey(newAddress.getPrimaryKey());

    Assert.assertEquals(existingAddress, newAddress);
  }
  @Test
  public void testRemove() throws Exception {
    Address newAddress = addAddress();

    _persistence.remove(newAddress);

    Address existingAddress = _persistence.fetchByPrimaryKey(newAddress.getPrimaryKey());

    Assert.assertNull(existingAddress);
  }
  @Test
  public void testCreate() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Address address = _persistence.create(pk);

    Assert.assertNotNull(address);

    Assert.assertEquals(address.getPrimaryKey(), pk);
  }
  @Override
  public Address getAddress(long addressId) throws PortalException, SystemException {

    Address address = addressPersistence.findByPrimaryKey(addressId);

    CommonPermissionUtil.check(
        getPermissionChecker(), address.getClassNameId(), address.getClassPK(), ActionKeys.VIEW);

    return address;
  }
  @Override
  public void deleteAddress(long addressId) throws PortalException, SystemException {

    Address address = addressPersistence.findByPrimaryKey(addressId);

    CommonPermissionUtil.check(
        getPermissionChecker(), address.getClassNameId(), address.getClassPK(), ActionKeys.UPDATE);

    addressLocalService.deleteAddress(address);
  }
  @Test
  public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
    Address newAddress = addAddress();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newAddress.getPrimaryKey());

    Map<Serializable, Address> addresses = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, addresses.size());
    Assert.assertEquals(newAddress, addresses.get(newAddress.getPrimaryKey()));
  }
Exemple #7
0
  public static com.liferay.portal.model.Address update(com.liferay.portal.model.Address address)
      throws com.liferay.portal.SystemException {
    AddressPersistence persistence = (AddressPersistence) InstancePool.get(PERSISTENCE);
    ModelListener listener = null;

    if (Validator.isNotNull(LISTENER)) {
      try {
        listener = (ModelListener) Class.forName(LISTENER).newInstance();
      } catch (Exception e) {
        _log.error(e.getMessage());
      }
    }

    boolean isNew = address.isNew();

    if (listener != null) {
      if (isNew) {
        listener.onBeforeCreate(address);
      } else {
        listener.onBeforeUpdate(address);
      }
    }

    address = persistence.update(address);

    if (listener != null) {
      if (isNew) {
        listener.onAfterCreate(address);
      } else {
        listener.onAfterUpdate(address);
      }
    }

    return address;
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    Address newAddress = addAddress();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(Address.class, _dynamicQueryClassLoader);

    dynamicQuery.add(RestrictionsFactoryUtil.eq("addressId", newAddress.getAddressId()));

    List<Address> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Address existingAddress = result.get(0);

    Assert.assertEquals(existingAddress, newAddress);
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
      throws Exception {
    Address newAddress = addAddress();

    long pk = RandomTestUtil.nextLong();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newAddress.getPrimaryKey());
    primaryKeys.add(pk);

    Map<Serializable, Address> addresses = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, addresses.size());
    Assert.assertEquals(newAddress, addresses.get(newAddress.getPrimaryKey()));
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    Address newAddress = addAddress();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(Address.class, _dynamicQueryClassLoader);

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("addressId"));

    Object newAddressId = newAddress.getAddressId();

    dynamicQuery.add(RestrictionsFactoryUtil.in("addressId", new Object[] {newAddressId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Object existingAddressId = result.get(0);

    Assert.assertEquals(existingAddressId, newAddressId);
  }
  @Override
  public Address updateAddress(
      long addressId,
      String street1,
      String street2,
      String street3,
      String city,
      String zip,
      long regionId,
      long countryId,
      int typeId,
      boolean mailing,
      boolean primary)
      throws PortalException, SystemException {

    Address address = addressPersistence.findByPrimaryKey(addressId);

    CommonPermissionUtil.check(
        getPermissionChecker(), address.getClassNameId(), address.getClassPK(), ActionKeys.UPDATE);

    return addressLocalService.updateAddress(
        addressId, street1, street2, street3, city, zip, regionId, countryId, typeId, mailing,
        primary);
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
      throws Exception {
    Address newAddress1 = addAddress();
    Address newAddress2 = addAddress();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newAddress1.getPrimaryKey());
    primaryKeys.add(newAddress2.getPrimaryKey());

    Map<Serializable, Address> addresses = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(2, addresses.size());
    Assert.assertEquals(newAddress1, addresses.get(newAddress1.getPrimaryKey()));
    Assert.assertEquals(newAddress2, addresses.get(newAddress2.getPrimaryKey()));
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, Address address)
      throws Exception {

    long userId = portletDataContext.getUserId(address.getUserUuid());

    ServiceContext serviceContext = portletDataContext.createServiceContext(address);

    Address existingAddress =
        AddressLocalServiceUtil.fetchAddressByUuidAndCompanyId(
            address.getUuid(), portletDataContext.getCompanyId());

    Address importedAddress = null;

    if (existingAddress == null) {
      serviceContext.setUuid(address.getUuid());

      importedAddress =
          AddressLocalServiceUtil.addAddress(
              userId,
              address.getClassName(),
              address.getClassPK(),
              address.getStreet1(),
              address.getStreet2(),
              address.getStreet3(),
              address.getCity(),
              address.getZip(),
              address.getRegionId(),
              address.getCountryId(),
              address.getTypeId(),
              address.getMailing(),
              address.isPrimary(),
              serviceContext);
    } else {
      importedAddress =
          AddressLocalServiceUtil.updateAddress(
              existingAddress.getAddressId(),
              address.getStreet1(),
              address.getStreet2(),
              address.getStreet3(),
              address.getCity(),
              address.getZip(),
              address.getRegionId(),
              address.getCountryId(),
              address.getTypeId(),
              address.getMailing(),
              address.isPrimary());
    }

    portletDataContext.importClassedModel(address, importedAddress);
  }
  protected Address addAddress() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Address address = _persistence.create(pk);

    address.setMvccVersion(RandomTestUtil.nextLong());

    address.setUuid(RandomTestUtil.randomString());

    address.setCompanyId(RandomTestUtil.nextLong());

    address.setUserId(RandomTestUtil.nextLong());

    address.setUserName(RandomTestUtil.randomString());

    address.setCreateDate(RandomTestUtil.nextDate());

    address.setModifiedDate(RandomTestUtil.nextDate());

    address.setClassNameId(RandomTestUtil.nextLong());

    address.setClassPK(RandomTestUtil.nextLong());

    address.setStreet1(RandomTestUtil.randomString());

    address.setStreet2(RandomTestUtil.randomString());

    address.setStreet3(RandomTestUtil.randomString());

    address.setCity(RandomTestUtil.randomString());

    address.setZip(RandomTestUtil.randomString());

    address.setRegionId(RandomTestUtil.nextLong());

    address.setCountryId(RandomTestUtil.nextLong());

    address.setTypeId(RandomTestUtil.nextLong());

    address.setMailing(RandomTestUtil.randomBoolean());

    address.setPrimary(RandomTestUtil.randomBoolean());

    address.setLastPublishDate(RandomTestUtil.nextDate());

    _addresses.add(_persistence.update(address));

    return address;
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Address newAddress = _persistence.create(pk);

    newAddress.setMvccVersion(RandomTestUtil.nextLong());

    newAddress.setUuid(RandomTestUtil.randomString());

    newAddress.setCompanyId(RandomTestUtil.nextLong());

    newAddress.setUserId(RandomTestUtil.nextLong());

    newAddress.setUserName(RandomTestUtil.randomString());

    newAddress.setCreateDate(RandomTestUtil.nextDate());

    newAddress.setModifiedDate(RandomTestUtil.nextDate());

    newAddress.setClassNameId(RandomTestUtil.nextLong());

    newAddress.setClassPK(RandomTestUtil.nextLong());

    newAddress.setStreet1(RandomTestUtil.randomString());

    newAddress.setStreet2(RandomTestUtil.randomString());

    newAddress.setStreet3(RandomTestUtil.randomString());

    newAddress.setCity(RandomTestUtil.randomString());

    newAddress.setZip(RandomTestUtil.randomString());

    newAddress.setRegionId(RandomTestUtil.nextLong());

    newAddress.setCountryId(RandomTestUtil.nextLong());

    newAddress.setTypeId(RandomTestUtil.nextLong());

    newAddress.setMailing(RandomTestUtil.randomBoolean());

    newAddress.setPrimary(RandomTestUtil.randomBoolean());

    newAddress.setLastPublishDate(RandomTestUtil.nextDate());

    _addresses.add(_persistence.update(newAddress));

    Address existingAddress = _persistence.findByPrimaryKey(newAddress.getPrimaryKey());

    Assert.assertEquals(existingAddress.getMvccVersion(), newAddress.getMvccVersion());
    Assert.assertEquals(existingAddress.getUuid(), newAddress.getUuid());
    Assert.assertEquals(existingAddress.getAddressId(), newAddress.getAddressId());
    Assert.assertEquals(existingAddress.getCompanyId(), newAddress.getCompanyId());
    Assert.assertEquals(existingAddress.getUserId(), newAddress.getUserId());
    Assert.assertEquals(existingAddress.getUserName(), newAddress.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingAddress.getCreateDate()),
        Time.getShortTimestamp(newAddress.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingAddress.getModifiedDate()),
        Time.getShortTimestamp(newAddress.getModifiedDate()));
    Assert.assertEquals(existingAddress.getClassNameId(), newAddress.getClassNameId());
    Assert.assertEquals(existingAddress.getClassPK(), newAddress.getClassPK());
    Assert.assertEquals(existingAddress.getStreet1(), newAddress.getStreet1());
    Assert.assertEquals(existingAddress.getStreet2(), newAddress.getStreet2());
    Assert.assertEquals(existingAddress.getStreet3(), newAddress.getStreet3());
    Assert.assertEquals(existingAddress.getCity(), newAddress.getCity());
    Assert.assertEquals(existingAddress.getZip(), newAddress.getZip());
    Assert.assertEquals(existingAddress.getRegionId(), newAddress.getRegionId());
    Assert.assertEquals(existingAddress.getCountryId(), newAddress.getCountryId());
    Assert.assertEquals(existingAddress.getTypeId(), newAddress.getTypeId());
    Assert.assertEquals(existingAddress.getMailing(), newAddress.getMailing());
    Assert.assertEquals(existingAddress.getPrimary(), newAddress.getPrimary());
    Assert.assertEquals(
        Time.getShortTimestamp(existingAddress.getLastPublishDate()),
        Time.getShortTimestamp(newAddress.getLastPublishDate()));
  }
  private void createAccount(WebForm form, HttpServletRequest request) throws Exception {

    User user =
        APILocator.getUserAPI()
            .loadByUserByEmail(form.getEmail(), APILocator.getUserAPI().getSystemUser(), false);
    User defaultUser = APILocator.getUserAPI().getDefaultUser();
    Date today = new Date();

    if (user.isNew() || (!user.isNew() && user.getLastLoginDate() == null)) {

      // ### CREATE USER ###
      Company company = PublicCompanyFactory.getDefaultCompany();
      user.setEmailAddress(form.getEmail().trim().toLowerCase());
      user.setFirstName(form.getFirstName() == null ? "" : form.getFirstName());
      user.setMiddleName(form.getMiddleName() == null ? "" : form.getMiddleName());
      user.setLastName(form.getLastName() == null ? "" : form.getLastName());
      user.setNickName("");
      user.setCompanyId(company.getCompanyId());
      user.setPasswordEncrypted(true);
      user.setGreeting("Welcome, " + user.getFullName() + "!");

      // Set defaults values
      if (user.isNew()) {
        // if it's a new user we set random password
        String pass = PublicEncryptionFactory.getRandomPassword();
        user.setPassword(PublicEncryptionFactory.digestString(pass));
        user.setLanguageId(defaultUser.getLanguageId());
        user.setTimeZoneId(defaultUser.getTimeZoneId());
        user.setSkinId(defaultUser.getSkinId());
        user.setDottedSkins(defaultUser.isDottedSkins());
        user.setRoundedSkins(defaultUser.isRoundedSkins());
        user.setResolution(defaultUser.getResolution());
        user.setRefreshRate(defaultUser.getRefreshRate());
        user.setLayoutIds("");
        user.setActive(true);
        user.setCreateDate(today);
      }
      APILocator.getUserAPI().save(user, APILocator.getUserAPI().getSystemUser(), false);
      // ### END CREATE USER ###

      // ### CREATE USER_PROXY ###
      UserProxy userProxy =
          com.dotmarketing.business.APILocator.getUserProxyAPI()
              .getUserProxy(user.getUserId(), APILocator.getUserAPI().getSystemUser(), false);
      userProxy.setPrefix("");
      userProxy.setTitle(form.getTitle());
      userProxy.setOrganization(form.getOrganization());
      userProxy.setUserId(user.getUserId());
      com.dotmarketing.business.APILocator.getUserProxyAPI()
          .saveUserProxy(userProxy, APILocator.getUserAPI().getSystemUser(), false);
      // ### END CRETE USER_PROXY ###

      // saving user inode on web form
      form.setUserInode(userProxy.getInode());
      if (UtilMethods.isSet(form.getFormType())) {
        HibernateUtil.saveOrUpdate(form);
      }

      ///// WE CAN DO THIS! BUT WE NEED TO ADD CATEGORIES TO WEBFORM AND ALSO CHANGE THE PROCESSES
      // THAT
      //// CREATE THE EXCEL DOWNLOAD FROM WEB FORMS. I DIDN'T ADD IT SO I COMMENTED THIS CODE FOR
      // NOW
      // get the old categories, wipe them out
      /*
      List<Category> categories = InodeFactory.getParentsOfClass(userProxy, Category.class);
      for (int i = 0; i < categories.size(); i++) {
      	categories.get(i).deleteChild(userProxy);
      }
       */
      // Save the new categories
      /*String[] arr = form.getCategories();
      if (arr != null) {
      	for (int i = 0; i < arr.length; i++) {
      		Category node = (Category) InodeFactory.getInode(arr[i], Category.class);
      		node.addChild(userProxy);
      	}
      }*/

      // ### CREATE ADDRESS ###
      try {
        List<Address> addresses = PublicAddressFactory.getAddressesByUserId(user.getUserId());
        Address address =
            (addresses.size() > 0 ? addresses.get(0) : PublicAddressFactory.getInstance());
        address.setStreet1(form.getAddress1() == null ? "" : form.getAddress1());
        address.setStreet2(form.getAddress2() == null ? "" : form.getAddress2());
        address.setCity(form.getCity() == null ? "" : form.getCity());
        address.setState(form.getState() == null ? "" : form.getState());
        address.setZip(form.getZip() == null ? "" : form.getZip());
        String phone = form.getPhone();
        address.setPhone(phone == null ? "" : phone);
        address.setUserId(user.getUserId());
        address.setCompanyId(company.getCompanyId());
        PublicAddressFactory.save(address);
      } catch (Exception ex) {
        Logger.error(this, ex.getMessage(), ex);
      }

      Role defaultRole =
          com.dotmarketing.business.APILocator.getRoleAPI()
              .loadRoleByKey(Config.getStringProperty("CMS_VIEWER_ROLE"));
      String roleId = defaultRole.getId();
      if (InodeUtils.isSet(roleId)) {
        com.dotmarketing.business.APILocator.getRoleAPI().addRoleToUser(roleId, user);
      }
    }
    // ### END CREATE ADDRESS ###

    // ### BUILD THE USER COMMENT ###
    addUserComments(user.getUserId(), form, request);
    // ### END BUILD THE USER COMMENT ###

    /* associate user with their clickstream request */
    if (Config.getBooleanProperty("ENABLE_CLICKSTREAM_TRACKING", false)) {
      ClickstreamFactory.setClickStreamUser(user.getUserId(), request);
    }
  }