001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.AccountNameException;
018 import com.liferay.portal.CompanyMxException;
019 import com.liferay.portal.CompanyVirtualHostException;
020 import com.liferay.portal.CompanyWebIdException;
021 import com.liferay.portal.LocaleException;
022 import com.liferay.portal.NoSuchShardException;
023 import com.liferay.portal.NoSuchUserException;
024 import com.liferay.portal.NoSuchVirtualHostException;
025 import com.liferay.portal.kernel.exception.PortalException;
026 import com.liferay.portal.kernel.exception.SystemException;
027 import com.liferay.portal.kernel.language.LanguageUtil;
028 import com.liferay.portal.kernel.log.Log;
029 import com.liferay.portal.kernel.log.LogFactoryUtil;
030 import com.liferay.portal.kernel.search.FacetedSearcher;
031 import com.liferay.portal.kernel.search.Hits;
032 import com.liferay.portal.kernel.search.Indexer;
033 import com.liferay.portal.kernel.search.SearchContext;
034 import com.liferay.portal.kernel.search.SearchEngineUtil;
035 import com.liferay.portal.kernel.search.facet.AssetEntriesFacet;
036 import com.liferay.portal.kernel.search.facet.Facet;
037 import com.liferay.portal.kernel.search.facet.ScopeFacet;
038 import com.liferay.portal.kernel.util.ArrayUtil;
039 import com.liferay.portal.kernel.util.Base64;
040 import com.liferay.portal.kernel.util.LocaleUtil;
041 import com.liferay.portal.kernel.util.PropsKeys;
042 import com.liferay.portal.kernel.util.StringPool;
043 import com.liferay.portal.kernel.util.StringUtil;
044 import com.liferay.portal.kernel.util.TimeZoneUtil;
045 import com.liferay.portal.kernel.util.UnicodeProperties;
046 import com.liferay.portal.kernel.util.Validator;
047 import com.liferay.portal.kernel.workflow.WorkflowConstants;
048 import com.liferay.portal.model.Account;
049 import com.liferay.portal.model.Company;
050 import com.liferay.portal.model.CompanyConstants;
051 import com.liferay.portal.model.Contact;
052 import com.liferay.portal.model.ContactConstants;
053 import com.liferay.portal.model.Group;
054 import com.liferay.portal.model.GroupConstants;
055 import com.liferay.portal.model.Role;
056 import com.liferay.portal.model.RoleConstants;
057 import com.liferay.portal.model.User;
058 import com.liferay.portal.model.VirtualHost;
059 import com.liferay.portal.service.ServiceContext;
060 import com.liferay.portal.service.base.CompanyLocalServiceBaseImpl;
061 import com.liferay.portal.util.Portal;
062 import com.liferay.portal.util.PortalInstances;
063 import com.liferay.portal.util.PrefsPropsUtil;
064 import com.liferay.portal.util.PropsUtil;
065 import com.liferay.portal.util.PropsValues;
066 import com.liferay.util.Encryptor;
067 import com.liferay.util.EncryptorException;
068
069 import java.io.File;
070 import java.io.IOException;
071 import java.io.InputStream;
072
073 import java.util.ArrayList;
074 import java.util.Calendar;
075 import java.util.Date;
076 import java.util.List;
077 import java.util.Locale;
078 import java.util.Map;
079 import java.util.TimeZone;
080
081 import javax.portlet.PortletException;
082 import javax.portlet.PortletPreferences;
083
084
091 public class CompanyLocalServiceImpl extends CompanyLocalServiceBaseImpl {
092
093
110 public Company addCompany(
111 String webId, String virtualHostname, String mx, String shardName,
112 boolean system, int maxUsers, boolean active)
113 throws PortalException, SystemException {
114
115
116
117 virtualHostname = virtualHostname.trim().toLowerCase();
118
119 if ((Validator.isNull(webId)) ||
120 (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) ||
121 (companyPersistence.fetchByWebId(webId) != null)) {
122
123 throw new CompanyWebIdException();
124 }
125
126 validate(webId, virtualHostname, mx);
127
128 Company company = checkCompany(webId, mx, shardName);
129
130 company.setMx(mx);
131 company.setSystem(system);
132 company.setMaxUsers(maxUsers);
133 company.setActive(active);
134
135 companyPersistence.update(company, false);
136
137
138
139 updateVirtualHost(company.getCompanyId(), virtualHostname);
140
141 return company;
142 }
143
144
155 public Company checkCompany(String webId)
156 throws PortalException, SystemException {
157
158 String mx = webId;
159
160 return companyLocalService.checkCompany(
161 webId, mx, PropsValues.SHARD_DEFAULT_NAME);
162 }
163
164
178 public Company checkCompany(String webId, String mx, String shardName)
179 throws PortalException, SystemException {
180
181
182
183 Date now = new Date();
184
185 Company company = companyPersistence.fetchByWebId(webId);
186
187 if (company == null) {
188 String virtualHostname = webId;
189
190 if (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
191 virtualHostname = _DEFAULT_VIRTUAL_HOST;
192 }
193
194 String homeURL = null;
195 String name = webId;
196 String legalName = null;
197 String legalId = null;
198 String legalType = null;
199 String sicCode = null;
200 String tickerSymbol = null;
201 String industry = null;
202 String type = null;
203 String size = null;
204
205 long companyId = counterLocalService.increment();
206
207 company = companyPersistence.create(companyId);
208
209 try {
210 company.setKey(Base64.objectToString(Encryptor.generateKey()));
211 }
212 catch (EncryptorException ee) {
213 throw new SystemException(ee);
214 }
215
216 company.setWebId(webId);
217 company.setMx(mx);
218 company.setActive(true);
219
220 companyPersistence.update(company, false);
221
222
223
224 shardLocalService.addShard(
225 Company.class.getName(), companyId, shardName);
226
227
228
229 updateCompany(
230 companyId, virtualHostname, mx, homeURL, name, legalName,
231 legalId, legalType, sicCode, tickerSymbol, industry, type,
232 size);
233
234
235
236 updateVirtualHost(companyId, virtualHostname);
237
238
239
240 if (webId.equals("liferay.net")) {
241 company = companyPersistence.findByWebId(webId);
242
243 updateVirtualHost(companyId, "demo.liferay.net");
244
245 updateSecurity(
246 companyId, CompanyConstants.AUTH_TYPE_EA, true, true, true,
247 true, false, true);
248
249 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
250 companyId);
251
252 try {
253 preferences.setValue(
254 PropsKeys.ADMIN_EMAIL_FROM_NAME, "Liferay Demo");
255 preferences.setValue(
256 PropsKeys.ADMIN_EMAIL_FROM_ADDRESS, "test@liferay.net");
257
258 preferences.store();
259 }
260 catch (IOException ioe) {
261 throw new SystemException(ioe);
262 }
263 catch (PortletException pe) {
264 throw new SystemException(pe);
265 }
266 }
267 }
268 else {
269 try {
270 shardLocalService.getShard(
271 Company.class.getName(), company.getCompanyId());
272 }
273 catch (NoSuchShardException nsse) {
274 shardLocalService.addShard(
275 Company.class.getName(), company.getCompanyId(), shardName);
276 }
277 }
278
279 long companyId = company.getCompanyId();
280
281
282
283 checkCompanyKey(companyId);
284
285
286
287 User defaultUser = null;
288
289 try {
290 defaultUser = userLocalService.getDefaultUser(companyId);
291
292 if (!defaultUser.isAgreedToTermsOfUse()) {
293 defaultUser.setAgreedToTermsOfUse(true);
294
295 userPersistence.update(defaultUser, false);
296 }
297 }
298 catch (NoSuchUserException nsue) {
299 long userId = counterLocalService.increment();
300
301 defaultUser = userPersistence.create(userId);
302
303 defaultUser.setCompanyId(companyId);
304 defaultUser.setCreateDate(now);
305 defaultUser.setModifiedDate(now);
306 defaultUser.setDefaultUser(true);
307 defaultUser.setContactId(counterLocalService.increment());
308 defaultUser.setPassword("password");
309 defaultUser.setScreenName(String.valueOf(defaultUser.getUserId()));
310 defaultUser.setEmailAddress("default@" + company.getMx());
311
312 if (Validator.isNotNull(PropsValues.COMPANY_DEFAULT_LOCALE)) {
313 defaultUser.setLanguageId(PropsValues.COMPANY_DEFAULT_LOCALE);
314 }
315 else {
316 Locale locale = LocaleUtil.getDefault();
317
318 defaultUser.setLanguageId(locale.toString());
319 }
320
321 if (Validator.isNotNull(PropsValues.COMPANY_DEFAULT_TIME_ZONE)) {
322 defaultUser.setTimeZoneId(
323 PropsValues.COMPANY_DEFAULT_TIME_ZONE);
324 }
325 else {
326 TimeZone timeZone = TimeZoneUtil.getDefault();
327
328 defaultUser.setTimeZoneId(timeZone.getID());
329 }
330
331 defaultUser.setGreeting(
332 LanguageUtil.format(
333 defaultUser.getLocale(), "welcome-x", StringPool.BLANK,
334 false));
335 defaultUser.setLoginDate(now);
336 defaultUser.setFailedLoginAttempts(0);
337 defaultUser.setAgreedToTermsOfUse(true);
338 defaultUser.setStatus(WorkflowConstants.STATUS_APPROVED);
339
340 userPersistence.update(defaultUser, false);
341
342
343
344 Contact defaultContact = contactPersistence.create(
345 defaultUser.getContactId());
346
347 defaultContact.setCompanyId(defaultUser.getCompanyId());
348 defaultContact.setUserId(defaultUser.getUserId());
349 defaultContact.setUserName(StringPool.BLANK);
350 defaultContact.setCreateDate(now);
351 defaultContact.setModifiedDate(now);
352 defaultContact.setAccountId(company.getAccountId());
353 defaultContact.setParentContactId(
354 ContactConstants.DEFAULT_PARENT_CONTACT_ID);
355 defaultContact.setFirstName(StringPool.BLANK);
356 defaultContact.setMiddleName(StringPool.BLANK);
357 defaultContact.setLastName(StringPool.BLANK);
358 defaultContact.setMale(true);
359 defaultContact.setBirthday(now);
360
361 contactPersistence.update(defaultContact, false);
362 }
363
364
365
366 roleLocalService.checkSystemRoles(companyId);
367
368
369
370 groupLocalService.checkSystemGroups(companyId);
371
372
373
374 groupLocalService.checkCompanyGroup(companyId);
375
376
377
378 passwordPolicyLocalService.checkDefaultPasswordPolicy(companyId);
379
380
381
382 Role guestRole = roleLocalService.getRole(
383 companyId, RoleConstants.GUEST);
384
385 roleLocalService.setUserRoles(
386 defaultUser.getUserId(), new long[] {guestRole.getRoleId()});
387
388
389
390 if (userPersistence.countByCompanyId(companyId) == 1) {
391 long creatorUserId = 0;
392 boolean autoPassword = false;
393 String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
394 String password2 = password1;
395 boolean autoScreenName = false;
396 String screenName = PropsValues.DEFAULT_ADMIN_SCREEN_NAME;
397
398 String emailAddress = null;
399
400 if (companyPersistence.countAll() == 1) {
401 emailAddress = PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS;
402 }
403
404 if (Validator.isNull(emailAddress)) {
405 emailAddress =
406 PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX + "@" + mx;
407 }
408
409 long facebookId = 0;
410 String openId = StringPool.BLANK;
411 Locale locale = defaultUser.getLocale();
412 String firstName = PropsValues.DEFAULT_ADMIN_FIRST_NAME;
413 String middleName = PropsValues.DEFAULT_ADMIN_MIDDLE_NAME;
414 String lastName = PropsValues.DEFAULT_ADMIN_LAST_NAME;
415 int prefixId = 0;
416 int suffixId = 0;
417 boolean male = true;
418 int birthdayMonth = Calendar.JANUARY;
419 int birthdayDay = 1;
420 int birthdayYear = 1970;
421 String jobTitle = StringPool.BLANK;
422
423 Group guestGroup = groupLocalService.getGroup(
424 companyId, GroupConstants.GUEST);
425
426 long[] groupIds = new long[] {guestGroup.getGroupId()};
427
428 long[] organizationIds = null;
429
430 Role adminRole = roleLocalService.getRole(
431 companyId, RoleConstants.ADMINISTRATOR);
432
433 Role powerUserRole = roleLocalService.getRole(
434 companyId, RoleConstants.POWER_USER);
435
436 long[] roleIds = new long[] {
437 adminRole.getRoleId(), powerUserRole.getRoleId()
438 };
439
440 long[] userGroupIds = null;
441 boolean sendEmail = false;
442 ServiceContext serviceContext = new ServiceContext();
443
444 User defaultAdminUser = userLocalService.addUser(
445 creatorUserId, companyId, autoPassword, password1, password2,
446 autoScreenName, screenName, emailAddress, facebookId, openId,
447 locale, firstName, middleName, lastName, prefixId, suffixId,
448 male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
449 groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
450 serviceContext);
451
452 userLocalService.updateEmailAddressVerified(
453 defaultAdminUser.getUserId(), true);
454
455 userLocalService.updateLastLogin(
456 defaultAdminUser.getUserId(), defaultAdminUser.getLoginIP());
457
458 userLocalService.updatePasswordReset(
459 defaultAdminUser.getUserId(), false);
460 }
461
462
463
464 portletLocalService.checkPortlets(companyId);
465
466 return company;
467 }
468
469
478 public void checkCompanyKey(long companyId)
479 throws PortalException, SystemException {
480
481 Company company = companyPersistence.findByPrimaryKey(companyId);
482
483 if ((Validator.isNull(company.getKey())) &&
484 (company.getKeyObj() == null)) {
485
486 try {
487 company.setKey(Base64.objectToString(Encryptor.generateKey()));
488 }
489 catch (EncryptorException ee) {
490 throw new SystemException(ee);
491 }
492
493 companyPersistence.update(company, false);
494 }
495 }
496
497
505 public void deleteLogo(long companyId)
506 throws PortalException, SystemException {
507
508 Company company = companyPersistence.findByPrimaryKey(companyId);
509
510 long logoId = company.getLogoId();
511
512 if (logoId > 0) {
513 company.setLogoId(0);
514
515 companyPersistence.update(company, false);
516
517 imageLocalService.deleteImage(logoId);
518 }
519 }
520
521
529 public Company fetchCompanyById(long companyId) throws SystemException {
530 return companyPersistence.fetchByPrimaryKey(companyId);
531 }
532
533
541 public Company fetchCompanyByVirtualHost(String virtualHostname)
542 throws SystemException {
543
544 virtualHostname = virtualHostname.trim().toLowerCase();
545
546 VirtualHost virtualHost = virtualHostPersistence.fetchByHostname(
547 virtualHostname);
548
549 if ((virtualHost == null) || (virtualHost.getLayoutSetId() != 0)) {
550 return null;
551 }
552
553 return companyPersistence.fetchByPrimaryKey(virtualHost.getCompanyId());
554 }
555
556
562 public List<Company> getCompanies() throws SystemException {
563 return companyPersistence.findAll();
564 }
565
566
574 public List<Company> getCompanies(boolean system) throws SystemException {
575 return companyPersistence.findBySystem(system);
576 }
577
578
586 public int getCompaniesCount(boolean system) throws SystemException {
587 return companyPersistence.countBySystem(system);
588 }
589
590
599 public Company getCompanyById(long companyId)
600 throws PortalException, SystemException {
601
602 return companyPersistence.findByPrimaryKey(companyId);
603 }
604
605
613 public Company getCompanyByLogoId(long logoId)
614 throws PortalException, SystemException {
615
616 return companyPersistence.findByLogoId(logoId);
617 }
618
619
628 public Company getCompanyByMx(String mx)
629 throws PortalException, SystemException {
630
631 return companyPersistence.findByMx(mx);
632 }
633
634
644 public Company getCompanyByVirtualHost(String virtualHostname)
645 throws PortalException, SystemException {
646
647 try {
648 virtualHostname = virtualHostname.trim().toLowerCase();
649
650 VirtualHost virtualHost = virtualHostPersistence.findByHostname(
651 virtualHostname);
652
653 if (virtualHost.getLayoutSetId() != 0) {
654 throw new CompanyVirtualHostException(
655 "Virtual host is associated with layout set " +
656 virtualHost.getLayoutSetId());
657 }
658
659 return companyPersistence.findByPrimaryKey(
660 virtualHost.getCompanyId());
661 }
662 catch (NoSuchVirtualHostException nsvhe) {
663 throw new CompanyVirtualHostException(nsvhe);
664 }
665 }
666
667
676 public Company getCompanyByWebId(String webId)
677 throws PortalException, SystemException {
678
679 return companyPersistence.findByWebId(webId);
680 }
681
682
691 public long getCompanyIdByUserId(long userId) throws Exception {
692 long[] companyIds = PortalInstances.getCompanyIds();
693
694 long companyId = 0;
695
696 if (companyIds.length == 1) {
697 companyId = companyIds[0];
698 }
699 else if (companyIds.length > 1) {
700 try {
701 User user = userPersistence.findByPrimaryKey(userId);
702
703 companyId = user.getCompanyId();
704 }
705 catch (Exception e) {
706 if (_log.isWarnEnabled()) {
707 _log.warn(
708 "Unable to get the company id for user " + userId, e);
709 }
710 }
711 }
712
713 return companyId;
714 }
715
716
727 public void removePreferences(long companyId, String[] keys)
728 throws SystemException {
729
730 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
731 companyId);
732
733 try {
734 for (String key : keys) {
735 preferences.reset(key);
736 }
737
738 preferences.store();
739 }
740 catch (Exception e) {
741 throw new SystemException(e);
742 }
743 }
744
745
763 public Hits search(
764 long companyId, long userId, String keywords, int start, int end)
765 throws SystemException {
766
767 return search(companyId, userId, null, 0, null, keywords, start, end);
768 }
769
770
789 public Hits search(
790 long companyId, long userId, String portletId, long groupId,
791 String type, String keywords, int start, int end)
792 throws SystemException {
793
794 try {
795 SearchContext searchContext = new SearchContext();
796
797 Facet assetEntriesFacet = new AssetEntriesFacet(searchContext);
798
799 assetEntriesFacet.setStatic(true);
800
801 searchContext.addFacet(assetEntriesFacet);
802
803 Facet scopeFacet = new ScopeFacet(searchContext);
804
805 scopeFacet.setStatic(true);
806
807 searchContext.addFacet(scopeFacet);
808
809 searchContext.setCompanyId(companyId);
810 searchContext.setEnd(end);
811 searchContext.setEntryClassNames(
812 SearchEngineUtil.getEntryClassNames());
813
814 if (groupId > 0) {
815 searchContext.setGroupIds(new long[]{groupId});
816 }
817
818 searchContext.setKeywords(keywords);
819
820 if (Validator.isNotNull(portletId)) {
821 searchContext.setPortletIds(new String[] {portletId});
822 }
823
824 searchContext.setSearchEngineId(SearchEngineUtil.SYSTEM_ENGINE_ID);
825 searchContext.setStart(start);
826 searchContext.setUserId(userId);
827
828 Indexer indexer = FacetedSearcher.getInstance();
829
830 return indexer.search(searchContext);
831 }
832 catch (Exception e) {
833 throw new SystemException(e);
834 }
835 }
836
837
851 public Company updateCompany(
852 long companyId, String virtualHostname, String mx, int maxUsers,
853 boolean active)
854 throws PortalException, SystemException {
855
856
857
858 virtualHostname = virtualHostname.trim().toLowerCase();
859
860 if (!active) {
861 if (companyId == PortalInstances.getDefaultCompanyId()) {
862 active = true;
863 }
864 }
865
866 Company company = companyPersistence.findByPrimaryKey(companyId);
867
868 validate(company.getWebId(), virtualHostname, mx);
869
870 if (PropsValues.MAIL_MX_UPDATE) {
871 company.setMx(mx);
872 }
873
874 company.setMaxUsers(maxUsers);
875 company.setActive(active);
876
877 companyPersistence.update(company, false);
878
879
880
881 updateVirtualHost(companyId, virtualHostname);
882
883 return company;
884 }
885
886
913 public Company updateCompany(
914 long companyId, String virtualHostname, String mx, String homeURL,
915 String name, String legalName, String legalId, String legalType,
916 String sicCode, String tickerSymbol, String industry, String type,
917 String size)
918 throws PortalException, SystemException {
919
920
921
922 virtualHostname = virtualHostname.trim().toLowerCase();
923 Date now = new Date();
924
925 Company company = companyPersistence.findByPrimaryKey(companyId);
926
927 validate(company.getWebId(), virtualHostname, mx);
928 validate(name);
929
930 if (PropsValues.MAIL_MX_UPDATE) {
931 company.setMx(mx);
932 }
933
934 company.setHomeURL(homeURL);
935
936 companyPersistence.update(company, false);
937
938
939
940 Account account = accountPersistence.fetchByPrimaryKey(
941 company.getAccountId());
942
943 if (account == null) {
944 long accountId = counterLocalService.increment();
945
946 account = accountPersistence.create(accountId);
947
948 account.setCreateDate(now);
949 account.setCompanyId(companyId);
950 account.setUserId(0);
951 account.setUserName(StringPool.BLANK);
952
953 company.setAccountId(accountId);
954
955 companyPersistence.update(company, false);
956 }
957
958 account.setModifiedDate(now);
959 account.setName(name);
960 account.setLegalName(legalName);
961 account.setLegalId(legalId);
962 account.setLegalType(legalType);
963 account.setSicCode(sicCode);
964 account.setTickerSymbol(tickerSymbol);
965 account.setIndustry(industry);
966 account.setType(type);
967 account.setSize(size);
968
969 accountPersistence.update(account, false);
970
971
972
973 updateVirtualHost(companyId, virtualHostname);
974
975 return company;
976 }
977
978
987 public void updateDisplay(
988 long companyId, String languageId, String timeZoneId)
989 throws PortalException, SystemException {
990
991 User user = userLocalService.getDefaultUser(companyId);
992
993 user.setLanguageId(languageId);
994 user.setTimeZoneId(timeZoneId);
995
996 userPersistence.update(user, false);
997 }
998
999
1009 public Company updateLogo(long companyId, byte[] bytes)
1010 throws PortalException, SystemException {
1011
1012 Company company = checkLogo(companyId);
1013
1014 imageLocalService.updateImage(company.getLogoId(), bytes);
1015
1016 return company;
1017 }
1018
1019
1029 public Company updateLogo(long companyId, File file)
1030 throws PortalException, SystemException {
1031
1032 Company company = checkLogo(companyId);
1033
1034 imageLocalService.updateImage(company.getLogoId(), file);
1035
1036 return company;
1037 }
1038
1039
1049 public Company updateLogo(long companyId, InputStream is)
1050 throws PortalException, SystemException {
1051
1052 Company company = checkLogo(companyId);
1053
1054 imageLocalService.updateImage(company.getLogoId(), is);
1055
1056 return company;
1057 }
1058
1059
1070 public void updatePreferences(long companyId, UnicodeProperties properties)
1071 throws PortalException, SystemException {
1072
1073 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
1074 companyId);
1075
1076 try {
1077 String newLocales = properties.getProperty(PropsKeys.LOCALES);
1078
1079 if (newLocales != null) {
1080 String oldLocales = preferences.getValue(
1081 PropsKeys.LOCALES, StringPool.BLANK);
1082
1083 if (!Validator.equals(oldLocales, newLocales)) {
1084 validateLocales(newLocales);
1085
1086 LanguageUtil.resetAvailableLocales(companyId);
1087 }
1088 }
1089
1090 List<String> resetKeys = new ArrayList<String>();
1091
1092 for (Map.Entry<String, String> entry : properties.entrySet()) {
1093 String key = entry.getKey();
1094 String value = entry.getValue();
1095
1096 if (value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
1097 continue;
1098 }
1099
1100 String propsUtilValue = PropsUtil.get(key);
1101
1102 if (!value.equals(propsUtilValue)) {
1103 preferences.setValue(key, value);
1104 }
1105 else {
1106 String preferencesValue = preferences.getValue(key, null);
1107
1108 if (preferencesValue != null) {
1109 resetKeys.add(key);
1110 }
1111 }
1112 }
1113
1114 for (String key : resetKeys) {
1115 preferences.reset(key);
1116 }
1117
1118 preferences.store();
1119 }
1120 catch (LocaleException le) {
1121 throw le;
1122 }
1123 catch (Exception e) {
1124 throw new SystemException(e);
1125 }
1126 }
1127
1128
1147 public void updateSecurity(
1148 long companyId, String authType, boolean autoLogin,
1149 boolean sendPassword, boolean strangers, boolean strangersWithMx,
1150 boolean strangersVerify, boolean siteLogo)
1151 throws SystemException {
1152
1153 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
1154 companyId);
1155
1156 try {
1157 preferences.setValue(
1158 PropsKeys.COMPANY_SECURITY_AUTH_TYPE, authType);
1159 preferences.setValue(
1160 PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
1161 String.valueOf(autoLogin));
1162 preferences.setValue(
1163 PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
1164 String.valueOf(sendPassword));
1165 preferences.setValue(
1166 PropsKeys.COMPANY_SECURITY_STRANGERS,
1167 String.valueOf(strangers));
1168 preferences.setValue(
1169 PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
1170 String.valueOf(strangersWithMx));
1171 preferences.setValue(
1172 PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
1173 String.valueOf(strangersVerify));
1174 preferences.setValue(
1175 PropsKeys.COMPANY_SECURITY_SITE_LOGO, String.valueOf(siteLogo));
1176
1177 preferences.store();
1178 }
1179 catch (IOException ioe) {
1180 throw new SystemException(ioe);
1181 }
1182 catch (PortletException pe) {
1183 throw new SystemException(pe);
1184 }
1185 }
1186
1187 protected Company checkLogo(long companyId)
1188 throws PortalException, SystemException {
1189
1190 Company company = companyPersistence.findByPrimaryKey(companyId);
1191
1192 long logoId = company.getLogoId();
1193
1194 if (logoId <= 0) {
1195 logoId = counterLocalService.increment();
1196
1197 company.setLogoId(logoId);
1198
1199 company = companyPersistence.update(company, false);
1200 }
1201
1202 return company;
1203 }
1204
1205 protected void updateVirtualHost(long companyId, String virtualHostname)
1206 throws CompanyVirtualHostException, SystemException {
1207
1208 if (Validator.isNotNull(virtualHostname)) {
1209 try {
1210 VirtualHost virtualHost = virtualHostPersistence.findByHostname(
1211 virtualHostname);
1212
1213 if ((virtualHost.getCompanyId() != companyId) ||
1214 (virtualHost.getLayoutSetId() != 0)) {
1215
1216 throw new CompanyVirtualHostException();
1217 }
1218 }
1219 catch (NoSuchVirtualHostException nsvhe) {
1220 virtualHostLocalService.updateVirtualHost(
1221 companyId, 0, virtualHostname);
1222 }
1223 }
1224 else {
1225 try {
1226 virtualHostPersistence.removeByC_L(companyId, 0);
1227 }
1228 catch (NoSuchVirtualHostException nsvhe) {
1229 }
1230 }
1231 }
1232
1233 protected void validate(String name) throws PortalException {
1234 if (Validator.isNull(name)) {
1235 throw new AccountNameException();
1236 }
1237 }
1238
1239 protected void validate(String webId, String virtualHostname, String mx)
1240 throws PortalException, SystemException {
1241
1242 if (Validator.isNull(virtualHostname)) {
1243 throw new CompanyVirtualHostException();
1244 }
1245 else if (virtualHostname.equals(_DEFAULT_VIRTUAL_HOST) &&
1246 !webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
1247
1248 throw new CompanyVirtualHostException();
1249 }
1250 else if (!Validator.isDomain(virtualHostname)) {
1251 throw new CompanyVirtualHostException();
1252 }
1253 else {
1254 try {
1255 VirtualHost virtualHost = virtualHostPersistence.findByHostname(
1256 virtualHostname);
1257
1258 long companyId = virtualHost.getCompanyId();
1259
1260 Company virtualHostnameCompany =
1261 companyPersistence.findByPrimaryKey(companyId);
1262
1263 if (!virtualHostnameCompany.getWebId().equals(webId)) {
1264 throw new CompanyVirtualHostException();
1265 }
1266 }
1267 catch (NoSuchVirtualHostException nsvhe) {
1268 }
1269 }
1270
1271 if (Validator.isNull(mx)) {
1272 throw new CompanyMxException();
1273 }
1274 else if (!Validator.isDomain(mx)) {
1275 throw new CompanyMxException();
1276 }
1277 }
1278
1279 protected void validateLocales(String locales) throws PortalException {
1280 String[] localesArray = StringUtil.split(locales, StringPool.COMMA);
1281
1282 for (String locale : localesArray) {
1283 if (!ArrayUtil.contains(PropsValues.LOCALES, locale)) {
1284 throw new LocaleException();
1285 }
1286 }
1287 }
1288
1289 private static final String _DEFAULT_VIRTUAL_HOST = "localhost";
1290
1291 private static Log _log = LogFactoryUtil.getLog(
1292 CompanyLocalServiceImpl.class);
1293
1294 }